Commit 0d2accb1 authored by Alexander Prendetskiy's avatar Alexander Prendetskiy Committed by Matija Obreza

Merge catalog tests

parent 65e83758
......@@ -268,6 +268,13 @@
<version>${spring-data-jpa.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>${spring.security.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
......
......@@ -61,7 +61,7 @@ import io.swagger.annotations.Api;
public class DatasetController {
// Rest controller base URL
protected static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/dataset";
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/dataset";
private static final Logger LOG = LoggerFactory.getLogger(DatasetController.class);
......
......@@ -49,7 +49,7 @@ import java.util.UUID;
public class DatasetCreatorController {
/** The Constant API_BASE. */
protected static final String CONTROLLER_URL = DatasetController.CONTROLLER_URL + "/{UUID}/datasetcreator";
public static final String CONTROLLER_URL = DatasetController.CONTROLLER_URL + "/{UUID}/datasetcreator";
/** The dataset service. */
@Autowired
......
......@@ -41,7 +41,7 @@ import java.util.Map;
public class DescriptorListExporter {
@Autowired
private FreemarkerTemplating freemarker;
private FreemarkerTemplating freemarkerTemplating;
/** The Constant OPTIONS. */
/*@formatter:off*/
......@@ -75,7 +75,7 @@ public class DescriptorListExporter {
public String markdownDescriptorList(final DescriptorList descriptorList) throws FreemarkerException {
final Map<String, Object> root = new HashMap<>();
root.put("descriptorList", descriptorList);
return freemarker.processTemplateResource("descriptorlist/booklet.ftl", root);
return freemarkerTemplating.processTemplateResource("descriptorlist/booklet.ftl", root);
}
/**
......@@ -88,7 +88,7 @@ public class DescriptorListExporter {
public String htmlDescriptorList(final DescriptorList descriptorList) throws FreemarkerException {
final Map<String, Object> root = new HashMap<>();
root.put("descriptorList", descriptorList);
final String markdown = freemarker.processTemplateResource("descriptorlist/booklet.ftl", root);
final String markdown = freemarkerTemplating.processTemplateResource("descriptorlist/booklet.ftl", root);
// System.err.println(markdown);
final Parser parser = Parser.builder(OPTIONS).build();
......@@ -103,7 +103,7 @@ public class DescriptorListExporter {
root.put("author", StringUtils.defaultIfEmpty(descriptorList.getPublisher(), descriptorList.getOwner().getName()));
root.put("html", html);
return freemarker.processTemplateResource("html.ftl", root);
return freemarkerTemplating.processTemplateResource("html.ftl", root);
}
}
......@@ -79,7 +79,7 @@ public class DatasetServiceImpl implements DatasetService {
/** The file repo service. */
@Autowired
private RepositoryService fileRepoService;
private RepositoryService repositoryService;
@Autowired
private CustomAclService aclService;
......@@ -304,7 +304,7 @@ public class DatasetServiceImpl implements DatasetService {
InvalidRepositoryFileDataException {
dataset = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
final RepositoryFile repositoryFile = fileRepoService.addFile(Paths.get(datasetRepositoryPath, dataset.getUuid().toString()).toAbsolutePath().toString(), file
final RepositoryFile repositoryFile = repositoryService.addFile(Paths.get(datasetRepositoryPath, dataset.getUuid().toString()).toAbsolutePath().toString(), file
.getOriginalFilename(), file.getContentType(), file.getBytes(), null);
dataset.getRepositoryFiles().add(repositoryFile);
return lazyLoad(datasetRepository.save(dataset));
......@@ -320,7 +320,7 @@ public class DatasetServiceImpl implements DatasetService {
if (datasetFile == null) {
throw new NotFoundElement("No such dataset file");
}
final RepositoryFile updated = fileRepoService.updateMetadata(metadata);
final RepositoryFile updated = repositoryService.updateMetadata(metadata);
// replace it
dataset.getRepositoryFiles().replaceAll(df -> df.getUuid().equals(datasetFile.getUuid()) ? updated : df);
return lazyLoad(dataset);
......@@ -335,9 +335,9 @@ public class DatasetServiceImpl implements DatasetService {
public Dataset removeDatasetFile(Dataset dataset, final UUID fileUuid) throws NotFoundElement, NoSuchRepositoryFileException, IOException {
dataset = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
final RepositoryFile repositoryFile = fileRepoService.getFile(fileUuid);
final RepositoryFile repositoryFile = repositoryService.getFile(fileUuid);
dataset.getRepositoryFiles().remove(repositoryFile);
fileRepoService.removeFile(repositoryFile);
repositoryService.removeFile(repositoryFile);
return lazyLoad(datasetRepository.save(dataset));
}
......
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.config;
import java.io.File;
import org.genesys.filerepository.service.BytesStorageService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys.filerepository.service.impl.FilesystemStorageServiceImpl;
import org.genesys.filerepository.service.impl.RepositoryServiceImpl;
import org.genesys2.server.service.audit.SpringSecurityAuditorAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.annotation.DirtiesContext;
/**
* Loads and configures the test application context. Application configuration
* is loaded from
*
* <ul>
* <li>application.properties</li>
* <li>genesys-catalog.properties</li>
* <li>genesys-catalog-core.properties</li>
* </ul>
*
* @author Matija Obreza
*/
@Configuration
@ComponentScan(basePackages = { "org.genesys.catalog.service", "org.genesys.blocks.security.service",
"org.genesys2.server.security", "org.genesys.blocks.auditlog.service" })
public class TestApplicationConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean(name = "bytesStorageService")
public BytesStorageService bytesStorageService() {
final File repoDir = new File("data");
final FilesystemStorageServiceImpl storageService = new FilesystemStorageServiceImpl();
storageService.setRepositoryBaseDirectory(repoDir);
return storageService;
}
@Bean
public RepositoryService repositoryService() {
return new RepositoryServiceImpl();
}
@Bean
public SpringSecurityAuditorAware auditorAware() {
return new SpringSecurityAuditorAware();
}
}
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.server.controller.api.v0;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.persistence.PartnerRepository;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.traits.DescriptorRepository;
import org.genesys.catalog.server.controller.rest.AbstractRestTest;
import org.genesys.catalog.service.DatasetService;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.HashSet;
import java.util.UUID;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
public abstract class AbstractDatasetControllerTest extends AbstractRestTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
protected DatasetRepository datasetRepository;
@Autowired
private PartnerRepository partnerRepository;
@Autowired
protected DescriptorRepository descriptorRepository;
@Autowired
protected DatasetService datasetService;
protected MockMvc mockMvc;
public AbstractDatasetControllerTest() {
super();
}
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(documentationConfiguration(restDocumentation).uris().withScheme("https").withHost(
"api.catalog.genesys-pgr.org").withPort(443)).build();
}
@After
@Override
public void cleanup() {
datasetRepository.deleteAll();
partnerRepository.deleteAll();
super.cleanup();
}
protected Dataset setUpDataset(final boolean published) {
return setupDataset("Test Dataset Title", "Test Dataset Description", setUpPartner("Partner", true, "PartnerShortName", UUID.randomUUID()), published);
}
protected Dataset setupDataset(final String title, final String description, final Partner owner, final boolean published) {
final Dataset input = new Dataset();
input.setTitle(title);
input.setDescription(description);
input.setOwner(owner);
// input.setAccessionIdentifiers(accessions);
input.setPublished(published);
return input;
}
protected Partner setUpPartner(final String name, final boolean isActive, final String shortName, final UUID uuid) {
final Partner partner = new Partner();
partner.setName(name);
partner.setActive(isActive);
partner.setShortName(shortName);
partner.setUuid(uuid);
partner.setWiewsCodes(new HashSet<>());
partner.setUrls(new HashSet<>());
return partnerRepository.save(partner);
}
protected Descriptor setUpDescriptor() {
final Descriptor descriptor = new Descriptor();
descriptor.setUuid(UUID.randomUUID());
descriptor.setCategory(Descriptor.Category.PASSPORT);
descriptor.setDataType(Descriptor.DataType.BOOLEAN);
descriptor.setTitle("Title");
descriptor.setVocabulary(null);
descriptor.setMinValue(null);
descriptor.setMaxValue(null);
descriptor.setVersionTag("1.0");
descriptor.setOwner(setUpPartner("NewPartner", true, "ShortName", UUID.randomUUID()));
descriptor.setCrop("banana");
descriptor.setPublished(true);
return descriptor;
}
}
\ No newline at end of file
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.server.controller.api.v0;
import com.google.common.collect.Sets;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.catalog.api.v0.DatasetController;
import org.genesys.catalog.model.dataset.AccessionIdentifier;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.filters.PartnerFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.security.test.context.support.WithMockUser;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* @author Andrey Lugovskoy.
*/
public class DatasetControllerTest extends AbstractDatasetControllerTest {
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void getDataset() throws Exception {
Dataset dataset = setUpDataset(false);
dataset = datasetService.createDataset(dataset);
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.get(DatasetController.CONTROLLER_URL.concat("/{UUID}"), dataset.getUuid()))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(dataset.getUuid().toString())))
.andDo(document("contract-roles-get"));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void publishDataset() throws Exception {
Dataset dataset = setUpDataset(false);
dataset = datasetService.createDataset(dataset);
/*@formatter:off*/
mockMvc
.perform(post(DatasetController.CONTROLLER_URL.concat("/publish"))
.param("uuid", dataset.getUuid().toString())
.param("version", dataset.getVersion().toString()))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(dataset.getUuid().toString())))
.andExpect(jsonPath("$.published", is(true)))
// .andExpect(jsonPath("$.version", is(dataset.getVersion() + 1)))
.andDo(document("dataset-publish"));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void unpublishDataset() throws Exception {
Dataset dataset = setUpDataset(false);
dataset = datasetService.createDataset(dataset);
/*@formatter:off*/
mockMvc
.perform(post(DatasetController.CONTROLLER_URL.concat("/publish"))
.param("uuid", dataset.getUuid().toString())
.param("version", dataset.getVersion().toString()))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.published", is(true)))
// .andExpect(jsonPath("$.version", is(dataset.getVersion() + 1)))
;
mockMvc
.perform(post(DatasetController.CONTROLLER_URL.concat("/publish"))
.param("uuid", dataset.getUuid().toString())
.param("version", "" + (dataset.getVersion() + 1))
.param("published", "false"))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.published", is(false)));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "USER")
@Ignore
public void getDatasetNotFound() throws Exception {
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.get(DatasetController.CONTROLLER_URL.concat("/{UUID}"), UUID.randomUUID())
.accept(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isNotFound());
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void listDataset() throws Exception {
final DatasetFilter datasetFilter = new DatasetFilter();
final PartnerFilter partnerFilter = new PartnerFilter();
partnerFilter.shortName = new HashSet<>();
partnerFilter.shortName.add("PartnerShortNameTwo");
datasetFilter.owner = partnerFilter;
datasetService.createDataset(setUpDataset(false));
final Dataset dataset2 = datasetService.createDataset(setupDataset("Test Dataset Title Two", "Test Dataset Description Two", setUpPartner("PartnerTwo", true,
"PartnerShortNameTwo", UUID.randomUUID()), true));
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.post(DatasetController.CONTROLLER_URL.concat("/list"))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(datasetFilter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.filterCode", not(nullValue())))
.andExpect(jsonPath("$.filter", not(nullValue())))
.andExpect(jsonPath("$.content[0].id", is(dataset2.getId().intValue())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void listDatasetsByFilterCodeTest() throws Exception {
final DatasetFilter datasetFilter = new DatasetFilter();
final StringFilter datasetTitle = new StringFilter();
datasetTitle.contains = "two";
datasetFilter.title = datasetTitle;
final PartnerFilter partnerFilter = new PartnerFilter();
partnerFilter.shortName = new HashSet<>();
partnerFilter.shortName.add("PartnerShortNameTwo");
datasetFilter.owner = partnerFilter;
final Dataset dataset = datasetService.createDataset(setupDataset("Test Dataset Title Two", "Test Dataset Description Two", setUpPartner("PartnerTwo", true,
"PartnerShortNameTwo", UUID.randomUUID()), true));
datasetService.createDataset(setUpDataset(true));
assertThat(datasetRepository.count(), is(2L));
final String filterCode = shortFilterService.getCode(datasetFilter);
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.post(DatasetController.CONTROLLER_URL + "/list/{filterCode}", filterCode)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.filterCode", is(filterCode)))
.andExpect(jsonPath("$.filter", not(nullValue())))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.content[0].uuid", is(dataset.getUuid().toString())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void createDataset() throws Exception {
final Dataset dataset = setUpDataset(false);
final String s = verboseMapper.writeValueAsString(dataset);
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.post(DatasetController.CONTROLLER_URL + "/create")
.content(s)
.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())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void updateDataset() throws Exception {
final Dataset dataset = datasetService.createDataset(setUpDataset(false));
dataset.setTitle("Title 2");
final String s = verboseMapper.writeValueAsString(dataset);
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.post(DatasetController.CONTROLLER_URL + "/update")
.content(s)
.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(dataset.getUuid().toString())))
.andExpect(jsonPath("$.title", is("Title 2")));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void updateDatasetCrops() throws Exception {
final Dataset dataset = datasetService.createDataset(setUpDataset(false));
dataset.setTitle("Title 2");
dataset.setCrops(Sets.newHashSet("yam", "cassava"));
final String s = verboseMapper.writeValueAsString(dataset);
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.post(DatasetController.CONTROLLER_URL + "/update")
.content(s)
.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(dataset.getUuid().toString())))
.andExpect(jsonPath("$.title", is("Title 2")))
.andExpect(jsonPath("$.crops", hasSize(2)))
.andExpect(jsonPath("$.crops", containsInAnyOrder("cassava", "yam")));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void upsertAccessions() throws Exception {
final Dataset dataset = datasetService.createDataset(setUpDataset(false));
final Set<AccessionIdentifier> accessionIdentifiers = new Ha