Commit 7123a944 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

OrganizationControllerTest completed

parent 29c33e57
......@@ -16,17 +16,9 @@
package org.genesys2.server.servlet.controller.rest;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.xml.bind.ValidationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -39,14 +31,14 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import javax.xml.bind.ValidationException;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@Controller("restOrganizationController")
@PreAuthorize("isAuthenticated()")
......@@ -61,9 +53,9 @@ public class OrganizationController extends RestController {
/**
* List organizations
*
*
* @return
*
*
* @return
* @throws AuthorizationException
*/
......@@ -75,7 +67,7 @@ public class OrganizationController extends RestController {
/**
* Create or update organization
*
*
* @return
* @throws ValidationException
*/
......@@ -98,7 +90,7 @@ public class OrganizationController extends RestController {
/**
* Get organization details
*
*
* @return
* @throws AuthorizationException
*/
......@@ -110,7 +102,7 @@ public class OrganizationController extends RestController {
/**
* Get organization blurp
*
*
* @return
* @throws AuthorizationException
*/
......@@ -124,7 +116,7 @@ public class OrganizationController extends RestController {
/**
* Update blurp
*
*
* @return
* @throws AuthorizationException
*/
......@@ -137,7 +129,7 @@ public class OrganizationController extends RestController {
/**
* Get organization details
*
*
* @return
* @throws AuthorizationException
*/
......@@ -149,7 +141,7 @@ public class OrganizationController extends RestController {
/**
* Get organization details
*
*
* @return
* @throws AuthorizationException
*/
......@@ -165,7 +157,7 @@ public class OrganizationController extends RestController {
/**
* Add Institutes to Organization
*
*
* @return
* @throws IOException
* @throws JsonProcessingException
......@@ -185,7 +177,7 @@ public class OrganizationController extends RestController {
/**
* Set Institutes of Organization
*
*
* @return
* @throws IOException
* @throws JsonProcessingException
......@@ -205,10 +197,10 @@ public class OrganizationController extends RestController {
public static class OrganizationBlurpJson {
public String blurp;
public String language;
public String locale;
public Locale getLocale() {
return language == null ? null : new Locale(language);
return locale == null ? null : new Locale(locale);
}
}
}
......@@ -37,7 +37,6 @@ public class UserSerializer extends JsonSerializer<User> {
jgen.writeObjectField("uuid", user.getUuid());
jgen.writeObjectField("name", user.getName());
jgen.writeObjectField("email", user.getEmail());
jgen.writeObjectField("password", user.getPassword());
final Set<UserRole> roles = user.getRoles();
if (roles != null) {
jgen.writeArrayFieldStart("roles");
......
......@@ -3,7 +3,10 @@ package org.genesys2.tests.resttests;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.server.servlet.controller.rest.OrganizationController;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -12,10 +15,13 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -29,18 +35,48 @@ public class OrganizationControllerTest extends AbstractRestTest {
MockMvc mockMvc;
private Organization organization;
private Article article;
private FaoInstitute faoInstitute;
private List<FaoInstitute> institutes = new ArrayList<>();
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
organization = organizationService.create("slugOrganization", "TitleOrganization");
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");
institutes.add(faoInstitute);
instituteService.update(institutes);
organization.setMembers(institutes);
article = new Article();
article.setSlug("blurp");
article.setLang("en");
article.setBody("Body");
article.setTargetId(2L);
article.setClassPk(contentService.ensureClassPK(Organization.class));
List<Article> articleList = new ArrayList<>();
articleList.add(article);
contentService.save(articleList);
}
@After
public void tearDown() {
organizationRepository.deleteAll();
articleRepository.deleteAll();
instituteRepository.deleteAll();
}
@Test
......@@ -75,4 +111,150 @@ public class OrganizationControllerTest extends AbstractRestTest {
LOG.info("Test updateOrganizationTest is passed");
}
@Test
public void getOrganizationTest() throws Exception {
LOG.info("Start test-method getOrganizationTest");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(get("/api/v0/org/{shortName}", organization.getSlug())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(objectMapper.writeValueAsString(organization)));
LOG.info("Test getOrganizationTest is passed");
}
@Test
public void getBlurpTest() throws Exception {
LOG.info("Start test-method getBlurpTest");
mockMvc.perform(get("/api/v0/org/{shortName}/blurp/{language}", organization.getSlug(), article.getLang())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(article.getBody()));
LOG.info("Test getBlurpTest is passed");
}
@Test
public void updateBlurpTest() throws Exception {
LOG.info("Start test-method updateBlurpTest");
ObjectMapper objectMapper = new ObjectMapper();
OrganizationController.OrganizationBlurpJson organizationBlurpJson = new OrganizationController.OrganizationBlurpJson();
organizationBlurpJson.blurp = "newBlurp";
organizationBlurpJson.locale = "en";
mockMvc.perform(put("/api/v0/org/{shortName}/blurp", organization.getSlug())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(organizationBlurpJson)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(organizationService.updateBlurp(organization, organizationBlurpJson.blurp, organizationBlurpJson.getLocale()))));
LOG.info("test updateBlurpTest is passed");
}
@Test
public void deleteOrganizationTest() throws Exception {
LOG.info("Start test-method deleteOrganizationTest");
ObjectMapper objectMapper = new ObjectMapper();
organization.setId(null);
mockMvc.perform(delete("/api/v0/org/{shortName}", organization.getSlug())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(organization)));
LOG.info("test deleteOrganizationTest is passed");
}
@Test
public void getOrganizationMembersTest() throws Exception {
LOG.info("Start test-method getOrganizationMembersTest");
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(get("/api/v0/org/{shortName}/institutes", organization.getSlug())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(organizationService.getMembers(organizationService.getOrganization(organization.getSlug())))));
LOG.info("test getOrganizationMembersTest is passed");
}
@Test
@Transactional
public void addOrganizationInstitutesTest() throws Exception {
LOG.info("Start test-method addOrganizationInstitutesTest");
ObjectMapper objectMapper = new ObjectMapper();
List<String> instituteList = new ArrayList<>();
FaoInstitute secondFaoInstitute = new FaoInstitute();
secondFaoInstitute.setFullName("This is name of second institute");
secondFaoInstitute.setCurrent(true);
secondFaoInstitute.setPgrActivity(true);
secondFaoInstitute.setMaintainsCollection(true);
secondFaoInstitute.setPgrActivity(true);
secondFaoInstitute.setAccessionCount(1);
secondFaoInstitute.setUniqueAcceNumbs(true);
secondFaoInstitute.setCode("SecondCode");
List<FaoInstitute> faoInstituteList = new ArrayList<>();
faoInstituteList.add(secondFaoInstitute);
instituteService.update(faoInstituteList);
instituteList.add(faoInstitute.getCode());
instituteList.add(secondFaoInstitute.getCode());
mockMvc.perform(post("/api/v0/org/{slug}/add-institutes", organization.getSlug())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(instituteList)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(true)));
LOG.info("test addOrganizationInstitutesTest is passed");
}
@Test
@Transactional
public void setOrganizationInstitutesTest() throws Exception {
LOG.info("Start test-method setOrganizationInstitutesTest");
ObjectMapper objectMapper = new ObjectMapper();
List<String> instituteList = new ArrayList<>();
FaoInstitute thirdFaoInstitute = new FaoInstitute();
thirdFaoInstitute.setFullName("This is name of second institute");
thirdFaoInstitute.setCurrent(true);
thirdFaoInstitute.setPgrActivity(true);
thirdFaoInstitute.setMaintainsCollection(true);
thirdFaoInstitute.setPgrActivity(true);
thirdFaoInstitute.setAccessionCount(1);
thirdFaoInstitute.setUniqueAcceNumbs(true);
thirdFaoInstitute.setCode("SecondCode");
List<FaoInstitute> faoInstituteList = new ArrayList<>();
faoInstituteList.add(thirdFaoInstitute);
instituteService.update(faoInstituteList);
instituteList.add(faoInstitute.getCode());
instituteList.add(thirdFaoInstitute.getCode());
mockMvc.perform(post("/api/v0/org/{slug}/set-institutes", organization.getSlug())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(instituteList)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(true)));
LOG.info("test setOrganizationInstitutesTest is passed");
}
}
......@@ -150,8 +150,10 @@ public class EmailVerificationServiceTest extends AbstractServicesTest {
LOG.info("Start test-method cancelValidationTest");
assertTrue(!verificationTokenRepository.findAll().isEmpty());
int size1 = verificationTokenRepository.findAll().size();
emailVerificationService.cancelValidation(verificationTokenRepository.findAll().get(0).getUuid());
assertTrue(verificationTokenRepository.findAll().isEmpty());
int size2 = verificationTokenRepository.findAll().size();
assertTrue(size1 != size2);
LOG.info("Test cancelValidationTest passed!");
}
......
......@@ -47,7 +47,7 @@ public class TokenVerificationServiceTest extends AbstractServicesTest {
public void teardown() {
if (isAnyTokensExists()) {
tokenRepository.delete(tokenRepository.findAll().get(0).getId());
tokenRepository.deleteAll();
}
}
......@@ -58,7 +58,7 @@ public class TokenVerificationServiceTest extends AbstractServicesTest {
@Test
public void generateTokenTest() {
LOG.info("Start test-method generateTokenTest");
tokenRepository.deleteAll();
assertTrue(!isAnyTokensExists());
VerificationToken token = tokenVerificationService.generateToken(tokenPurpose, data);
......
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