Commit b3d99b77 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza

REST tests first UserController tested

parent c5c69fdf
......@@ -467,7 +467,7 @@
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>transifex-client</artifactId>
<version>0.4-SNAPSHOT</version>
<version>0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
......@@ -477,7 +477,7 @@
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>pgr-ontology-model</artifactId>
<version>0.7.1-SNAPSHOT</version>
<version>0.7.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
......@@ -485,6 +485,12 @@
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
package org.genesys2.resttests;
import org.apache.velocity.app.VelocityEngine;
import org.genesys2.server.aspect.AsAdminAspect;
import org.genesys2.server.persistence.acl.AclClassPersistence;
import org.genesys2.server.persistence.acl.AclEntryPersistence;
import org.genesys2.server.persistence.acl.AclObjectIdentityPersistence;
import org.genesys2.server.persistence.acl.AclSidPersistence;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepositoryCustomImpl;
import org.genesys2.server.persistence.domain.TeamRepository;
import org.genesys2.server.persistence.domain.UserPersistence;
import org.genesys2.server.service.*;
import org.genesys2.server.service.impl.*;
import org.genesys2.server.servlet.controller.rest.UserController;
import org.genesys2.server.test.JpaDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AbstractRestTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@WebAppConfiguration
public abstract class AbstractRestTest {
@Configuration
@EnableWebMvc
@Import({ JpaDataConfig.class, HazelcastConfig.class })
@PropertySource({ "classpath:application.properties", "classpath:spring/spring.properties" })
public static class Config {
@Bean
public UserController userController(){
return new UserController();
}
@Bean
public CacheManager cacheManager(){
return new NoOpCacheManager();
}
@Bean
public UserService userService(){
return new UserServiceImpl();
}
@Bean
public TeamService teamService(){
return new TeamServiceImpl();
}
@Bean
public InstituteService instituteService(){
return new InstituteServiceImpl();
}
@Bean
public ContentService contentService(){
return new ContentServiceImpl();
}
@Bean
public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer();
}
@Bean
public VelocityEngine velocityEngine() {
return new VelocityEngine();
}
@Bean
public GenesysLowlevelRepository genesysLowlevelRepository() {
return new GenesysLowlevelRepositoryCustomImpl();
}
@Bean
public AclService aclService() {
return new AclServiceImpl();
}
@Bean
public AsAdminAspect asAdminAspect() {
return new AsAdminAspect();
}
}
@Autowired
UserService userService;
@Autowired
UserController userController;
@Autowired
UserPersistence userPersistence;
@Autowired
InstituteService instituteService;
@Autowired
TeamRepository teamRepository;
@Autowired
AclService aclService;
@Autowired
AclSidPersistence aclSidPersistence;
@Autowired
AclEntryPersistence aclEntryPersistence;
@Autowired
AclObjectIdentityPersistence aclObjectIdentityPersistence;
@Autowired
AclClassPersistence aclClassPersistence;
@Autowired
TeamService teamService;
}
package org.genesys2.resttests;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.exception.UserException;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Team;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.servlet.controller.rest.UserController;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class UserControllerTest extends AbstractRestTest {
private static final Log LOG = LogFactory.getLog(UserControllerTest.class);
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
User user = new User();
FaoInstitute faoInstitute;
Team team;
List<FaoInstitute> institutes = new ArrayList<>();
@Before
public void setUp() throws UserException {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
faoInstitute = new FaoInstitute();
faoInstitute.setFullName("This is name of institute");
faoInstitute.setCurrent(true);
faoInstitute.setPgrActivity(true);
faoInstitute.setMaintainsCollection(true);
faoInstitute.setPgrActivity(true);
faoInstitute.setAccessionCount(1);
faoInstitute.setUniqueAcceNumbs(true);
faoInstitute.setCode("Code");
user = new User();
user.setEmail("salexandrbasov@gmail.com");
user.setPassword("Alexandr19011990");
user.setName("SYS_ADMIN");
team = new Team();
team.setName("name of team");
team.setVersion(1);
userService.addUser(user);
institutes.add(faoInstitute);
instituteService.update(institutes);
Map<Integer, Boolean> permission = new HashMap<>();
permission.put(4, true);
permission.put(1, false);
permission.put(2, false);
permission.put(8, false);
permission.put(16, true);
aclService.addPermissions(faoInstitute.getId(), FaoInstitute.class.getName(), "SYS_ADMIN", true, permission);
List<GrantedAuthority> authorities = new ArrayList<>();
GrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(UserRole.ADMINISTRATOR.getName());
authorities.add(simpleGrantedAuthority);
AuthUserDetails authUserDetails = new AuthUserDetails(user.getName(), user.getPassword(), authorities);
// set actual DB user
authUserDetails.setUser(user);
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(authUserDetails, authUserDetails, authorities);
SecurityContextHolder.getContext().setAuthentication(authToken);
teamService.addTeam(team.getName());
}
@After
public void tearDown() {
teamRepository.deleteAll();
userPersistence.deleteAll();
instituteService.delete(faoInstitute.getCode());
aclObjectIdentityPersistence.deleteAll();
aclClassPersistence.deleteAll();
aclSidPersistence.deleteAll();
aclEntryPersistence.deleteAll();
}
@Test
public void getProfileTest() throws Exception {
LOG.info("Start test-method getProfileTest");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(get("/api/v0/me").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(user)));
LOG.info("Test getProfileTest passed!");
}
@Test
public void listInstitutesTest() throws Exception {
LOG.info("Start test-method listInstitutesTest");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(get("/api/v0/me/institutes").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(institutes)));
LOG.info("Test listInstitutesTest passed!");
}
@Test
public void listTeamsTest() throws Exception {
LOG.info("Start test-method listTeamsTest");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(get("/api/v0/me/teams").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(teamRepository.findAll())));
LOG.info("Test listTeamsTest passed");
}
@Test
public void createTeamTest() throws Exception {
LOG.info("Start test-method createTeamTest");
teamRepository.deleteAll();
ObjectMapper objectMapper = new ObjectMapper();
UserController.TeamJson teamJson = new UserController.TeamJson();
teamJson.name = "name";
mockMvc.perform(post("/api/v0/me/teams")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(teamJson)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("name").value("name"))
.andExpect(content().string(objectMapper.writeValueAsString(teamRepository.findAll().get(0))));
LOG.info("Test createTeamTest passed");
}
@Test
public void changeLockTest() throws Exception {
LOG.info("Start test-method changeLockTest");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(post("/api/v0/user/{uuid}/locked", user.getUuid())
.contentType(MediaType.APPLICATION_JSON)
.content(Boolean.TRUE.toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(Boolean.TRUE)));
LOG.info("Test changeLockTest passed");
}
@Test
public void changeEnabled() throws Exception {
LOG.info("Start test-method changeEnabled");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(post("/api/v0/user/{uuid}/enabled", user.getUuid())
.contentType(MediaType.APPLICATION_JSON)
.content(Boolean.TRUE.toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(Boolean.TRUE)));
LOG.info("Test changeEnabled passed");
}
@Test
public void leaveTeamTest() throws Exception {
LOG.info("Start test-method leaveTeamTest");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode JSON_OK = objectMapper.readTree("{\"result\":true}");
mockMvc.perform(post("/api/v0/me/teams/{teamId}/leave", teamRepository.findAll().get(0).getId())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(JSON_OK.toString()));
LOG.info("Test leaveTeamTest passed");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment