Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed May 17, 2021
1 parent 6e888ad commit 236ed3a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<spring-cloud-sleuth.version>2.2.8.RELEASE</spring-cloud-sleuth.version>

<!-- GBIF -->
<gbif-api.version>0.145</gbif-api.version>
<gbif-api.version>0.148-SNAPSHOT</gbif-api.version>
<gbif-common.version>0.50</gbif-common.version>
<gbif-common-mybatis.version>1.1</gbif-common-mybatis.version>
<gbif-common-ws.version>1.15</gbif-common-ws.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private SubEntityCollectionEvent(
String subEntityKey,
EventType eventType) {
super(eventType, collectionEntityClass);
this.collectionEntityKey = Preconditions.checkNotNull(collectionEntityKey);
this.collectionEntityKey = collectionEntityKey;
this.subEntityClass = Preconditions.checkNotNull(subEntityClass);
this.subEntity = subEntity;
this.subEntityKey = Preconditions.checkNotNull(subEntityKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;

import static org.gbif.registry.domain.collections.Constants.IDIGBIO_NAMESPACE;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public abstract class BaseMergeServiceIT<
T extends
Expand Down Expand Up @@ -106,7 +106,6 @@ public BaseMergeServiceIT(
this.personService = personService;
}

@WithMockUser(username = "aa")
@Test
public void mergeTest() {
T toReplace = createEntityToReplace();
Expand Down Expand Up @@ -173,8 +172,8 @@ public void mergeTest() {
assertEquals(2, replaced.getMachineTags().size());
assertEquals(1, replacementUpdated.getMachineTags().size());
assertEquals(2, replacementUpdated.getContacts().size());
assertEquals(a2, replacementUpdated.getAddress());
assertEquals(ma1, replacementUpdated.getMailingAddress());
assertTrue(a2.lenientEquals(replacementUpdated.getAddress()));
assertTrue(ma1.lenientEquals(replacementUpdated.getMailingAddress()));
assertEquals(replacement.getCreatedBy(), replacementUpdated.getCreatedBy());
assertNull(replacementUpdated.getDeleted());
assertEquals(1, replaced.getOccurrenceMappings().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface ChangeSuggestionMapper {
List<ChangeSuggestionDto> list(
@Param("status") Status status,
@Param("type") Type type,
@Param("entityType") CollectionEntityType collectionEntityType,
@Param("collectionEntityType") CollectionEntityType collectionEntityType,
@Param("country") Country country,
@Param("proposer") String proposer,
@Param("entityKey") UUID entityKey,
Expand All @@ -34,7 +34,7 @@ List<ChangeSuggestionDto> list(
long count(
@Param("status") Status status,
@Param("type") Type type,
@Param("entityType") CollectionEntityType collectionEntityType,
@Param("collectionEntityType") CollectionEntityType collectionEntityType,
@Param("country") Country country,
@Param("proposer") String proposer,
@Param("entityKey") UUID entityKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
CREATE TABLE grscicoll_audit_log (
key bigserial NOT NULL PRIMARY KEY,
trace_id bigserial NOT NULL,
entity_type enum_grscicoll_entity_type NOT NULL,
entity_key uuid,
collection_entity_type enum_grscicoll_entity_type NOT NULL,
subEntityType varchar(50),
operation varchar(50) NOT NULL,
collection_entity_key uuid,
sub_entity_key varchar(36),
replacement_key uuid,
created timestamptz NOT NULL DEFAULT now(),
created_by varchar NOT NULL CHECK (assert_min_length(created_by, 3)),
pre_state jsonb,
post_state jsonb,
note text);
post_state jsonb)
]]>
</sql>
</changeSet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.gbif.api.model.registry.Commentable;
import org.gbif.api.model.registry.Identifiable;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.MachineTag;
import org.gbif.api.model.registry.MachineTaggable;
import org.gbif.api.model.registry.Taggable;
import org.gbif.api.service.collections.PrimaryCollectionEntityService;
Expand All @@ -35,7 +36,6 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -109,30 +109,27 @@ && isIDigBioRecord(replacement)) {

// merge entity fields
T updatedEntityToReplace = mergeEntityFields(entityToReplace, replacement);
updatedEntityToReplace.setModifiedBy(authentication.getName());
primaryEntityService.update(updatedEntityToReplace);

// copy the identifiers
entityToReplace
.getIdentifiers()
.forEach(
i -> {
i.setKey(null);
// TODO: created must be null, create utility method to reuse
primaryEntityService.addIdentifier(replacementKey, i);
});
i ->
primaryEntityService.addIdentifier(
replacementKey, new Identifier(i.getType(), i.getIdentifier())));

// copy iDigBio machine tags
entityToReplace.getMachineTags().stream()
.filter(mt -> mt.getNamespace().equals(IDIGBIO_NAMESPACE))
.forEach(
mt -> {
mt.setKey(null);
primaryEntityService.addMachineTag(replacementKey, mt);
});
mt ->
primaryEntityService.addMachineTag(
replacementKey,
new MachineTag(mt.getNamespace(), mt.getName(), mt.getValue())));

// merge contacts
Objects.requireNonNull(entityToReplace.getContacts()).stream()
entityToReplace.getContacts().stream()
.filter(c -> !replacement.getContacts().contains(c))
.forEach(c -> primaryEntityService.addContact(replacementKey, c.getKey()));

Expand All @@ -147,7 +144,9 @@ && isIDigBioRecord(replacement)) {
occMappings.forEach(
om -> {
om.setKey(null);
primaryEntityService.addOccurrenceMapping(replacementKey, om);
primaryEntityService.addOccurrenceMapping(
replacementKey,
new OccurrenceMapping(om.getCode(), om.getIdentifier(), om.getDatasetKey()));
});

additionalOperations(entityToReplace, replacement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ void checkMergeExtraPreconditions(Collection entityToReplace, Collection replace
replacement.getReplacedBy() == null, "Cannot do a merge with an entity that was replaced");
}

// TODO: si uso primaryCollectionEntity lo puedo mover a la base
@Override
Collection mergeEntityFields(Collection entityToReplace, Collection replacement) {
setNullFields(replacement, entityToReplace);
Expand Down Expand Up @@ -85,7 +84,7 @@ Collection mergeEntityFields(Collection entityToReplace, Collection replacement)
@Override
void additionalOperations(Collection entityToReplace, Collection replacement) {
// fix primary collection of contacts
PagingResponse<Person> persons = personService.list(null, entityToReplace.getKey(), null, null);
PagingResponse<Person> persons = personService.list(null, null, entityToReplace.getKey(), null);
persons
.getResults()
.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import org.gbif.api.model.collections.AlternativeCode;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.OccurrenceMapping;
import org.gbif.api.model.collections.Person;
import org.gbif.api.model.collections.request.CollectionSearchRequest;
import org.gbif.api.model.collections.view.CollectionView;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.MachineTag;
import org.gbif.api.service.collections.CollectionService;
import org.gbif.api.service.collections.InstitutionService;
import org.gbif.api.service.collections.PersonService;
Expand Down Expand Up @@ -91,7 +94,6 @@ && isIDigBioRecord(institutionToConvert)) {
}

Collection newCollection = new Collection();
newCollection.setKey(UUID.randomUUID());
newCollection.setCode(institutionToConvert.getCode());
newCollection.setAlternativeCodes(institutionToConvert.getAlternativeCodes());
newCollection.setName(institutionToConvert.getName());
Expand All @@ -105,17 +107,12 @@ && isIDigBioRecord(institutionToConvert)) {
newCollection.setApiUrl(institutionToConvert.getApiUrl());
newCollection.setAddress(institutionToConvert.getAddress());
newCollection.setMailingAddress(institutionToConvert.getMailingAddress());
newCollection.setCreatedBy(authentication.getName());
newCollection.setModifiedBy(authentication.getName());

// if there is no institution passed we need to create a new institution
if (institutionKeyForNewCollection == null) {
Institution newInstitution = new Institution();
newInstitution.setKey(UUID.randomUUID());
newInstitution.setCode(institutionToConvert.getCode());
newInstitution.setName(newInstitutionName);
newInstitution.setCreatedBy(authentication.getName());
newInstitution.setModifiedBy(authentication.getName());
institutionService.create(newInstitution);

newCollection.setInstitutionKey(newInstitution.getKey());
Expand All @@ -140,27 +137,28 @@ && isIDigBioRecord(institutionToConvert)) {
institutionToConvert
.getIdentifiers()
.forEach(
i -> {
i.setKey(null);
collectionService.addIdentifier(newCollection.getKey(), i);
});
i ->
collectionService.addIdentifier(
newCollection.getKey(), new Identifier(i.getType(), i.getIdentifier())));

// move the machine tags
institutionToConvert
.getMachineTags()
.forEach(
mt -> {
mt.setKey(null);
collectionService.addMachineTag(newCollection.getKey(), mt);
});
mt ->
collectionService.addMachineTag(
newCollection.getKey(),
new MachineTag(mt.getNamespace(), mt.getName(), mt.getValue())));

// move the occurrence mappings
institutionToConvert
.getOccurrenceMappings()
.forEach(
om -> {
om.setKey(null);
collectionService.addOccurrenceMapping(newCollection.getKey(), om);
collectionService.addOccurrenceMapping(
newCollection.getKey(),
new OccurrenceMapping(om.getCode(), om.getIdentifier(), om.getDatasetKey()));
});

// copy the contacts
Expand Down

0 comments on commit 236ed3a

Please sign in to comment.