Commit 8119c25a authored by Maxym Borodenko's avatar Maxym Borodenko

Update AccessionId#imageCount without touching version and lastModified data

parent 6d6224fc
......@@ -133,8 +133,7 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
ImageGallery imageGallery = instituteFilesService.createImageGallery(accession.getInstitute(), accession);
if (accessionId.getRepositoryFolder() == null) {
accessionId.setRepositoryFolder(imageGallery.getFolder());
accessionIdRepository.save(accessionId);
accessionIdRepository.updateRepositoryFolder(accessionId, imageGallery.getFolder());
}
}
}
......@@ -168,11 +167,10 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
try {
ImageGallery imageGallery = instituteFilesService.loadImageGallery(accession.getInstitute(), accession);
if (imageGallery != null) {
accessionId.setImageCount(imageGallery.getImages().size());
accessionIdRepository.updateImageCount(accessionId, imageGallery.getImages().size());
} else {
accessionId.setImageCount(0);
accessionIdRepository.updateImageCount(accessionId, 0);
}
accessionIdRepository.save(accessionId);
} catch (InvalidRepositoryPathException e) {
LOG.warn("Error updating imageCount of accession, error is {}", e.getMessage());
}
......
......@@ -15,6 +15,7 @@
*/
package org.genesys2.server.persistence;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys2.server.model.genesys.AccessionId;
import java.util.List;
......@@ -35,4 +36,9 @@ public interface AccessionIdRepositoryCustom {
void decreaseSubsetCount(List<AccessionId> accessionIds);
void resetSubsetAndDatasetCounters();
void updateImageCount(AccessionId accessionId, int imageCount);
void updateRepositoryFolder(AccessionId accessionId, RepositoryFolder repositoryFolder);
}
......@@ -19,6 +19,7 @@ import java.util.List;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.QAccessionId;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -69,6 +70,22 @@ public class AccessionIdRepositoryCustomImpl implements AccessionIdRepositoryCus
.execute();
}
@Override
public void updateImageCount(AccessionId accessionId, int imageCount) {
jpaQueryFactory.update(QAccessionId.accessionId)
.where(QAccessionId.accessionId.eq(accessionId))
.set(QAccessionId.accessionId.imageCount, Math.max(imageCount, 0))
.execute();
}
@Override
public void updateRepositoryFolder(AccessionId accessionId, RepositoryFolder repositoryFolder) {
jpaQueryFactory.update(QAccessionId.accessionId)
.where(QAccessionId.accessionId.eq(accessionId))
.set(QAccessionId.accessionId.repositoryFolder, repositoryFolder)
.execute();
}
private void decreaseCount(List<AccessionId> accessionIds, NumberPath<Long> path) {
jpaQueryFactory.update(QAccessionId.accessionId)
.where(QAccessionId.accessionId.in(accessionIds).and(path.gt(0)))
......
......@@ -15,33 +15,98 @@
*/
package org.genesys.test.base;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import org.genesys.test.config.ApplicationConfig;
import org.junit.After;
import org.junit.Before;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionHistoricRepository;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.worker.AccessionUploader;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev")
@ContextHierarchy(@ContextConfiguration(name = "services", classes = { ApplicationConfig.BaseConfig.class }))
public abstract class AbstractServiceTest extends AbstractTest {
@Before
@Transactional
@Override
public void beforeTest() throws Exception {
super.beforeTest();
@Autowired
protected InstituteService instituteService;
@Autowired
private AccessionUploader accessionUploader;
@Autowired
protected AccessionIdRepository accessionIdRepository;
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected AccessionHistoricRepository accessionHistoricRepository;
@Autowired
protected FaoInstituteRepository instituteRepository;
protected FaoInstitute setupInstitute(final String code){
final FaoInstitute inputI = new FaoInstitute();
inputI.setCode(code);
return instituteRepository.save(inputI);
}
protected Accession upsertAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode updates = objectMapper.createArrayNode();
ObjectNode accession = updates.addObject();
accession.put("instituteCode", instCode);
accession.put("accessionNumber", acceNumb);
ObjectNode taxa = accession.putObject("taxonomy");
taxa.put("genus", genus);
accessionUploader.upsertAccessions(institute, updates);
return accessionRepository.findOne(institute, null, acceNumb, genus);
}
@After
@Transactional
@Override
public void cleanup() throws Exception {
super.cleanup();
protected void deleteAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper mapper = new ObjectMapper();
final ArrayNode arrayNode = mapper.createArrayNode();
ObjectNode rootNode = arrayNode.addObject();
rootNode.put("instituteCode", instCode);
rootNode.put("accessionNumber", acceNumb);
ObjectNode taxonNode = rootNode.putObject("taxonomy");
taxonNode.put("genus", genus);
AccessionHeaderJson ah = new AccessionHeaderJson();
ah.instCode = instCode;
ah.acceNumb = acceNumb;
ah.genus = genus;
accessionUploader.deleteAccessions(institute, arrayNode);
assert(accessionRepository.findOne(institute, null, acceNumb, genus) == null);
}
}
\ No newline at end of file
......@@ -38,26 +38,11 @@ import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.VocabularyService;
import org.genesys.filerepository.persistence.RepositoryFolderRepository;
import org.genesys.test.base.AbstractServiceTest;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionHistoricRepository;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.worker.AccessionUploader;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
......@@ -93,12 +78,6 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
@Autowired
protected PartnerRepository partnerRepository;
@Autowired
protected InstituteService instituteService;
@Autowired
protected FaoInstituteRepository instituteRepository;
@Autowired
protected DescriptorListService descriptorListService;
......@@ -123,16 +102,6 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
@Autowired
protected ShortFilterRepository shortFilterRepository;
@Autowired
private AccessionUploader accessionUploader;
@Autowired
protected AccessionIdRepository accessionIdRepository;
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected AccessionHistoricRepository accessionHistoricRepository;
@Autowired
private RepositoryFolderRepository repositoryFolderRepository;
......@@ -173,12 +142,6 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
return partnerService.createPartner(inputP);
}
protected FaoInstitute setupInstitute(final String code){
final FaoInstitute inputI = new FaoInstitute();
inputI.setCode(code);
return instituteRepository.save(inputI);
}
protected DescriptorList setupDescriptorList(final String title, final String version, final String description) {
final DescriptorList descriptorList = new DescriptorList();
descriptorList.setOwner(partner);
......@@ -214,53 +177,4 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
return input;
}
protected Accession upsertAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode updates = objectMapper.createArrayNode();
ObjectNode accession = updates.addObject();
accession.put("instituteCode", instCode);
accession.put("accessionNumber", acceNumb);
ObjectNode taxa = accession.putObject("taxonomy");
taxa.put("genus", genus);
accessionUploader.upsertAccessions(institute, updates);
return accessionRepository.findOne(institute, null, acceNumb, genus);
}
protected void deleteAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper mapper = new ObjectMapper();
final ArrayNode arrayNode = mapper.createArrayNode();
ObjectNode rootNode = arrayNode.addObject();
rootNode.put("instituteCode", instCode);
rootNode.put("accessionNumber", acceNumb);
ObjectNode taxonNode = rootNode.putObject("taxonomy");
taxonNode.put("genus", genus);
AccessionHeaderJson ah = new AccessionHeaderJson();
ah.instCode = instCode;
ah.acceNumb = acceNumb;
ah.genus = genus;
accessionUploader.deleteAccessions(institute, arrayNode);
assert(accessionRepository.findOne(institute, null, acceNumb, genus) == null);
}
}
......@@ -29,6 +29,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -54,6 +55,7 @@ import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.service.impl.SearchException;
import org.junit.Test;
import org.springframework.dao.ConcurrencyFailureException;
......@@ -194,6 +196,9 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertNotNull(accession);
final Long acceId = accession.getId();
assertTrue(acceId > 0);
final Long lastModifiedBy = accession.getAccessionId().getLastModifiedBy();
final Integer version = accession.getAccessionId().getVersion();
final Date lastModifiedDate = accession.getAccessionId().getLastModifiedDate();
Dataset dataset = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, PublishState.DRAFT);
dataset = datasetService.createDataset(dataset);
......@@ -210,19 +215,32 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
dataset = datasetService.reviewDataset(dataset);
assertEquals(PublishState.REVIEWING, dataset.getState());
assertEquals(0L, accessionIdRepository.findById(acceId).get().getDatasetCount());
AccessionId updatedAccessionId = accessionIdRepository.findById(acceId).get();
assertEquals(0L, updatedAccessionId.getDatasetCount());
assertEquals(lastModifiedBy, updatedAccessionId.getLastModifiedBy());
assertEquals(version, updatedAccessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), updatedAccessionId.getLastModifiedDate().getTime());
dataset = datasetService.approveDataset(dataset);
assertEquals(PublishState.PUBLISHED, dataset.getState());
// expecting increased dataset count after approving
assertEquals(1L, accessionIdRepository.findById(acceId).get().getDatasetCount());
updatedAccessionId = accessionIdRepository.findById(acceId).get();
assertEquals(1L, updatedAccessionId.getDatasetCount());
assertEquals(lastModifiedBy, updatedAccessionId.getLastModifiedBy());
assertEquals(version, updatedAccessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), updatedAccessionId.getLastModifiedDate().getTime());
dataset = datasetService.rejectDataset(dataset);
assertEquals(PublishState.DRAFT, dataset.getState());
// expecting decreased dataset count after approving
assertEquals(0L, accessionIdRepository.findById(acceId).get().getDatasetCount());
updatedAccessionId = accessionIdRepository.findById(acceId).get();
assertNotNull(updatedAccessionId);
assertEquals(0L, updatedAccessionId.getDatasetCount());
assertEquals(lastModifiedBy, updatedAccessionId.getLastModifiedBy());
assertEquals(version, updatedAccessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), updatedAccessionId.getLastModifiedDate().getTime());
}
@Test
......
......@@ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -13,6 +14,7 @@ import javax.validation.ConstraintViolationException;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetAccessionRef;
......@@ -231,6 +233,9 @@ public class SubsetServiceTest extends CatalogServiceTest {
public void testUpdateSubsetCountInAccessionId() {
final Accession accession = upsertAccession("XXX0001", "acceNumb1", "genus1");
assertNotNull(accession);
final Long lastModifiedBy = accession.getAccessionId().getLastModifiedBy();
final Integer version = accession.getAccessionId().getVersion();
final Date lastModifiedDate = accession.getAccessionId().getLastModifiedDate();
final Long acceId = accession.getId();
assertTrue(acceId > 0);
......@@ -244,19 +249,31 @@ public class SubsetServiceTest extends CatalogServiceTest {
subset = subsetService.reviewSubset(subset);
assertEquals(PublishState.REVIEWING, subset.getState());
assertEquals(0L, accessionIdRepository.findById(acceId).get().getSubsetCount());
AccessionId updatedAccessionId = accessionIdRepository.findById(acceId).get();
assertEquals(0L, updatedAccessionId.getSubsetCount());
assertEquals(lastModifiedBy, updatedAccessionId.getLastModifiedBy());
assertEquals(version, updatedAccessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), updatedAccessionId.getLastModifiedDate().getTime());
subset = subsetService.approveSubset(subset);
assertEquals(PublishState.PUBLISHED, subset.getState());
// expecting increased subset count after approving
assertEquals(1L, accessionIdRepository.findById(acceId).get().getSubsetCount());
updatedAccessionId = accessionIdRepository.findById(acceId).get();
assertEquals(1L, updatedAccessionId.getSubsetCount());
assertEquals(lastModifiedBy, updatedAccessionId.getLastModifiedBy());
assertEquals(version, updatedAccessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), updatedAccessionId.getLastModifiedDate().getTime());
subset = subsetService.rejectSubset(subset);
assertEquals(PublishState.DRAFT, subset.getState());
// expecting decreased subset count after approving
assertEquals(0L, accessionIdRepository.findById(acceId).get().getSubsetCount());
updatedAccessionId = accessionIdRepository.findById(acceId).get();
assertEquals(0L, updatedAccessionId.getSubsetCount());
assertEquals(lastModifiedBy, updatedAccessionId.getLastModifiedBy());
assertEquals(version, updatedAccessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), updatedAccessionId.getLastModifiedDate().getTime());
}
@Test
......
......@@ -26,8 +26,8 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
......@@ -223,7 +223,7 @@ public class RepositoryControllerTest extends AbstractApiTest {
/*@formatter:off*/
mockMvc
.perform(fileUpload(RepositoryController.CONTROLLER_URL.concat("/upload").concat(FOLDER_PATH_1))
.perform(multipart(RepositoryController.CONTROLLER_URL.concat("/upload").concat(FOLDER_PATH_1))
.file(MOCK_FILE)
.file(metadata))
// .andDo(MockMvcResultHandlers.print())
......
/*
* Copyright 2020 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.test.server.services;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.RepositoryImage;
import org.genesys.filerepository.persistence.ImageGalleryPersistence;
import org.genesys.filerepository.persistence.RepositoryImagePersistence;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.FaoInstitute;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Maxym Borodenko
*/
public class AccessionServiceTest extends AbstractServicesTest {
private static final String INST_CODE = "XYZ001";
private static final String ACCE_NUMB = "A1";
private FaoInstitute institute;
@Autowired
private RepositoryService repositoryService;
@Autowired
private RepositoryImagePersistence repositoryImagePersistence;
@Autowired
private ImageGalleryPersistence imageGalleryPersistence;
@Before
@Transactional
@Override
public void beforeTest() throws Exception {
super.beforeTest();
institute = setupInstitute(INST_CODE);
assertNotNull(institute);
}
@After
@Transactional
@Override
public void cleanup() throws Exception {
accessionRepository.deleteAll();
accessionHistoricRepository.deleteAll();
instituteRepository.deleteAll();
imageGalleryPersistence.deleteAll();
repositoryImagePersistence.deleteAll();
repositoryFileRepository.deleteAll();
accessionIdRepository.deleteAll();
super.cleanup();
}
/**
* Test updating of AccessionId#imageCount without touching version and lastModified data
*/
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void updateImageCountInAccessionIdTest() throws InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException {
Accession accession = upsertAccession(INST_CODE, ACCE_NUMB, "genus1");
assertNotNull(accession);
final Long lastModifiedBy = accession.getAccessionId().getLastModifiedBy();
final Integer version = accession.getAccessionId().getVersion();
final Date lastModifiedDate = accession.getAccessionId().getLastModifiedDate();
final Long acceId = accession.getId();
assertTrue(acceId > 0);
final RepositoryImage image = new RepositoryImage();
image.setDescription("Test description of image.");
image.setOriginalFilename("testImage.jpg");
Path repositoryPath = Paths.get("/wiews/" + INST_CODE + "/acn/" + ACCE_NUMB);
AccessionId accessionId = accessionIdRepository.findById(acceId).get();
assertEquals(0, accessionId.getImageCount());
assertEquals(lastModifiedBy, accessionId.getLastModifiedBy());
assertEquals(version, accessionId.getVersion());
assertEquals(lastModifiedDate.getTime(), accessionId.getLastModifiedDate().getTime());
// add image
RepositoryImage savedImage = repositoryService.addImage(repositoryPath, image.getOriginalFilename(), image.getContentType(), new byte[0], null);
assertNotNull(savedImage);
// expecting increased image count after adding the image
accessionId = accessionIdRepository.findById(acceId).get();
assertEquals(1, accessionId.getImageCount());
// version, lastModifiedBy and lastModifiedDate must not be updated
assertEquals(lastModifiedBy, accessionId.getLastModifiedBy());
assertEquals(lastModifiedDate.getTime(), accessionId.getLastModifiedDate().getTime());
assertEquals(version, accessionId.getVersion());
}
}
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