- Add REST Integration tests.

This commit is contained in:
Paulo Gustavo Veiga 2012-03-12 10:48:54 -03:00
parent 2d38b5d990
commit 1aebcf48e3
7 changed files with 396 additions and 280 deletions

View File

@ -267,7 +267,6 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@ -418,9 +417,6 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>true</parallel>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
@ -432,7 +428,7 @@
<stopPort>9999</stopPort> <stopPort>9999</stopPort>
<war>${project.build.directory}/wisemapping.war</war> <war>${project.build.directory}/wisemapping.war</war>
<reload>automatic</reload> <reload>automatic</reload>
<scanIntervalSeconds>10</scanIntervalSeconds> <!--<scanIntervalSeconds>10</scanIntervalSeconds>-->
</configuration> </configuration>
<executions> <executions>
<execution> <execution>

View File

@ -204,7 +204,7 @@ public class MindmapIcons {
public static final MindmapIcon PEOPLE_FEMALE = new MindmapIcon(IconFamily.PEOPLE, "female1"); public static final MindmapIcon PEOPLE_FEMALE = new MindmapIcon(IconFamily.PEOPLE, "female1");
public static final MindmapIcon PEOPLE_FEMALE2 = new MindmapIcon(IconFamily.PEOPLE, "female2"); public static final MindmapIcon PEOPLE_FEMALE2 = new MindmapIcon(IconFamily.PEOPLE, "female2");
@NotNull
public static List<MindmapIcon> getIconByFamily(@NotNull IconFamily family) { public static List<MindmapIcon> getIconByFamily(@NotNull IconFamily family) {
load(); load();

View File

@ -110,7 +110,7 @@ public class User
if (!getEmail().equals(user.getEmail())) return false; if (!getEmail().equals(user.getEmail())) return false;
if (firstname != null ? !firstname.equals(user.firstname) : user.firstname != null) return false; if (firstname != null ? !firstname.equals(user.firstname) : user.firstname != null) return false;
if (lastname != null ? !lastname.equals(user.lastname) : user.lastname != null) return false; if (lastname != null ? !lastname.equals(user.lastname) : user.lastname != null) return false;
if (password != null ? !password.equals(user.password) : user.password != null) return false; if (username != null ? !username.equals(user.username) : user.username != null) return false;
return true; return true;
} }

View File

@ -11,11 +11,11 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
@Controller @Controller
public class AdminController extends BaseController { public class AdminController extends BaseController {
private static final String RESPONSE_VIEW = "responseView";
@Autowired @Autowired
private UserService userService; private UserService userService;
@ -40,7 +40,8 @@ public class AdminController extends BaseController {
} }
@RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"}) @RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
public ModelAndView getUserByEmail(@RequestBody RestUser user) throws IOException, WiseMappingException { @ResponseStatus(value = HttpStatus.CREATED)
public void getUserByEmail(@RequestBody RestUser user, HttpServletResponse response) throws IOException, WiseMappingException {
if (user == null) { if (user == null) {
throw new IllegalArgumentException("User could not be found"); throw new IllegalArgumentException("User could not be found");
} }
@ -51,8 +52,24 @@ public class AdminController extends BaseController {
throw new IllegalArgumentException("User already exists with this email."); throw new IllegalArgumentException("User already exists with this email.");
} }
userService.createUser(user.getDelegated(), false); final User delegated = user.getDelegated();
return new ModelAndView(RESPONSE_VIEW, "message", "User '" + user.getId() + "' created successfully"); final String lastname = delegated.getLastname();
if (lastname == null || lastname.isEmpty()) {
throw new IllegalArgumentException("lastname can not be null");
}
final String firstName = delegated.getFirstname();
if (firstName == null || firstName.isEmpty()) {
throw new IllegalArgumentException("firstname can not be null");
}
final String username = delegated.getUsername();
if (username == null || username.isEmpty()) {
throw new IllegalArgumentException("username can not be null");
}
userService.createUser(delegated, false);
response.setHeader("Location","/service/admin/users/" + user.getId());
} }
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"}) @RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})

View File

@ -1,7 +1,6 @@
package com.wisemapping.rest.model; package com.wisemapping.rest.model;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.User; import com.wisemapping.model.User;
import org.codehaus.jackson.annotate.JsonAutoDetect; import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonIgnore;
@ -10,9 +9,7 @@ import org.jetbrains.annotations.NotNull;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
import java.util.Set; import java.util.Set;
@XmlRootElement(name = "user") @XmlRootElement(name = "user")
@ -22,6 +19,7 @@ import java.util.Set;
public class RestUser { public class RestUser {
private User user; private User user;
private String password;
public RestUser() { public RestUser() {
this(new User()); this(new User());
@ -59,9 +57,9 @@ public class RestUser {
user.setLastname(lastname); user.setLastname(lastname);
} }
public boolean isActive() { // public boolean isActive() {
return user.isActive(); // return user.isActive();
} // }
public String getUsername() { public String getUsername() {
return user.getUsername(); return user.getUsername();
@ -91,12 +89,34 @@ public class RestUser {
// user.setCreationDate(creationDate); // user.setCreationDate(creationDate);
} }
public void setPassword(@NotNull final String password){ public void setPassword(final String password) {
this.user.setPassword(password); this.user.setPassword(password);
this.password = password;
}
public String getPassword() {
return this.password;
} }
@JsonIgnore @JsonIgnore
public User getDelegated() { public User getDelegated() {
return this.user; return this.user;
} }
@Override
public boolean equals(Object o) {
if (!(o instanceof RestUser)) {
return false;
}
RestUser restUser = (RestUser) o;
return this.getDelegated().equals(restUser.getDelegated());
}
@Override
public int hashCode() {
return this.getDelegated().hashCode();
}
} }

View File

@ -54,7 +54,9 @@
</property> </property>
<property name="defaultViews"> <property name="defaultViews">
<list> <list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="extractValueFromSingleKeyModel" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller"/> <constructor-arg ref="jaxbMarshaller"/>
</bean> </bean>

View File

@ -1,17 +1,98 @@
package com.wisemapping.test.rest; 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.*;
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 org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
@Test @Test
public class RestAdminITCase { public class RestAdminITCase {
@Test @NonNls
public void createNewUser() { private static final String HOST_PORT = "http://localhost:8080/";
String uri = "http://localhost:9000/service/admin/user"; private static final String BASE_REST_URL = HOST_PORT + "service";
// RestTemplate template = new RestTemplate(); @Test(dataProvider = "ContentType-Provider-Function")
// location = template.postForLocation(uri); public void findUser(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate();
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
final ResponseEntity<RestUser> result = templateRest.exchange(BASE_REST_URL + "/admin/users/2", HttpMethod.GET, findUserEntity, RestUser.class);
}
@Test(dataProvider = "ContentType-Provider-Function")
public void createNewUser(final @NotNull MediaType mediaType) {
// Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
// Configure media ...
final RestTemplate templateRest = createTemplate();
// Fill user data ...
final RestUser restUser = new RestUser();
restUser.setEmail("foo" + System.nanoTime() + "@example.org");
restUser.setUsername("foo");
restUser.setFirstname("foo first name");
restUser.setLastname("foo last name");
restUser.setPassword("foo password");
// Post request ...
HttpEntity<RestUser> createUserEntity = new HttpEntity<RestUser>(restUser, requestHeaders);
URI location = templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity);
System.out.println("location:" + location);
// Check that the user has been created ...
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
final String url = "http://localhost:8080" + location;
final ResponseEntity<RestUser> result = templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class);
assertEquals(result.getBody(), restUser, "Returned object object seems not be the same.");
}
private HttpHeaders createHeaders(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 authorisation = "admin@wisemapping.org" + ":" + "admin";
byte[] encodedAuthorisation = Base64.encode(authorisation.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}};
} }
} }