From d48abe61217d4d459a88b68b0c794fc6c39593e5 Mon Sep 17 00:00:00 2001 From: Mariela Michalek Date: Sun, 9 Feb 2014 17:50:57 -0300 Subject: [PATCH] refactor test - add helper class --- .../test/rest/RestAccountITCase.java | 42 ++---------- .../test/rest/RestAdminITCase.java | 42 +++--------- .../com/wisemapping/test/rest/RestHelper.java | 53 +++++++++++++++ .../test/rest/RestMindmapITCase.java | 67 ++++++------------- .../test/rest/RestMindmapLabelITCase.java | 42 ++---------- 5 files changed, 97 insertions(+), 149 deletions(-) create mode 100644 wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java index 53b3646f..6676f5b2 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java @@ -39,19 +39,18 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; +import static com.wisemapping.test.rest.RestHelper.ADMIN_CREDENTIALS; +import static com.wisemapping.test.rest.RestHelper.BASE_REST_URL; +import static com.wisemapping.test.rest.RestHelper.HOST_PORT; +import static com.wisemapping.test.rest.RestHelper.createHeaders; +import static com.wisemapping.test.rest.RestHelper.createTemplate; import static org.testng.Assert.assertEquals; @Test public class RestAccountITCase { - @NonNls - private static final String HOST_PORT = "http://localhost:8080"; - private static final String BASE_REST_URL = HOST_PORT + "/service"; - private static final String ADMIN_CREDENTIALS = "admin@wisemapping.org" + ":" + "admin"; - - - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); final RestTemplate adminTemplate = createTemplate(ADMIN_CREDENTIALS); @@ -115,30 +114,6 @@ public class RestAccountITCase { return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); } - private HttpHeaders createHeaders(@NotNull MediaType mediaType) { - List acceptableMediaTypes = new ArrayList(); - acceptableMediaTypes.add(mediaType); - - final HttpHeaders result = new HttpHeaders(); - result.setAccept(acceptableMediaTypes); - result.setContentType(mediaType); - return result; - } - - private RestTemplate createTemplate(@NotNull final String authorisation) { - SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { - @Override - protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { - super.prepareConnection(connection, httpMethod); - - byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes()); - connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); - } - - }; - return new RestTemplate(s); - } - private RestUser createDummyUser() { final RestUser restUser = new RestUser(); final String username = "foo-to-delete" + System.nanoTime(); @@ -150,9 +125,4 @@ public class RestAccountITCase { return restUser; } - - @DataProvider(name = "ContentType-Provider-Function") - public Object[][] contentTypes() { - return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}}; - } } diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java index 9e30855c..d4c420e7 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java @@ -29,6 +29,9 @@ import org.springframework.web.client.RestTemplate; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import static com.wisemapping.test.rest.RestHelper.BASE_REST_URL; +import static com.wisemapping.test.rest.RestHelper.HOST_PORT; +import static com.wisemapping.test.rest.RestHelper.createTemplate; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; @@ -40,17 +43,15 @@ import java.util.ArrayList; import java.util.List; -@Test +@Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public class RestAdminITCase { - @NonNls - private static final String HOST_PORT = "http://localhost:8080"; - private static final String BASE_REST_URL = HOST_PORT + "/service"; + String authorisation = "admin@wisemapping.org" + ":" + "admin"; - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(); + final RestTemplate templateRest = createTemplate(authorisation); // Fill user data ... final RestUser restUser = createDummyUser(); @@ -68,10 +69,10 @@ public class RestAdminITCase { } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(); + final RestTemplate templateRest = createTemplate(authorisation); final RestUser restUser = createDummyUser(); @@ -97,7 +98,7 @@ public class RestAdminITCase { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(); + final RestTemplate templateRest = createTemplate(authorisation); // Fill user data ... final RestUser restUser = createDummyUser(); @@ -116,7 +117,7 @@ public class RestAdminITCase { return restUser.getEmail(); } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void createUser(final @NotNull MediaType mediaType) { this.createNewUser(mediaType); } @@ -150,22 +151,6 @@ public class RestAdminITCase { return result; } - private RestTemplate createTemplate() { - SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { - @Override - protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { - super.prepareConnection(connection, httpMethod); - - //Basic Authentication for Police API - String authorisation = "admin@wisemapping.org" + ":" + "admin"; - byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes()); - connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); - } - - }; - return new RestTemplate(s); - } - private RestUser createDummyUser() { final RestUser restUser = new RestUser(); final String username = "foo-to-delete" + System.nanoTime(); @@ -177,9 +162,4 @@ public class RestAdminITCase { return restUser; } - - @DataProvider(name = "ContentType-Provider-Function") - public Object[][] contentTypes() { - return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}}; - } } diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java new file mode 100644 index 00000000..69b3ceb3 --- /dev/null +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java @@ -0,0 +1,53 @@ +package com.wisemapping.test.rest; + +import com.wisemapping.rest.model.RestUser; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.security.crypto.codec.Base64; +import org.springframework.web.client.RestTemplate; +import org.testng.annotations.DataProvider; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.List; + +public class RestHelper { + + public static final String HOST_PORT = "http://localhost:8080"; + public static final String BASE_REST_URL = HOST_PORT + "/service"; + public static final String ADMIN_CREDENTIALS = "admin@wisemapping.org" + ":" + "admin"; + + public static HttpHeaders createHeaders(@NotNull MediaType mediaType) { + List acceptableMediaTypes = new ArrayList(); + acceptableMediaTypes.add(mediaType); + + final HttpHeaders result = new HttpHeaders(); + result.setAccept(acceptableMediaTypes); + result.setContentType(mediaType); + return result; + } + + public static RestTemplate createTemplate(@NotNull final String authorisation) { + SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { + @Override + protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { + super.prepareConnection(connection, httpMethod); + + byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes()); + connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); + } + + }; + return new RestTemplate(s); + } + + @DataProvider(name = "ContentType-Provider-Function") + public static Object[][] contentTypes() { + return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}}; + } + +} diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java index 65113336..de5e8944 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java @@ -28,6 +28,10 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; +import static com.wisemapping.test.rest.RestHelper.BASE_REST_URL; +import static com.wisemapping.test.rest.RestHelper.HOST_PORT; +import static com.wisemapping.test.rest.RestHelper.createHeaders; +import static com.wisemapping.test.rest.RestHelper.createTemplate; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -36,20 +40,19 @@ import static org.testng.Assert.fail; public class RestMindmapITCase { private String userEmail = "admin@wisemapping.com"; - private static final String HOST_PORT = "http://localhost:8080"; - private static final String BASE_REST_URL = HOST_PORT + "/service"; @BeforeClass void createUser() { final RestAdminITCase restAdminITCase = new RestAdminITCase(); userEmail = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON); + userEmail += ":" + "admin"; } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void listMaps(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final String title1 = "List Maps 1 - " + mediaType.toString(); @@ -79,10 +82,10 @@ public class RestMindmapITCase { assertTrue(found1 && found2, "Map could not be found"); } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void deleteMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final String title1 = "Map to delete - " + mediaType.toString(); @@ -99,10 +102,10 @@ public class RestMindmapITCase { } } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void changeMapTitle(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final URI resourceUri = addNewMap(requestHeaders, template, "Map to change title - " + mediaType.toString()); @@ -118,10 +121,10 @@ public class RestMindmapITCase { assertEquals(newTitle, map.getTitle()); } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void validateMapsCreation(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final String title = "Map to Validate Creation - " + mediaType.toString(); @@ -145,10 +148,10 @@ public class RestMindmapITCase { } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void changeMapDescription(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final URI resourceUri = addNewMap(requestHeaders, template, "Map to change Description - " + mediaType.toString()); @@ -164,10 +167,10 @@ public class RestMindmapITCase { assertEquals(newDescription, map.getDescription()); } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void updateMapXml(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final String title = "Update XML sample " + mediaType.toString(); @@ -185,10 +188,10 @@ public class RestMindmapITCase { assertEquals(response.getXml(), newXmlContent); } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void cloneMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final String title = "Map to clone sample " + mediaType.toString(); @@ -210,14 +213,14 @@ public class RestMindmapITCase { } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void updateMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... if(MediaType.APPLICATION_XML==mediaType){ throw new SkipException("Some research need to check why it;s falling."); } final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final RestTemplate template = createTemplate(userEmail); // Create a sample map ... final String title = "Update sample " + mediaType.toString(); @@ -265,32 +268,4 @@ public class RestMindmapITCase { return addNewMap(requestHeaders, template, title, null); } - private HttpHeaders createHeaders(@NotNull MediaType mediaType) { - List acceptableMediaTypes = new ArrayList(); - acceptableMediaTypes.add(mediaType); - final HttpHeaders requestHeaders = new HttpHeaders(); - requestHeaders.setAccept(acceptableMediaTypes); - requestHeaders.setContentType(mediaType); - return requestHeaders; - } - - private RestTemplate createTemplate() { - SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { - @Override - protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { - super.prepareConnection(connection, httpMethod); - - //Basic Authentication for Police API - String authorization = userEmail + ":" + "admin"; - byte[] encodedAuthorisation = Base64.encode(authorization.getBytes()); - connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); - } - }; - return new RestTemplate(s); - } - - @DataProvider(name = "ContentType-Provider-Function") - public Object[][] contentTypes() { - return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}}; - } } diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapLabelITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapLabelITCase.java index 2c6191a0..e0ab59c7 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapLabelITCase.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapLabelITCase.java @@ -30,6 +30,7 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; +import static com.wisemapping.test.rest.RestHelper.BASE_REST_URL; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -37,9 +38,7 @@ import static org.testng.Assert.fail; @Test public class RestMindmapLabelITCase { - private String userEmail = "admin@wisemapping.com"; - private static final String HOST_PORT = "http://localhost:8080"; - private static final String BASE_REST_URL = HOST_PORT + "/service"; + private String userEmail; @BeforeClass void createUser() { @@ -48,10 +47,10 @@ public class RestMindmapLabelITCase { userEmail = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON); } - @Test(dataProvider = "ContentType-Provider-Function") + @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function") public void createLabel(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ... - final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate template = createTemplate(); + final HttpHeaders requestHeaders = RestHelper.createHeaders(mediaType); + final RestTemplate template = RestHelper.createTemplate( userEmail + ":" + "admin"); // Create a new label final String title1 = "Label 1 - " + mediaType.toString(); @@ -96,33 +95,4 @@ public class RestMindmapLabelITCase { private URI addNewLabel(@NotNull HttpHeaders requestHeaders, @NotNull RestTemplate template, @NotNull String title) throws IOException, WiseMappingException { return addNewLabel(requestHeaders, template, title, null); } - - private HttpHeaders createHeaders(@NotNull MediaType mediaType) { - List acceptableMediaTypes = new ArrayList(); - acceptableMediaTypes.add(mediaType); - final HttpHeaders requestHeaders = new HttpHeaders(); - requestHeaders.setAccept(acceptableMediaTypes); - requestHeaders.setContentType(mediaType); - return requestHeaders; - } - - private RestTemplate createTemplate() { - SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { - @Override - protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { - super.prepareConnection(connection, httpMethod); - - //Basic Authentication for Police API - String authorization = userEmail + ":" + "admin"; - byte[] encodedAuthorisation = Base64.encode(authorization.getBytes()); - connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); - } - }; - return new RestTemplate(s); - } - - @DataProvider(name = "ContentType-Provider-Function") - public Object[][] contentTypes() { - return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}}; - } -} +} \ No newline at end of file