Commit 22f30bb1 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Institute editing

- added API endpoint for updating of institute data
- unit test
parent 35949951
......@@ -112,7 +112,13 @@ public class InstituteController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, instituteService.list(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
}
@JsonView({ JsonViews.Protected.class })
@GetMapping(value ="/{code:[A-Z]+[0-9]+}", produces = MediaType.APPLICATION_JSON_VALUE)
public FaoInstitute get(@PathVariable(value = "code") String code){
return instituteService.findInstitute(code);
}
@GetMapping(value ="/{code:[A-Z]+[0-9]+}/details", produces = MediaType.APPLICATION_JSON_VALUE)
public InstituteDetails details(@PathVariable(value = "code") String code) {
FaoInstitute faoInstitute = instituteService.getInstitute(code);
......@@ -136,6 +142,13 @@ public class InstituteController {
return details;
}
@JsonView({JsonViews.Protected.class})
@PostMapping(value = "/{code:[A-Z]+[0-9]+}/update")
public FaoInstitute update(@PathVariable(value = "code") String code, @RequestBody FaoInstitute institute) {
return instituteService.update(code, institute);
}
private Map<String, ElasticsearchService.TermResult> getOverviewData(AccessionFilter byInstituteFilter) {
String[] terms = new String[] {"crop.shortName", "cropName", "taxonomy.genus", "taxonomy.genusSpecies"};
......
......@@ -22,7 +22,6 @@ import java.util.Locale;
import java.util.Map;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.filter.InstituteFilter;
......@@ -51,6 +50,7 @@ public interface InstituteService {
Page<FaoInstitute> listActive(Pageable pageable);
FaoInstitute update(String code, FaoInstitute institute);
List<FaoInstitute> update(Collection<FaoInstitute> institutes);
void updateAbout(FaoInstitute faoInstitute, String body, String summary, Locale locale) throws CRMException;
......
......@@ -97,6 +97,18 @@ public class InstituteServiceImpl implements InstituteService {
return instituteRepository.listAllActive(pageable);
}
@Override
@Transactional
public FaoInstitute update(String code, FaoInstitute institute) {
FaoInstitute target = findInstitute(code);
copyValues(target, institute);
instituteSettingRepository.save(target.getSettings().values());
return instituteRepository.save(target);
}
@Override
public Page<FaoInstitute> listPGRInstitutes(Pageable pageable) {
return instituteRepository.listPGRInstitutes(pageable);
......@@ -330,4 +342,15 @@ public class InstituteServiceImpl implements InstituteService {
Page<FaoInstitute> res = instituteRepository.findAll(page);
return res.map(inst -> toVocabularyTerm(inst));
}
private void copyValues(FaoInstitute target, FaoInstitute source) {
target.setAllowMaterialRequests(source.isAllowMaterialRequests());
target.setUniqueAcceNumbs(source.isUniqueAcceNumbs());
target.setCodeSGSV(source.getCodeSGSV());
if(source.getSettings() != null) {
target.getSettings().putAll(source.getSettings());
}
}
}
package org.genesys.test.server.api.v1;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v1.InstituteController;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.FaoInstituteSetting;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.FaoInstituteSettingRepository;
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.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class InstituteControllerTest extends AbstractApiTest {
@Autowired
private FaoInstituteRepository instituteRepository;
@Autowired
private FaoInstituteSettingRepository instituteSettingRepository;
private static FaoInstitute institute;
@Transactional
@Before
@Override
public void beforeTest() throws Exception {
super.beforeTest();
institute = setupInstitute("XXX01", "Test fullInstitute", "TI", "test@example.com", "governmental");
}
@Transactional
@After
@Override
public void cleanup() throws Exception {
instituteSettingRepository.deleteAll();
instituteRepository.deleteAll();
super.cleanup();
}
@Test
public void testUpdateInstitute() throws Exception{
Map<String, FaoInstituteSetting> instituteSettings = new HashMap<>();
FaoInstituteSetting gaTracker = new FaoInstituteSetting(institute);
gaTracker.setSetting("googleAnalytics.tracker");
gaTracker.setValue("test ga");
instituteSettings.put("googleAnalytics.tracker", gaTracker);
FaoInstituteSetting mailTo = new FaoInstituteSetting(institute);
mailTo.setSetting("requests.mailto");
mailTo.setValue("test mail");
instituteSettings.put("requests.mailto", mailTo);
institute.setSettings(instituteSettings);
mockMvc.perform(post(InstituteController.API_BASE.concat("/").concat(institute.getCode()).concat("/update"))
.content(objectMapper.writeValueAsString(institute))
.contentType(MediaType.APPLICATION_JSON_UTF8))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.code", is(institute.getCode())))
.andExpect(jsonPath("$.settings", is(notNullValue())))
.andExpect(jsonPath("$.settings", hasKey("requests.mailto")))
.andExpect(jsonPath("$.settings", hasKey("googleAnalytics.tracker")));
}
private FaoInstitute setupInstitute(String instCode, String fullName, String acronym, String email, String type) {
FaoInstitute institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName(fullName);
institute.setAcronym(acronym);
institute.setEmail(email);
institute.setType(type);
return instituteRepository.save(institute);
}
}
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