Commit c05bdcd9 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Adding/removing partner institutes

- Unit tests
parent 779c0ccd
......@@ -22,6 +22,7 @@ import org.genesys2.server.service.filter.InstituteFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Set;
import java.util.UUID;
/**
......@@ -106,6 +107,11 @@ public interface PartnerService {
*/
Page<Partner> listPartners(PartnerFilter partnerFilter, Pageable page);
Partner addInstitutes(Partner partner, Set<String> instituteCodes);
boolean removeInstitutes(Set<String> instituteCodes);
/**
* Method for copy values of source to target.
*
......
......@@ -18,6 +18,8 @@ package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.QPartner.partner;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.UUID;
......@@ -171,6 +173,28 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
@Override
@Transactional
public Partner addInstitutes(Partner partner, Set<String> instituteCodes) {
List<FaoInstitute> institutes = instituteRepository.findAllByCodes(instituteCodes);
institutes.forEach((inst) -> inst.setOwner(partner));
partner.getInstitutes().addAll(institutes);
return lazyLoad(partnerRepository.save(partner));
}
@Override
@Transactional
public boolean removeInstitutes(Set<String> instituteCodes) {
List<FaoInstitute> institutesToRemove = instituteRepository.findAllByCodes(instituteCodes);
institutesToRemove.forEach((institute -> institute.setOwner(null)));
instituteRepository.save(institutesToRemove);
return true;
}
/**
* {@inheritDoc}
*/
......
......@@ -17,6 +17,7 @@
package org.genesys2.server.api.v1;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import io.swagger.annotations.Api;
......@@ -111,6 +112,19 @@ public class PartnerController {
return partnerService.updatePartner(source);
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@PostMapping(value = "/addInstitutes/{UUID}")
public Partner addInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
Partner partner = partnerService.loadPartner(uuid);
return partnerService.addInstitutes(partner, instituteCodes);
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@PostMapping(value = "/removeInstitutes")
public String removeInstitutes(@RequestBody Set<String> instituteCodes) {
return partnerService.removeInstitutes(instituteCodes) ? "Ok" : "Error";
}
/**
* List partners.
*
......
......@@ -25,6 +25,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import org.genesys.blocks.model.filters.StringFilter;
......@@ -36,6 +37,7 @@ import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -77,8 +79,8 @@ public class PartnerControllerTest extends AbstractApiTest {
@After
@Override
public void cleanup() throws Exception {
partnerRepository.deleteAll();
instituteRepository.deleteAll();
partnerRepository.deleteAll();
super.cleanup();
}
......@@ -274,6 +276,86 @@ public class PartnerControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
@Test
public void listPartnerInstitutesTest() throws Exception {
final Partner partner = partnerRepository.save(setPartner("Partner one", true, "Partner one", UUID.randomUUID()));
List<FaoInstitute> instituteList = Lists.newArrayList(setupInstitute("PAR001"), setupInstitute("PAR002"));
instituteList.forEach((institute -> institute.setOwner(partner)));
instituteRepository.save(instituteList);
/*@formatter:off*/
this.mockMvc
.perform(RestDocumentationRequestBuilders.post(PartnerController.CONTROLLER_URL.concat("/{UUID}/institutes"), partner.getUuid())
.content("{}").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.content", not(nullValue())))
.andExpect(jsonPath("$.content", hasSize(2)))
.andExpect(jsonPath("$.content[0].code", isOneOf(instituteList.stream().map(FaoInstitute::getCode).toArray())))
.andExpect(jsonPath("$.content[0].owner.uuid", is(partner.getUuid().toString())))
.andExpect(jsonPath("$.content[1].code", isOneOf(instituteList.stream().map(FaoInstitute::getCode).toArray())))
.andExpect(jsonPath("$.content[1].owner", is(partner.getUuid().toString())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void testAddInstituteToPartner() throws Exception {
final Partner partner = partnerRepository.save(setPartner("Partner one", true, "Partner one", UUID.randomUUID()));
List<FaoInstitute> instituteList = instituteRepository.save(Lists.newArrayList(setupInstitute("PAR001"), setupInstitute("PAR002")));
Assert.assertNull(instituteList.get(0).getOwner());
Assert.assertNull(instituteList.get(1).getOwner());
this.mockMvc
.perform(RestDocumentationRequestBuilders.post(PartnerController.CONTROLLER_URL.concat("/addInstitutes/{UUID}"), partner.getUuid())
.content(verboseMapper.writeValueAsString(instituteList.stream().map(FaoInstitute::getCode).collect(Collectors.toList())))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(partner.getUuid().toString())));
instituteList = instituteRepository.findAll();
Assert.assertEquals(instituteList.get(0).getOwner(), partner);
Assert.assertEquals(instituteList.get(1).getOwner(), partner);
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void testRemoveInstituteFromPartner() throws Exception {
final Partner partner = partnerRepository.save(setPartner("Partner one", true, "Partner one", UUID.randomUUID()));
List<FaoInstitute> instituteList = Lists.newArrayList(setupInstitute("PAR001"), setupInstitute("PAR002"));
instituteList.forEach((institute -> institute.setOwner(partner)));
instituteRepository.save(instituteList);
Assert.assertEquals(instituteList.get(0).getOwner(), partner);
Assert.assertEquals(instituteList.get(1).getOwner(), partner);
String[] instCodesToRemove = { instituteList.get(0).getCode() };
this.mockMvc
.perform(RestDocumentationRequestBuilders.post(PartnerController.CONTROLLER_URL.concat("/removeInstitutes"))
.content(verboseMapper.writeValueAsString(instCodesToRemove))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())));
instituteList = instituteRepository.findAll();
Assert.assertNull(instituteList.get(0).getOwner());
Assert.assertEquals(instituteList.get(1).getOwner(), partner);
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void createPartnerTest() throws Exception {
......@@ -321,8 +403,6 @@ public class PartnerControllerTest extends AbstractApiTest {
partner.setActive(isActive);
partner.setShortName(shortName);
partner.setUuid(uuid);
List<FaoInstitute> instituteList = Lists.newArrayList(setupInstitute("PAR001"), setupInstitute("PAR002"));
partner.setInstitutes(instituteList);
partner.setUrls(Sets.newHashSet(new URL("https://localhost"), new URL("http://localhost:8080")));
return partner;
}
......
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