refactor test - add helper class

This commit is contained in:
Mariela Michalek 2014-02-09 17:50:57 -03:00
parent f564b4b34a
commit d48abe6121
5 changed files with 97 additions and 149 deletions

View File

@ -39,19 +39,18 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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; import static org.testng.Assert.assertEquals;
@Test @Test
public class RestAccountITCase { public class RestAccountITCase {
@NonNls @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function")
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")
public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ... public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate adminTemplate = createTemplate(ADMIN_CREDENTIALS); final RestTemplate adminTemplate = createTemplate(ADMIN_CREDENTIALS);
@ -115,30 +114,6 @@ public class RestAccountITCase {
return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity);
} }
private HttpHeaders createHeaders(@NotNull MediaType mediaType) {
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
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() { private RestUser createDummyUser() {
final RestUser restUser = new RestUser(); final RestUser restUser = new RestUser();
final String username = "foo-to-delete" + System.nanoTime(); final String username = "foo-to-delete" + System.nanoTime();
@ -150,9 +125,4 @@ public class RestAccountITCase {
return restUser; return restUser;
} }
@DataProvider(name = "ContentType-Provider-Function")
public Object[][] contentTypes() {
return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}};
}
} }

View File

@ -29,6 +29,9 @@ import org.springframework.web.client.RestTemplate;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; 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.assertEquals;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
@ -40,17 +43,15 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Test @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function")
public class RestAdminITCase { public class RestAdminITCase {
@NonNls String authorisation = "admin@wisemapping.org" + ":" + "admin";
private static final String HOST_PORT = "http://localhost:8080";
private static final String BASE_REST_URL = HOST_PORT + "/service";
@Test(dataProvider = "ContentType-Provider-Function") @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function")
public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ... public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(); final RestTemplate templateRest = createTemplate(authorisation);
// Fill user data ... // Fill user data ...
final RestUser restUser = createDummyUser(); 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 ... public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(); final RestTemplate templateRest = createTemplate(authorisation);
final RestUser restUser = createDummyUser(); final RestUser restUser = createDummyUser();
@ -97,7 +98,7 @@ public class RestAdminITCase {
// Configure media types ... // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(); final RestTemplate templateRest = createTemplate(authorisation);
// Fill user data ... // Fill user data ...
final RestUser restUser = createDummyUser(); final RestUser restUser = createDummyUser();
@ -116,7 +117,7 @@ public class RestAdminITCase {
return restUser.getEmail(); return restUser.getEmail();
} }
@Test(dataProvider = "ContentType-Provider-Function") @Test(dataProviderClass = RestHelper.class, dataProvider="ContentType-Provider-Function")
public void createUser(final @NotNull MediaType mediaType) { public void createUser(final @NotNull MediaType mediaType) {
this.createNewUser(mediaType); this.createNewUser(mediaType);
} }
@ -150,22 +151,6 @@ public class RestAdminITCase {
return result; 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() { private RestUser createDummyUser() {
final RestUser restUser = new RestUser(); final RestUser restUser = new RestUser();
final String username = "foo-to-delete" + System.nanoTime(); final String username = "foo-to-delete" + System.nanoTime();
@ -177,9 +162,4 @@ public class RestAdminITCase {
return restUser; return restUser;
} }
@DataProvider(name = "ContentType-Provider-Function")
public Object[][] contentTypes() {
return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}};
}
} }

View File

@ -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<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
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}};
}
}

View File

@ -28,6 +28,10 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.assertEquals;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
@ -36,20 +40,19 @@ import static org.testng.Assert.fail;
public class RestMindmapITCase { public class RestMindmapITCase {
private String userEmail = "admin@wisemapping.com"; 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 @BeforeClass
void createUser() { void createUser() {
final RestAdminITCase restAdminITCase = new RestAdminITCase(); final RestAdminITCase restAdminITCase = new RestAdminITCase();
userEmail = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON); 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 ... public void listMaps(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final String title1 = "List Maps 1 - " + mediaType.toString(); final String title1 = "List Maps 1 - " + mediaType.toString();
@ -79,10 +82,10 @@ public class RestMindmapITCase {
assertTrue(found1 && found2, "Map could not be found"); 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 ... public void deleteMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final String title1 = "Map to delete - " + mediaType.toString(); 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 ... public void changeMapTitle(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final URI resourceUri = addNewMap(requestHeaders, template, "Map to change title - " + mediaType.toString()); final URI resourceUri = addNewMap(requestHeaders, template, "Map to change title - " + mediaType.toString());
@ -118,10 +121,10 @@ public class RestMindmapITCase {
assertEquals(newTitle, map.getTitle()); 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 ... public void validateMapsCreation(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final String title = "Map to Validate Creation - " + mediaType.toString(); 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 ... public void changeMapDescription(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final URI resourceUri = addNewMap(requestHeaders, template, "Map to change Description - " + mediaType.toString()); final URI resourceUri = addNewMap(requestHeaders, template, "Map to change Description - " + mediaType.toString());
@ -164,10 +167,10 @@ public class RestMindmapITCase {
assertEquals(newDescription, map.getDescription()); 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 ... public void updateMapXml(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final String title = "Update XML sample " + mediaType.toString(); final String title = "Update XML sample " + mediaType.toString();
@ -185,10 +188,10 @@ public class RestMindmapITCase {
assertEquals(response.getXml(), newXmlContent); 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 ... public void cloneMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final String title = "Map to clone sample " + mediaType.toString(); 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 ... public void updateMap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
if(MediaType.APPLICATION_XML==mediaType){ if(MediaType.APPLICATION_XML==mediaType){
throw new SkipException("Some research need to check why it;s falling."); throw new SkipException("Some research need to check why it;s falling.");
} }
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = createTemplate(userEmail);
// Create a sample map ... // Create a sample map ...
final String title = "Update sample " + mediaType.toString(); final String title = "Update sample " + mediaType.toString();
@ -265,32 +268,4 @@ public class RestMindmapITCase {
return addNewMap(requestHeaders, template, title, null); return addNewMap(requestHeaders, template, title, null);
} }
private HttpHeaders createHeaders(@NotNull MediaType mediaType) {
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
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}};
}
} }

View File

@ -30,6 +30,7 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.assertEquals;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
@ -37,9 +38,7 @@ import static org.testng.Assert.fail;
@Test @Test
public class RestMindmapLabelITCase { public class RestMindmapLabelITCase {
private String userEmail = "admin@wisemapping.com"; private String userEmail;
private static final String HOST_PORT = "http://localhost:8080";
private static final String BASE_REST_URL = HOST_PORT + "/service";
@BeforeClass @BeforeClass
void createUser() { void createUser() {
@ -48,10 +47,10 @@ public class RestMindmapLabelITCase {
userEmail = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON); 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 ... public void createLabel(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType); final HttpHeaders requestHeaders = RestHelper.createHeaders(mediaType);
final RestTemplate template = createTemplate(); final RestTemplate template = RestHelper.createTemplate( userEmail + ":" + "admin");
// Create a new label // Create a new label
final String title1 = "Label 1 - " + mediaType.toString(); 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 { private URI addNewLabel(@NotNull HttpHeaders requestHeaders, @NotNull RestTemplate template, @NotNull String title) throws IOException, WiseMappingException {
return addNewLabel(requestHeaders, template, title, null); return addNewLabel(requestHeaders, template, title, null);
} }
private HttpHeaders createHeaders(@NotNull MediaType mediaType) {
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
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}};
}
} }