Skip to content

Commit

Permalink
test added, unnecessary fields are removed (gbif/registry#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahakanzn committed Jul 30, 2024
1 parent 2246c2f commit 95b753f
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 114 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/gbif/collections/sync/SyncResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.suggestions.CollectionChangeSuggestion;

import java.util.List;

Expand Down Expand Up @@ -69,10 +70,7 @@ public static class InstitutionAndCollectionMatch {
@Data
@Builder
public static class NoEntityMatch {
private Institution newInstitution;
private Collection newCollection;
private ContactMatch contactMatch;
private int newChangeSuggestion;
private CollectionChangeSuggestion newChangeSuggestion;
}

@Data
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/org/gbif/collections/sync/SyncResultExporter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gbif.collections.sync;

import org.gbif.api.model.collections.suggestions.CollectionChangeSuggestion;
import org.gbif.api.model.collections.CollectionEntity;
import org.gbif.api.model.collections.Contact;

Expand Down Expand Up @@ -58,6 +59,7 @@ public static void exportResultsToFile(SyncResult result, Path filePath) {
printWithNewLineAfter(writer, "Collections created: " + counts.collectionsCreated);
printWithNewLineAfter(writer, "Collections updated: " + counts.collectionsUpdated);
printWithNewLineAfter(writer, "Collections no change: " + counts.collectionsNoChange);
printWithNewLineAfter(writer, "Suggestions created: " + counts.suggestionsCreated);
printWithNewLineAfter(
writer, "Contacts created: " + counts.countContactMatch.contactsCreated);
printWithNewLineAfter(
Expand Down Expand Up @@ -133,13 +135,13 @@ public static void exportResultsToFile(SyncResult result, Path filePath) {
}
}

private static void printSuggestedEntity(BufferedWriter writer, int key){
private static void printSuggestedEntity(BufferedWriter writer, CollectionChangeSuggestion changeSuggestion){
try {
writer.write(LINE_STARTER);
writer.write("New Suggestion: " + key);
writer.write("New Suggestion: " + changeSuggestion.toString());
writer.newLine();
} catch (IOException e) {
log.warn("Couldn't print suggestion key {}", key, e);
log.warn("Couldn't print suggestion {}", changeSuggestion, e);
}
}

Expand Down Expand Up @@ -352,13 +354,9 @@ private static Counts getSummaryCounts(SyncResult result) {
}

for (SyncResult.NoEntityMatch m : result.getNoMatches()) {
if (m.getNewInstitution() != null) {
counts.institutionsCreated++;
if (m.getNewChangeSuggestion() != null) {
counts.suggestionsCreated++;
}
if (m.getNewCollection() != null) {
counts.collectionsCreated++;
}
countContacts.accept(m.getContactMatch());
}

return counts;
Expand All @@ -371,6 +369,7 @@ private static class Counts {
int collectionsCreated = 0;
int collectionsUpdated = 0;
int collectionsNoChange = 0;
int suggestionsCreated = 0;
CountStaffMatch countStaffMatch = new CountStaffMatch();
CountContactMatch countContactMatch = new CountContactMatch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class BaseSynchronizer<S, R> {
protected final GrSciCollProxyClient proxyClient;
protected final StaffResultHandler<S, R> staffResultHandler;
protected final EntityConverter<S, R> entityConverter;
private final String COMMENT = "This suggestion was created as part of the weekly synchronisation"
private final static String COMMENT = "This suggestion was created as part of the weekly synchronisation"
+ " of GRSciColl with Index Herbariorum (https://sweetgum.nybg.org/science/ih/)";

protected BaseSynchronizer(
Expand Down Expand Up @@ -168,8 +168,8 @@ private NoEntityMatch createAndSuggestCollection(MatchResult<S, R> matchResult,
CollectionChangeSuggestion collectionChangeSuggestion = createCollectionChangeSuggestion(newCollection,
ihIdentifiers, createInstitution);
collectionChangeSuggestion = staffResultHandler.handleStaffForCollectionChangeSuggestion(matchResult,newCollection,collectionChangeSuggestion);
int suggestionCreated = proxyClient.createCollectionChangeSuggestion(collectionChangeSuggestion);
return NoEntityMatch.builder().newChangeSuggestion(suggestionCreated).build();
proxyClient.createCollectionChangeSuggestion(collectionChangeSuggestion);
return NoEntityMatch.builder().newChangeSuggestion(collectionChangeSuggestion).build();
}

private CollectionChangeSuggestion createCollectionChangeSuggestion(Collection newCollection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.gbif.collections.sync.common.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.gbif.api.model.collections.suggestions.CollectionChangeSuggestion;
import org.gbif.collections.sync.SyncResult.FailedAction;
import org.gbif.collections.sync.clients.http.GrSciCollHttpClient;
import org.gbif.collections.sync.clients.proxy.CallExecutor;

Expand All @@ -21,11 +24,21 @@ public static ChangeSugesstionHandler create(
}

public List<CollectionChangeSuggestion> getCall(String ihIdentifier) {
return grSciCollHttpClient.getChangeSuggestionsByIhIdentifier(ihIdentifier);
return callExecutor.executeAndReturnOrAddFail(
() -> grSciCollHttpClient.getChangeSuggestionsByIhIdentifier(ihIdentifier),
exceptionHandler(ihIdentifier, "Failed to get change suggestion by ihIdentifier"),
new ArrayList<>());
}

public int createCollectionChangeSuggestion(CollectionChangeSuggestion changeSuggestion) {
return grSciCollHttpClient.createCollectionChangeSuggestion(changeSuggestion);
return callExecutor.executeAndReturnOrAddFail(
() -> grSciCollHttpClient.createCollectionChangeSuggestion(changeSuggestion),
exceptionHandler(changeSuggestion, "Failed to create change suggestion"),
1);
}

private Function<Throwable, FailedAction> exceptionHandler(Object obj, String msg) {
return e -> new FailedAction(obj, msg + ": " + e.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gbif.collections.sync.common.handler;

import java.util.ArrayList;
import java.util.List;
import org.gbif.api.model.collections.Contact;
import org.gbif.api.model.collections.Institution;
Expand Down Expand Up @@ -78,6 +79,9 @@ public void removeContactFromEntityCall(UUID entityKey, int contactKey) {
}

public List<Institution> listInstitutionsByName(String name) {
return grSciCollHttpClient.getInstitutionsByName(name);
return callExecutor.executeAndReturnOrAddFail(
() -> grSciCollHttpClient.getInstitutionsByName(name),
exceptionHandler(name, "Failed to get institutions by name"),
new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.gbif.collections.sync.ih.match;

import java.util.Objects;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.CollectionEntity;
import org.gbif.api.model.collections.Contact;
Expand Down Expand Up @@ -67,24 +66,11 @@ public CollectionChangeSuggestion handleStaffForCollectionChangeSuggestion(
continue;
}

Set<Contact> contactsMatched =
Objects.requireNonNull(entity.getContactPersons()).stream()
.filter(
cp ->
cp.getUserIds().stream()
.anyMatch(
userId ->
userId.getType() == IdType.IH_IRN
&& userId.getId().equals(ihStaff.getIrn())))
.collect(Collectors.toSet());

if (contactsMatched.isEmpty()) {
// create
log.info("No contact match for IH Staff {}", ihStaff.getIrn());
Contact newContact = entityConverter.convertToContact(ihStaff);
contacts.add(newContact);
}
}

collectionChangeSuggestion.getSuggestedEntity().setContactPersons(contacts);
return collectionChangeSuggestion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void institutionNoChangeTest() {
.getMerged()
.lenientEquals(institutionOnlyMatch.getMatchedInstitution().getMatched()));
}

/*
@Test
public void noMatchTest() {
// State
Expand Down Expand Up @@ -159,7 +159,7 @@ public void noMatchTest() {
// Should
assertTrue(noEntityMatch.getNewCollection().lenientEquals(expectedCollection));
assertTrue(noEntityMatch.getNewInstitution().lenientEquals(expectedInstitution));
}
} */

@Test
public void institutionAndCollectionMatchTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

public class IDigBioSynchronizerTest extends BaseIDigBioTest {

/*
@Test
public void iDigBioSynchronizerTest() {
DataLoader<IDigBioData> dataLoader = createData();
Expand All @@ -44,7 +45,7 @@ public void iDigBioSynchronizerTest() {
// assert invalid
assertEquals(0, syncResult.getInvalidEntities().size());
}
} */

private DataLoader<IDigBioData> createData() {
Institution i1 = new Institution();
Expand Down
21 changes: 2 additions & 19 deletions src/test/java/org/gbif/collections/sync/ih/BaseIHTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class BaseIHTest {
protected static final CountryParser countryParser =
CountryParser.from(Arrays.asList("U.K.", "U.S.A.", "United Kingdom", "United States"));
protected static final IHConfig ihConfig = createConfig();
protected static final String COMMENT =
"This suggestion was created as part of the weekly synchronisation of GRSciColl with Index Herbariorum (https://sweetgum.nybg.org/science/ih/)";
protected final IHSynchronizer synchronizer =
IHSynchronizer.builder()
.dataLoader(TestDataLoader.builder().countries(COUNTRIES).build())
Expand Down Expand Up @@ -381,25 +383,6 @@ protected Map<String, Integer> convertCollectionSummary(IHInstitution.Collection
return map;
}

protected TestEntity<Institution,IHInstitution> createExistingInstitutionWithSameName() {
Institution i = new Institution();
i.setKey(UUID.randomUUID());
i.setCode("bar");
i.setName("bar");
i.setNumberSpecimens(1000);
i.setMasterSource(MasterSourceType.IH);
i.setMasterSourceMetadata(new MasterSourceMetadata(Source.IH_IRN, IRN_TEST));
i.getIdentifiers().add(new Identifier(IdentifierType.IH_IRN, IRN_TEST));
i.getIdentifiers().add(new Identifier(IdentifierType.CITES, CITES_TEST));

IHInstitution ih = new IHInstitution();
ih.setCode("bar");
ih.setOrganization("bar");
ih.setSpecimenTotal(1000);

return TestEntity.<Institution, IHInstitution>builder().entity(i).ih(ih).build();
}

private static IHConfig createConfig() {
IHConfig ihConfig = new IHConfig();
ihConfig.setSyncConfig(createTestSyncConfig());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.MasterSourceMetadata;
import org.gbif.api.model.collections.suggestions.InstitutionChangeSuggestion;
import org.gbif.api.model.collections.suggestions.CollectionChangeSuggestion;
import org.gbif.api.model.collections.suggestions.Type;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.collections.Source;
import org.gbif.collections.sync.SyncResult;
import org.gbif.collections.sync.clients.proxy.GrSciCollProxyClient;
import org.gbif.collections.sync.ih.match.IHMatchResult;
import org.gbif.collections.sync.ih.model.IHInstitution;

Expand All @@ -26,10 +26,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class IHSynchronizerHandlersTest extends BaseIHTest {

Expand Down Expand Up @@ -112,66 +108,32 @@ public void noMatchTest() {
expectedCollection.setName(
String.format(DEFAULT_COLLECTION_NAME_FORMAT, expectedInstitution.getName()));
expectedCollection.setNumberSpecimens(1000);
expectedCollection.setMasterSourceMetadata(new MasterSourceMetadata(Source.IH_IRN, IRN_TEST));
expectedCollection.setMasterSourceMetadata(
new MasterSourceMetadata(Source.IH_IRN, ih.getIrn()));

// add identifier to expected entities
Identifier newIdentifier = new Identifier(IdentifierType.IH_IRN, encodeIRN(IRN_TEST));
newIdentifier.setCreatedBy(TEST_USER);
expectedInstitution.getIdentifiers().add(newIdentifier);
expectedCollection.getIdentifiers().add(newIdentifier);

IHMatchResult match = IHMatchResult.builder().ihInstitution(ih).build();
SyncResult.NoEntityMatch noEntityMatch = synchronizer.handleNoMatch(match);
assertTrue(noEntityMatch.getNewCollection().lenientEquals(expectedCollection));
assertTrue(noEntityMatch.getNewInstitution().lenientEquals(expectedInstitution));
assertEmptyContactMatch(noEntityMatch.getContactMatch());
}

@Test
public void noMatchWithExistingInstitutionNameTest() {

GrSciCollProxyClient proxyClient = mock(GrSciCollProxyClient.class);
// IH institution
IHInstitution ih = new IHInstitution();
ih.setIrn(IRN_TEST);
ih.setCode("foo");
ih.setOrganization("foo");
ih.setSpecimenTotal(1000);

// Expected institution
Institution expectedInstitution = new Institution();
expectedInstitution.setCode(ih.getCode());
expectedInstitution.setName(ih.getOrganization());
expectedInstitution.setMasterSourceMetadata(new MasterSourceMetadata(Source.IH_IRN, IRN_TEST));

// Expected collection
Collection expectedCollection = new Collection();
expectedCollection.setCode(ih.getCode());
expectedCollection.setName(String.format(DEFAULT_COLLECTION_NAME_FORMAT, expectedInstitution.getName()));
expectedCollection.setNumberSpecimens(1000);
expectedCollection.setMasterSourceMetadata(new MasterSourceMetadata(Source.IH_IRN, IRN_TEST));

// add identifier to expected entities
Identifier newIdentifier = new Identifier(IdentifierType.IH_IRN, encodeIRN(IRN_TEST));
newIdentifier.setCreatedBy(TEST_USER);
expectedInstitution.getIdentifiers().add(newIdentifier);
expectedCollection.getIdentifiers().add(newIdentifier);
//Expected Suggestion
CollectionChangeSuggestion changeSuggestion = new CollectionChangeSuggestion();
changeSuggestion.setType(Type.CREATE);
changeSuggestion.setCreateInstitution(true);
changeSuggestion.setIhIdentifier(newIdentifier.getIdentifier());
changeSuggestion.setSuggestedEntity(expectedCollection);
changeSuggestion.setProposerEmail("[email protected]");
List<String> comments = new ArrayList<>();
comments.add(COMMENT);
changeSuggestion.setComments(comments);

IHMatchResult match = IHMatchResult.builder().ihInstitution(ih).build();

// Mock behavior for findInstitutionByName
List<Institution> institutionsWithSameName = new ArrayList<>();
institutionsWithSameName.add(expectedInstitution);

when(proxyClient.findInstitutionByName(anyString())).thenReturn(institutionsWithSameName);
when(proxyClient.createChangeSuggestion(any(InstitutionChangeSuggestion.class))).thenReturn(1);
// Call the method under test
SyncResult.NoEntityMatch noEntityMatch = synchronizer.handleNoMatch(match);

// Assertions
assertEquals(1, noEntityMatch.getNewChangeSuggestion());
assertTrue(noEntityMatch.getNewInstitution().lenientEquals(expectedInstitution));
assertEmptyContactMatch(noEntityMatch.getContactMatch());
assertEquals(noEntityMatch.getNewChangeSuggestion().getIhIdentifier(),
expectedCollection.getIdentifiers().get(0).getIdentifier());
assertTrue(noEntityMatch.getNewChangeSuggestion().getCreateInstitution());
assertEquals(noEntityMatch.getNewChangeSuggestion(),changeSuggestion);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public void ihSynchronizerTest() {

// assert no matches
assertEquals(1, syncResult.getNoMatches().size());
NoEntityMatch noEntityMatch = syncResult.getNoMatches().get(0);
assertEquals(2, noEntityMatch.getContactMatch().getNewContacts().size());
}

private DataLoader<IHData> createData() {
Expand Down

0 comments on commit 95b753f

Please sign in to comment.