Skip to content

Commit

Permalink
#255 service to merge 2 grscicoll entities
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Dec 15, 2020
1 parent 5522e99 commit 391d349
Show file tree
Hide file tree
Showing 21 changed files with 1,111 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
* Copyright 2020 Global Biodiversity Information Facility (GBIF)
*
* 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.gbif.registry.ws.it.collections.merge;

import org.gbif.api.model.collections.Address;
import org.gbif.api.model.collections.CollectionEntity;
import org.gbif.api.model.collections.Contactable;
import org.gbif.api.model.collections.OccurrenceMappeable;
import org.gbif.api.model.collections.OccurrenceMapping;
import org.gbif.api.model.collections.Person;
import org.gbif.api.model.registry.Commentable;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.model.registry.Identifiable;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.Installation;
import org.gbif.api.model.registry.MachineTag;
import org.gbif.api.model.registry.MachineTaggable;
import org.gbif.api.model.registry.Node;
import org.gbif.api.model.registry.Organization;
import org.gbif.api.model.registry.Taggable;
import org.gbif.api.service.collections.ContactService;
import org.gbif.api.service.collections.CrudService;
import org.gbif.api.service.collections.OccurrenceMappingService;
import org.gbif.api.service.collections.PersonService;
import org.gbif.api.service.registry.DatasetService;
import org.gbif.api.service.registry.IdentifierService;
import org.gbif.api.service.registry.InstallationService;
import org.gbif.api.service.registry.MachineTagService;
import org.gbif.api.service.registry.NodeService;
import org.gbif.api.service.registry.OrganizationService;
import org.gbif.api.vocabulary.DatasetType;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.InstallationType;
import org.gbif.api.vocabulary.Language;
import org.gbif.api.vocabulary.License;
import org.gbif.api.vocabulary.NodeType;
import org.gbif.api.vocabulary.ParticipationStatus;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.merge.MergeService;
import org.gbif.registry.ws.it.BaseItTest;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import java.util.UUID;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public abstract class BaseMergeServiceIT<
T extends
CollectionEntity & Identifiable & MachineTaggable & OccurrenceMappeable & Contactable
& Taggable & Commentable>
extends BaseItTest {

private final MergeService mergeService;
private final CrudService<T> crudService;
private final IdentifierService identifierService;
private final ContactService contactService;
private final MachineTagService machineTagService;
private final OccurrenceMappingService occurrenceMappingService;
private final PersonService personService;

@Autowired private DatasetService datasetService;
@Autowired private NodeService nodeService;
@Autowired private OrganizationService organizationService;
@Autowired private InstallationService installationService;

public BaseMergeServiceIT(
SimplePrincipalProvider simplePrincipalProvider,
EsManageServer esServer,
MergeService mergeService,
CrudService<T> crudService,
IdentifierService identifierService,
ContactService contactService,
MachineTagService machineTagService,
OccurrenceMappingService occurrenceMappingService,
PersonService personService) {
super(simplePrincipalProvider, esServer);
this.mergeService = mergeService;
this.crudService = crudService;
this.identifierService = identifierService;
this.contactService = contactService;
this.machineTagService = machineTagService;
this.occurrenceMappingService = occurrenceMappingService;
this.personService = personService;
}

@Test
public void mergeTest() {
T toReplace = createEntityToReplace();

// addresses
Address a1 = new Address();
a1.setAddress("a1");
toReplace.setAddress(a1);

Address ma1 = new Address();
ma1.setAddress("ma1");
toReplace.setMailingAddress(ma1);

crudService.create(toReplace);

// identifiers
Identifier identifier = new Identifier(IdentifierType.LSID, "test");
identifierService.addIdentifier(toReplace.getKey(), identifier);

// contacts
Person p1 = new Person();
p1.setFirstName("p1");
personService.create(p1);

Person p2 = new Person();
p2.setFirstName("p2");
personService.create(p2);

contactService.addContact(toReplace.getKey(), p1.getKey());
contactService.addContact(toReplace.getKey(), p2.getKey());

// machine tags
MachineTag mt1 = new MachineTag("iDigBio.org", "test", "test");
machineTagService.addMachineTag(toReplace.getKey(), mt1);
MachineTag mt2 = new MachineTag("foo", "test", "test");
machineTagService.addMachineTag(toReplace.getKey(), mt2);

// occurrence mappings
Dataset dataset = createDataset();
OccurrenceMapping om1 = new OccurrenceMapping();
om1.setDatasetKey(dataset.getKey());
occurrenceMappingService.addOccurrenceMapping(toReplace.getKey(), om1);

T replacement = createReplacement();

Address a2 = new Address();
a2.setAddress("a2");
replacement.setAddress(a2);

crudService.create(replacement);

contactService.addContact(replacement.getKey(), p1.getKey());

mergeService.merge(toReplace.getKey(), replacement.getKey(), "test");

T replaced = crudService.get(toReplace.getKey());
T replacementUpdated = crudService.get(replacement.getKey());

assertEquals(0, replaced.getIdentifiers().size());
assertEquals(2, replacementUpdated.getIdentifiers().size());
assertEquals(1, replaced.getMachineTags().size());
assertEquals(1, replacementUpdated.getMachineTags().size());
assertEquals(2, replacementUpdated.getContacts().size());
assertEquals(a2, replacementUpdated.getAddress());
assertEquals(ma1, replacementUpdated.getMailingAddress());
assertEquals(replacement.getCreatedBy(), replacementUpdated.getCreatedBy());
assertNull(replacementUpdated.getDeleted());
assertEquals(1, replacementUpdated.getOccurrenceMappings().size());

extraAsserts(replaced, replacement, replacementUpdated);
}

@Test
public void preconditionsTest() {
T e1 = createEntityToReplace();
crudService.create(e1);
identifierService.addIdentifier(e1.getKey(), new Identifier(IdentifierType.IH_IRN, "test"));

T e2 = createReplacement();
crudService.create(e2);
identifierService.addIdentifier(e2.getKey(), new Identifier(IdentifierType.IH_IRN, "test"));

assertThrows(
IllegalArgumentException.class, () -> mergeService.merge(e1.getKey(), e2.getKey(), "user"));

assertThrows(
IllegalArgumentException.class,
() -> mergeService.merge(e1.getKey(), UUID.randomUUID(), "user"));

assertThrows(
IllegalArgumentException.class,
() -> mergeService.merge(UUID.randomUUID(), e2.getKey(), "user"));

assertThrows(
IllegalArgumentException.class, () -> mergeService.merge(e1.getKey(), e2.getKey(), null));
}

private Dataset createDataset() {
Node node = new Node();
node.setTitle("node");
node.setType(NodeType.COUNTRY);
node.setParticipationStatus(ParticipationStatus.AFFILIATE);
nodeService.create(node);

Organization org = new Organization();
org.setEndorsingNodeKey(node.getKey());
org.setTitle("organization");
org.setLanguage(Language.ABKHAZIAN);
org.setPassword("testtttt");
organizationService.create(org);

Installation installation = new Installation();
installation.setTitle("title");
installation.setOrganizationKey(org.getKey());
installation.setType(InstallationType.BIOCASE_INSTALLATION);
installationService.create(installation);

Dataset dataset = new Dataset();
dataset.setTitle("title");
dataset.setInstallationKey(installation.getKey());
dataset.setPublishingOrganizationKey(org.getKey());
dataset.setType(DatasetType.CHECKLIST);
dataset.setLanguage(Language.ABKHAZIAN);
dataset.setLicense(License.CC0_1_0);
datasetService.create(dataset);

return dataset;
}

abstract T createEntityToReplace();

abstract T createReplacement();

abstract void extraAsserts(T replaced, T replacement, T replacementUpdated);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 2020 Global Biodiversity Information Facility (GBIF)
*
* 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.gbif.registry.ws.it.collections.merge;

import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.Person;
import org.gbif.api.service.collections.CollectionService;
import org.gbif.api.service.collections.InstitutionService;
import org.gbif.api.service.collections.PersonService;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.merge.CollectionMergeService;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests the {@link CollectionMergeService}.
*/
public class CollectionMergeServiceIT extends BaseMergeServiceIT<Collection> {

private final CollectionMergeService collectionMergeService;
private final CollectionService collectionService;
private final InstitutionService institutionService;
private final PersonService personService;

@Autowired
public CollectionMergeServiceIT(
SimplePrincipalProvider simplePrincipalProvider,
EsManageServer esServer,
CollectionMergeService collectionMergeService,
CollectionService collectionService,
InstitutionService institutionService,
PersonService personService) {
super(
simplePrincipalProvider,
esServer,
collectionMergeService,
collectionService,
collectionService,
collectionService,
collectionService,
collectionService,
personService);
this.collectionMergeService = collectionMergeService;
this.collectionService = collectionService;
this.institutionService = institutionService;
this.personService = personService;
}

@Test
public void primaryCollectionInPersonsTests() {
Collection toReplace = createEntityToReplace();
collectionService.create(toReplace);

// contact that has the replaced collection as primary collection
Person p3 = new Person();
p3.setFirstName("p3");
p3.setPrimaryCollectionKey(toReplace.getKey());
personService.create(p3);

Collection replacement = createReplacement();
collectionService.create(replacement);

collectionMergeService.merge(toReplace.getKey(), replacement.getKey(), "test");

Person p3Updated = personService.get(p3.getKey());
assertEquals(replacement.getKey(), p3Updated.getPrimaryCollectionKey());
}

@Test
public void extraPreconditionsTest() {
Institution i1 = new Institution();
i1.setCode("i1");
i1.setName("i1");
institutionService.create(i1);

Institution i2 = new Institution();
i2.setCode("i2");
i2.setName("i2");
institutionService.create(i2);

Collection toReplace = createEntityToReplace();
toReplace.setInstitutionKey(i1.getKey());
collectionService.create(toReplace);

Collection replacement = createReplacement();
replacement.setInstitutionKey(i2.getKey());
collectionService.create(replacement);

assertThrows(
IllegalArgumentException.class,
() -> collectionMergeService.merge(toReplace.getKey(), replacement.getKey(), "user"));
}

@Override
Collection createEntityToReplace() {
Collection toReplace = new Collection();
toReplace.setCode("c1");
toReplace.setName("n1");
toReplace.setDescription("description");
toReplace.getEmail().add("a@a.com");
toReplace.getEmail().add("b@a.com");
toReplace.setGeography("replaced geo");
return toReplace;
}

@Override
Collection createReplacement() {
Collection replacement = new Collection();
replacement.setCode("c2");
replacement.setName("n2");
replacement.getEmail().add("a@a.com");
replacement.setGeography("geo");
return replacement;
}

@Override
void extraAsserts(Collection replaced, Collection replacement, Collection replacementUpdated) {
assertEquals(replacement.getKey(), replaced.getReplacedBy());
assertEquals(2, replacementUpdated.getEmail().size());
assertEquals(replacement.getGeography(), replacementUpdated.getGeography());
assertEquals(replaced.getDescription(), replacementUpdated.getDescription());
}
}
Loading

0 comments on commit 391d349

Please sign in to comment.