package org.gcube.common.gxrest.request; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Properties; import java.util.WeakHashMap; import org.gcube.com.fasterxml.jackson.databind.JsonMappingException; import org.gcube.common.gxrest.response.inbound.GXInboundResponse; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; /** * Test cases for {@link GXHTTPStringRequest} * * @author Manuele Simi (ISTI-CNR) * */ @RunWith(BlockJUnit4ClassRunner.class) public class GXHTTPStringRequestTest { private GXHTTPStringRequest request; public static String DEFAULT_TEST_SCOPE = ""; static String DEFAULT_RM_URL = ""; static String DEFAULT_RR_URL = ""; private static boolean skipTest = false; static { Properties properties = new Properties(); try (InputStream input = GXHTTPStringRequestTest.class.getClassLoader().getResourceAsStream("token.props")) { // load the properties file properties.load(input); DEFAULT_TEST_SCOPE = properties.getProperty("DEFAULT_SCOPE_TOKEN"); if (DEFAULT_TEST_SCOPE.isEmpty()) skipTest = true; DEFAULT_RM_URL = properties.getProperty("RM_URL"); DEFAULT_RR_URL = properties.getProperty("RR_URL"); } catch (IOException | NullPointerException e) { skipTest = true; } } @BeforeClass public static void beforeClass() throws Exception { setContext(DEFAULT_TEST_SCOPE); } public static void setContext(String token) throws Exception { if (DEFAULT_TEST_SCOPE.isEmpty()) { skipTest = true; return; } //SecurityTokenProvider.instance.set(token); } @AfterClass public static void afterClass() throws Exception { //SecurityTokenProvider.instance.reset(); } /** * Test method for {@link org.gcube.common.gxrest.request.GXHTTPStringRequest#newRequest(java.lang.String)}. */ @Before public void testNewRequest() { request = GXHTTPStringRequest.newRequest(DEFAULT_RM_URL).from("GXRequestTest"); } /** * Test method for {@link org.gcube.common.gxrest.request.GXHTTPStringRequest#post()}. */ @Test public void testPost() { if (skipTest) return; request.clear(); String context ="{\"@class\":\"Context\",\"header\":{\"@class\":\"Header\",\"uuid\":\"b252e6ce-8b9a-4142-9bca-3ab5486313c4\",\"creator\":null,\"modifiedBy\":\"gxRestTest\",\"creationTime\":null,\"lastUpdateTime\":null},\"name\":\"gxTest\",\"parent\":null,\"children\":[]}"; Map queryParams = new WeakHashMap<>(); queryParams.put("rrURL", DEFAULT_RR_URL); try { GXInboundResponse response = request.path("gxrest") .header("Another header", "GXHTTPRequestTest") .queryParams(queryParams).post(context); if (response.hasException()) { try { throw response.getException(); } catch (ClassNotFoundException e) { //that's OK, we can tolerate this } catch (Exception e) { e.printStackTrace(); throw e; } } assertTrue("Unexpected returned code.", response.hasCREATEDCode()); System.out.println("Returned string " + response.getStreamedContentAsString()); } catch (Exception e) { e.printStackTrace(); fail("Failed to send a POST request"); } } @SuppressWarnings("deprecation") @Test(expected=org.gcube.com.fasterxml.jackson.databind.JsonMappingException.class) public void testPostNoBody() throws Exception { if (skipTest) throw new JsonMappingException(""); request.clear(); Map queryParams = new WeakHashMap<>(); queryParams.put("rrURL", DEFAULT_RR_URL); try { GXInboundResponse response = request.path("gxrest") .header("Another header", "GXHTTPRequestTest") .queryParams(queryParams).post(); assertTrue("Unexpected returned code.", response.hasNOT_ACCEPTABLECode()); assertTrue("Unexpected GX status", response.hasGXError()); if (response.hasGXError()) { try { throw response.getException(); } catch (ClassNotFoundException e) { //that's OK, we can tolerate this } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("Returned string " + response.getStreamedContentAsString()); } } catch (Exception e) { e.printStackTrace(); throw e; //fail("Failed to send a POST request"); } } /** * Test method for {@link org.gcube.common.gxrest.request.GXHTTPStringRequest#get()}. */ @Test public void testGet() { request.clear(); } /** * Test method for {@link org.gcube.common.gxrest.request.GXHTTPStringRequest#put()}. */ @Test public void testPut() { if (skipTest) return; request.clear(); Map queryParams = new WeakHashMap<>(); queryParams.put("rrURL", DEFAULT_RR_URL); String context ="{\"@class\":\"Context\",\"header\":{\"@class\":\"Header\",\"uuid\":\"6f86dc81-2f59-486b-8aa9-3ab5486313c4\",\"creator\":null,\"modifiedBy\":\"gxRestTest\",\"creationTime\":null,\"lastUpdateTime\":null},\"name\":\"gxTest\",\"parent\":null,\"children\":[]}"; try { GXInboundResponse response = request.path("gxrest") .header("Another header", "GXHTTPRequestTest") .path("5f86dc81-2f59-486b-8aa9-3ab5486313c4").queryParams(queryParams).put(context); assertTrue("Unexpected returned code.", response.hasCREATEDCode()); if (response.hasException()) { try { throw response.getException(); } catch (ClassNotFoundException e) { //that's OK, we can tolerate this } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("Returned string " + response.getStreamedContentAsString()); } } catch (Exception e) { e.printStackTrace(); fail("Failed to send a DELETE request"); } } /** * Test method for {@link org.gcube.common.gxrest.request.GXHTTPStringRequest#delete()}. */ @Test public void testDelete() { if (skipTest) return; request.clear(); Map queryParams = new WeakHashMap<>(); queryParams.put("rrURL", DEFAULT_RR_URL); try { GXInboundResponse response = request.path("gxrest") .header("Another header", "GXHTTPRequestTest") .path("5f86dc81-2f59-486b-8aa9-3ab5486313c4").queryParams(queryParams).delete(); assertTrue("Unexpected returned code.", response.hasBAD_REQUESTCode()); if (response.hasException()) { try { throw response.getException(); } catch (ClassNotFoundException e) { //that's OK, we can tolerate this } catch (Exception e) { e.printStackTrace(); throw e; } } } catch (Exception e) { e.printStackTrace(); fail("Failed to send a DELETE request"); } } /** * Test method for {@link org.gcube.common.gxrest.request.GXHTTPStringRequest#head()}. */ @Test public void testHead() { request.clear(); } }