Commit 15afa677 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added Dataset#crops as set of Strings

parent 71b046b7
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 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.
......@@ -118,6 +118,14 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
@Column(name = "descriptors")
private Integer descriptorCount;
/** The crops. */
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "datasetCrops", joinColumns = @JoinColumn(name = "datasetId"),
// index
indexes = { @Index(columnList = "datasetId, crop") })
@Column(name = "crop", nullable = false, length = 20)
private Set<String> crops;
/**
* User-specified versions version tag. E.g. "1.0", "1.1"
*/
......@@ -174,7 +182,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
if (descriptors != null) {
this.descriptorCount = descriptors.size();
}
trimStringsToNull();
}
......@@ -502,4 +510,22 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
public Integer getDescriptorCount() {
return descriptorCount;
}
/**
* Gets the crops.
*
* @return the crops
*/
public Set<String> getCrops() {
return crops;
}
/**
* Sets the crops.
*
* @param crops the new crops
*/
public void setCrops(Set<String> crops) {
this.crops = crops;
}
}
......@@ -102,6 +102,7 @@ public class DatasetServiceImpl implements DatasetService {
dataset.setRepositoryFiles(new ArrayList<>());
dataset.setCreators(new ArrayList<>());
dataset.setLocations(new ArrayList<>());
dataset.setCrops(source.getCrops());
if ((source.getAccessionIdentifiers() != null) && !source.getAccessionIdentifiers().isEmpty()) {
dataset.setAccessionIdentifiers(new HashSet<>(source.getAccessionIdentifiers()));
......@@ -353,6 +354,8 @@ public class DatasetServiceImpl implements DatasetService {
dataset.getCreators().size();
if (dataset.getLocations() != null)
dataset.getLocations().size();
if (dataset.getCrops() != null)
dataset.getCrops().size();
return dataset;
}
......@@ -436,6 +439,14 @@ public class DatasetServiceImpl implements DatasetService {
target.setOwner(source.getOwner());
target.setVersionTag(source.getVersionTag());
target.setCreated(source.getCreated());
if (source.getCrops() != null) {
if (target.getCrops() == null) {
target.setCrops(new HashSet<>());
}
target.getCrops().clear();
target.getCrops().addAll(source.getCrops());
}
// target.setDescriptors(source.getDescriptors());
}
......
......@@ -53,6 +53,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.mock.web.MockMultipartFile;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public class DatasetServiceTest extends AbstractDatasetServiceTest {
......@@ -80,6 +81,20 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input.getDescription(), is(DATASET_DESCRIPTION_2));
}
@Test
public void testCreateDatasetWithCrops() {
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, null, true);
input.setCrops(Sets.newHashSet("maize", "wheat"));
input = datasetService.createDataset(input);
assertThat(input, not(nullValue()));
assertThat(input.getCrops(), hasSize(2));
assertThat(input.getCrops(), containsInAnyOrder("wheat", "maize"));
input = datasetService.loadDataset(input.getUuid());
assertThat(input.getCrops(), hasSize(2));
assertThat(input.getCrops(), containsInAnyOrder("wheat", "maize"));
}
@Test
public void testCreateDatasetIsUnpublished() {
Dataset input = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
......@@ -195,6 +210,22 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
datasetVersionsRepository.save(resultUpdated.getVersions());
assertThat(resultUpdated.getVersions().getAllVersions(), hasSize(2));
}
@Test
public void testUpdateDatasetCrops() {
Dataset result = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
assertThat(result.isPublished(), is(false));
result.setCrops(Sets.newHashSet("cassava", "yam"));
result = datasetService.updateDataset(result);
assertThat(result.getCrops(), hasSize(2));
assertThat(result.getCrops(), containsInAnyOrder("yam", "cassava"));
result.getCrops().clear();
result = datasetService.updateDataset(result);
assertThat(result.getCrops(), hasSize(0));
}
@Test
public void testAddDescriptorToDataset() {
......
......@@ -3566,3 +3566,213 @@ databaseChangeLog:
- renameTable:
newTableName: repository_gallery_image
oldTableName: repositorygalleryimage
# Dataset crops
- changeSet:
id: 1518458507926-1
author: mobreza (generated)
comment: Added Dataset#crops as set of Strings
changes:
- createTable:
columns:
- column:
constraints:
nullable: false
name: dataset_id
type: BIGINT
- column:
constraints:
nullable: false
name: crop
type: VARCHAR(20)
tableName: dataset_crops
- addPrimaryKey:
columnNames: dataset_id, crop
constraintName: PRIMARY
tableName: dataset_crops
- addForeignKeyConstraint:
baseColumnNames: dataset_id
baseTableName: dataset_crops
constraintName: FK_dqdciicevxx3tluhep5ctkt39
deferrable: false
initiallyDeferred: false
onDelete: NO ACTION
onUpdate: NO ACTION
referencedColumnNames: id
referencedTableName: dataset
# file-repository:1.4 types
- changeSet:
id: 1518458507926-5
author: mobreza (generated)
comment: Update file-repository tables types
changes:
- modifyDataType:
columnName: abstract
newDataType: clob
tableName: repository_document
- modifyDataType:
columnName: access_rights
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: access_rights
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: access_rights
newDataType: varchar(200)
tableName: repository_image
- modifyDataType:
columnName: bibliographic_citation
newDataType: clob
tableName: repository_document
- modifyDataType:
columnName: bibliographic_citation
newDataType: clob
tableName: repository_file
- modifyDataType:
columnName: bibliographic_citation
newDataType: clob
tableName: repository_image
- modifyDataType:
columnName: content_type
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: content_type
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: content_type
newDataType: varchar(200)
tableName: repository_image
- modifyDataType:
columnName: created
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: created
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: created
newDataType: varchar(200)
tableName: repository_image
- modifyDataType:
columnName: creator
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: creator
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: creator
newDataType: varchar(200)
tableName: repository_image
- modifyDataType:
columnName: extension
newDataType: varchar(50)
tableName: repository_document
- modifyDataType:
columnName: extension
newDataType: varchar(50)
tableName: repository_file
- modifyDataType:
columnName: extension
newDataType: varchar(50)
tableName: repository_image
- modifyDataType:
columnName: extent
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: extent
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: extent
newDataType: varchar(200)
tableName: repository_image
- modifyDataType:
columnName: license
newDataType: varchar(50)
tableName: repository_document
- modifyDataType:
columnName: license
newDataType: varchar(50)
tableName: repository_file
- modifyDataType:
columnName: license
newDataType: varchar(50)
tableName: repository_image
- modifyDataType:
columnName: original_url
newDataType: varchar(500)
tableName: repository_document
- modifyDataType:
columnName: original_url
newDataType: varchar(500)
tableName: repository_file
- modifyDataType:
columnName: original_url
newDataType: varchar(500)
tableName: repository_image
- modifyDataType:
columnName: rights_holder
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: rights_holder
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: rights_holder
newDataType: varchar(200)
tableName: repository_image
- modifyDataType:
columnName: subject
newDataType: clob
tableName: repository_document
- modifyDataType:
columnName: subject
newDataType: clob
tableName: repository_file
- modifyDataType:
columnName: subject
newDataType: clob
tableName: repository_image
- modifyDataType:
columnName: title
newDataType: varchar(200)
tableName: repository_document
- modifyDataType:
columnName: title
newDataType: varchar(200)
tableName: repository_file
- modifyDataType:
columnName: title
newDataType: varchar(200)
tableName: repository_image
- changeSet:
id: 1518458507926-27
author: mobreza (generated)
comment: Limit length of 'extra' key column
changes:
- modifyDataType:
columnName: extra
newDataType: varchar(25)
tableName: descriptor_list_extra
- changeSet:
id: 1518458507926-31
author: mobreza (generated)
comment: mask column in acl_entry can't be null
changes:
- addNotNullConstraint:
columnDataType: int(10)
columnName: mask
tableName: acl_entry
......@@ -19,6 +19,8 @@ import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.containsInAnyOrder;
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.content;
......@@ -39,6 +41,8 @@ import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.security.test.context.support.WithMockUser;
import com.google.common.collect.Sets;
/**
* @author Andrey Lugovskoy.
*/
......@@ -173,7 +177,7 @@ public class DatasetControllerTest extends AbstractDatasetControllerTest {
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void updateDescriptorListTest() throws Exception {
public void updateDataset() throws Exception {
final Dataset dataset = datasetService.createDataset(setUpDataset(false));
dataset.setTitle("Title 2");
......@@ -194,6 +198,33 @@ public class DatasetControllerTest extends AbstractDatasetControllerTest {
/*@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.API_BASE + "/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 {
......
Supports Markdown
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