Commit bd6caaab authored by Matija Obreza's avatar Matija Obreza

Merge branch 'extra-endpoints-for-sabset-dataset-accessions' into 'master'

Extra endpoints to load fully JSON with subset/dataset accessions

See merge request genesys-pgr/genesys-server!484
parents 9615eebe c86a5186
......@@ -24,6 +24,7 @@ import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import com.fasterxml.jackson.annotation.JsonIgnore;
......@@ -63,6 +64,15 @@ public class DatasetAccessionRef extends AccessionRef<Dataset> {
this.accession = ref.getAccession();
}
/**
* Instantiates a new dataset accession ref.
*
* @param accession the accession
*/
public DatasetAccessionRef(Accession accession) {
super(accession);
}
public DatasetAccessionRef(String instCode, String acceNumb, String genus, String doi) {
super(instCode, acceNumb, genus, doi);
}
......
......@@ -49,8 +49,11 @@ import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.ElasticsearchService.TermResult;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.spring.CSVMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -114,6 +117,10 @@ public class DatasetController extends ApiBaseController {
@Autowired
protected DescriptorService descriptorService;
/** The accession service. */
@Autowired
private AccessionService accessionService;
/**
* Creates the dataset.
*
......@@ -355,6 +362,22 @@ public class DatasetController extends ApiBaseController {
return datasetService.listAccessions(datasetService.getDataset(uuid, null), page.toPageRequest(100));
}
/**
* Load full accessions list by Dataset
*
* @param uuid uuid of Subset
* @param page Pageable
* @return the page
* @throws NotFoundElement
*/
@JsonView({ JsonViews.Public.class })
@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE }, params = { "full" })
public Page<Accession> listFullAccessions(@PathVariable("uuid") final UUID uuid, final Pagination page) throws NotFoundElement {
AccessionFilter filter = new AccessionFilter();
filter.datasets = Sets.newHashSet(uuid.toString());
return accessionService.list(filter, page.toPageRequest(100));
}
/**
* Removes the descriptors.
*
......
......@@ -26,6 +26,7 @@ import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import com.google.common.collect.Sets;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
......@@ -33,12 +34,15 @@ import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetAccessionRef;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.spring.CSVMessageConverter;
......@@ -90,6 +94,10 @@ public class SubsetController extends ApiBaseController {
@Autowired
protected ShortFilterService shortFilterService;
/** The accession service. */
@Autowired
private AccessionService accessionService;
/**
* Register the subset.
*
......@@ -221,6 +229,22 @@ public class SubsetController extends ApiBaseController {
return subsetService.listAccessions(subsetService.getSubset(uuid), page.toPageRequest(100));
}
/**
* Load full accessions list by Subset
*
* @param uuid uuid of Subset
* @param page Pageable
* @return the page
* @throws NotFoundElement
*/
@JsonView({ JsonViews.Public.class })
@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE }, params = { "full" })
public Page<Accession> listFullAccessions(@PathVariable("uuid") final UUID uuid, final Pagination page) throws NotFoundElement {
AccessionFilter filter = new AccessionFilter();
filter.subsets = Sets.newHashSet(uuid.toString());
return accessionService.list(filter, page.toPageRequest(100));
}
/**
* Add accessions to subset.
*
......
......@@ -23,7 +23,9 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.RandomUtils;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetAccessionRef;
......@@ -40,9 +42,15 @@ import org.genesys.catalog.service.ShortFilterService;
import org.genesys.filerepository.persistence.RepositoryFilePersistence;
import org.genesys.test.config.ApplicationConfig;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.TaxonomyService;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
......@@ -104,6 +112,10 @@ public abstract class AbstractApiTest extends AbstractTest {
protected RepositoryFilePersistence repositoryFilePersistence;
@Autowired
protected DatasetAccessionRefRepository accessionRefRepository;
@Autowired
protected TaxonomyService taxonomyService;
private AtomicInteger acceNumb = new AtomicInteger(1);
protected MockMvc mockMvc;
@Rule
......@@ -242,4 +254,20 @@ public abstract class AbstractApiTest extends AbstractTest {
return accession;
}
protected Accession setUpAccession(FaoInstitute institute) {
Accession a = new Accession();
AccessionId accessionId = new AccessionId();
AccessionGeo geo = new AccessionGeo();
geo.setLatitude(RandomUtils.nextDouble(0d, 45d));
geo.setLongitude(RandomUtils.nextDouble(45d, 90d));
accessionId.setGeo(geo);
a.setAccessionId(accessionId);
a.setInstitute(institute);
a.setAccessionNumber("ACC" + System.currentTimeMillis() + "-" + acceNumb.incrementAndGet());
Taxonomy2 taxon = new Taxonomy2();
taxon.setGenus("Hordeum");
a.setTaxonomy(taxonomyService.ensureTaxonomy(taxon));
return a;
}
}
\ No newline at end of file
......@@ -21,10 +21,14 @@ import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetAccessionRef;
......@@ -35,9 +39,17 @@ import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v1.DatasetController;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.spring.CSVMessageConverter;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -47,6 +59,7 @@ import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.ResultActions;
import com.google.common.collect.Sets;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Andrey Lugovskoy
......@@ -55,6 +68,19 @@ import com.google.common.collect.Sets;
*/
public class DatasetControllerTest extends AbstractApiTest {
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private InstituteService instituteService;
@After
@Override
@Transactional
public void cleanup() throws Exception {
accessionRepository.deleteAll();
super.cleanup();
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void getDataset() throws Exception {
......@@ -741,6 +767,45 @@ public class DatasetControllerTest extends AbstractApiTest {
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void loadMoreAccessionsWithFullJsonTest() throws Exception {
Dataset dataset = datasetService.createDataset(setUpDataset());
assertThat(dataset, notNullValue());
assertThat(dataset.getAccessionCount(), is(0));
FaoInstitute institute = setUpInstitute();
Set<DatasetAccessionRef> newAccessions = setUpAccessions(institute);
newAccessions.addAll(setUpAccessions(institute));
assertThat(newAccessions.size(), is(4));
dataset = datasetService.setAccessionRefs(dataset, newAccessions);
dataset = datasetService.rematchDatasetAccessions(dataset);
assertThat(dataset.getAccessionCount(), is(4));
final int PAGE_SIZE = 1;
for (int page = 0; page < 4; page++) {
/*@formatter:off*/
mockMvc
.perform(get(DatasetController.CONTROLLER_URL.concat("/accessions/{uuid}?full=full&p={page}&l={pageSize}"), dataset.getUuid(), page, PAGE_SIZE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.number", is(page)))
.andExpect(jsonPath("$.size", is(PAGE_SIZE)))
.andExpect(jsonPath("$.totalElements", is(dataset.getAccessionCount())))
.andExpect(jsonPath("$.numberOfElements", is(PAGE_SIZE)))
.andExpect(jsonPath("$.first", is(page == 0)))
.andExpect(jsonPath("$.last", is(page == 3)))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.content[0].geo.id", not(nullValue())))
;
/*@formatter:on*/
}
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void addDescriptorTest() throws Exception {
......@@ -799,4 +864,23 @@ public class DatasetControllerTest extends AbstractApiTest {
return accessionRefs;
}
private FaoInstitute setUpInstitute() {
FaoInstitute institute = new FaoInstitute();
institute.setCode("INS001");
institute.setFullName("An institute");
instituteService.update(Lists.newArrayList(institute));
return instituteService.getInstitute("INS001");
}
private Set<DatasetAccessionRef> setUpAccessions(FaoInstitute institute) {
final List<Accession> accessions = new ArrayList<>(2);
accessions.add(setUpAccession(institute));
accessions.add(setUpAccession(institute));
Set<DatasetAccessionRef> res = accessionRepository.save(accessions).stream().map(a -> new DatasetAccessionRef(a)).collect(Collectors.toSet());
res.stream().map(AccessionRef::getAccession).map(AccessionData::getInstitute).forEach(inst -> inst.setNetworks(null));
res.stream().map(AccessionRef::getAccession).map(AccessionData::getInstitute).forEach(inst -> inst.setSettings(null));
return res;
}
}
......@@ -27,7 +27,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.genesys.catalog.model.Partner;
......@@ -37,9 +36,7 @@ import org.genesys2.server.api.v1.SubsetController;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetAccessionRef;
......@@ -54,7 +51,6 @@ import org.genesys2.server.persistence.SubsetRepository;
import org.genesys2.server.persistence.SubsetVersionsRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.spring.CSVMessageConverter;
import org.junit.After;
import org.junit.Before;
......@@ -108,11 +104,8 @@ public class SubsetRestControllerTest extends AbstractApiTest {
private InstituteService instituteService;
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private TaxonomyService taxonomyService;
private FaoInstitute institute;
private AtomicInteger acceNumb=new AtomicInteger(1);
protected static final ObjectMapper objectMapper;
protected static final ObjectMapper verboseMapper = new ObjectMapper();
......@@ -357,6 +350,43 @@ public class SubsetRestControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
@Test
public void loadMoreSubsetAccessionsWithFullJsonTest() throws Exception {
Subset subset = subsetService.create(institute, setUpSubset());
assertThat(subset, notNullValue());
assertThat(subset.getAccessionCount(), is(0));
Set<SubsetAccessionRef> newAccessions = setUpAccessions();
newAccessions.addAll(setUpAccessions());
assertThat(newAccessions.size(), is(4));
subset = subsetService.addAccessionRefs(subset, newAccessions);
subset = subsetService.rematchSubsetAccessions(subset);
assertThat(subset.getAccessionCount(), is(newAccessions.size()));
final int PAGE_SIZE = 1;
for (int page = 0; page < 4; page++) {
/*@formatter:off*/
mockMvc
.perform(get(SubsetController.CONTROLLER_URL.concat("/accessions/{uuid}?full=full&p={page}&l={pageSize}"), subset.getUuid(), page, PAGE_SIZE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.number", is(page)))
.andExpect(jsonPath("$.size", is(PAGE_SIZE)))
.andExpect(jsonPath("$.totalElements", is(subset.getAccessionCount())))
.andExpect(jsonPath("$.numberOfElements", is(PAGE_SIZE)))
.andExpect(jsonPath("$.first", is(page == 0)))
.andExpect(jsonPath("$.last", is(page == 3)))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.content[0].geo.id", not(nullValue())))
;
/*@formatter:on*/
}
}
@Test
@WithMockOAuth2Authentication(roles = { "ADMINISTRATOR" }, scopes = { "write" })
public void approveSubsetTest() throws Exception {
......@@ -759,23 +789,12 @@ public class SubsetRestControllerTest extends AbstractApiTest {
private Set<SubsetAccessionRef> setUpAccessions() {
final List<Accession> accessions = new ArrayList<>(2);
accessions.add(setUpAccession());
accessions.add(setUpAccession());
accessions.add(setUpAccession(institute));
accessions.add(setUpAccession(institute));
Set<SubsetAccessionRef> res = accessionRepository.save(accessions).stream().map(a -> new SubsetAccessionRef(a)).collect(Collectors.toSet());
res.stream().map(AccessionRef::getAccession).map(AccessionData::getInstitute).forEach(institute -> institute.setNetworks(null));
res.stream().map(AccessionRef::getAccession).map(AccessionData::getInstitute).forEach(institute -> institute.setSettings(null));
return res;
}
private Accession setUpAccession() {
Accession a = new Accession();
a.setAccessionId(new AccessionId());
a.setInstitute(institute);
a.setAccessionNumber("ACC" + System.currentTimeMillis() + "-" + acceNumb.incrementAndGet());
Taxonomy2 taxon = new Taxonomy2();
taxon.setGenus("Hordeum");
a.setTaxonomy(taxonomyService.ensureTaxonomy(taxon));
return a;
}
}
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