Skip to content

Commit

Permalink
use of new interfaces for services
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed May 5, 2021
1 parent c1b0baf commit b9526c6
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static <T extends CollectionEntity> TestData<T> create(Class<T> clazz) {
throw new UnsupportedOperationException();
}

// TODO: populate more fields

public static class CollectionTestData implements TestData<Collection> {

public static final String NAME = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import org.gbif.api.model.registry.MachineTaggable;
import org.gbif.api.model.registry.Tag;
import org.gbif.api.model.registry.Taggable;
import org.gbif.api.service.collections.CrudService;
import org.gbif.api.service.registry.CommentService;
import org.gbif.api.service.registry.IdentifierService;
import org.gbif.api.service.registry.MachineTagService;
import org.gbif.api.service.registry.TagService;
import org.gbif.api.service.collections.CollectionEntityService;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.ws.client.collections.BaseCollectionEntityClient;
Expand Down Expand Up @@ -77,7 +73,7 @@ public void crudTest() {
T entity = testData.newEntity();
UUID entityKey = UUID.randomUUID();

when(getMockCrudService().create(entity)).thenReturn(entityKey);
when(getMockBaseService().create(entity)).thenReturn(entityKey);
UUID key = baseClient.create(entity);
assertEquals(entityKey, key);

Expand All @@ -88,15 +84,15 @@ public void crudTest() {

// update
entity = testData.updateEntity(entitySaved);
doNothing().when(getMockCrudService()).update(entity);
doNothing().when(getMockBaseService()).update(entity);
baseClient.update(entity);

mockGetEntity(entityKey, entity);
entitySaved = baseClient.get(key);
assertTrue(entity.lenientEquals(entitySaved));

// delete
doNothing().when(getMockCrudService()).delete(key);
doNothing().when(getMockBaseService()).delete(key);
baseClient.delete(key);

entity.setDeleted(new Date());
Expand Down Expand Up @@ -176,19 +172,19 @@ public void tagsTest() {
tag.setValue("value");

int tagKey = 1;
when(getMockTagService().addTag(key, tag)).thenReturn(tagKey);
when(getMockBaseService().addTag(key, tag)).thenReturn(tagKey);
int returnedKey = baseClient.addTag(key, tag);
assertEquals(tagKey, returnedKey);
tag.setKey(returnedKey);

when(getMockTagService().listTags(key, null)).thenReturn(Collections.singletonList(tag));
when(getMockBaseService().listTags(key, null)).thenReturn(Collections.singletonList(tag));

List<Tag> tagsReturned = baseClient.listTags(key, null);
assertEquals(1, tagsReturned.size());
assertEquals(tagKey, tagsReturned.get(0).getKey());
assertEquals("value", tagsReturned.get(0).getValue());

doNothing().when(getMockTagService()).deleteTag(key, tagKey);
doNothing().when(getMockBaseService()).deleteTag(key, tagKey);
assertDoesNotThrow(() -> baseClient.deleteTag(key, tagKey));
}

Expand All @@ -197,20 +193,20 @@ public void machineTagsTest() {
UUID entityKey = UUID.randomUUID();
MachineTag machineTag = new MachineTag("ns", "name", "value");
int machineTagKey = 1;
when(getMockMachineTagService().addMachineTag(entityKey, machineTag)).thenReturn(machineTagKey);
when(getMockBaseService().addMachineTag(entityKey, machineTag)).thenReturn(machineTagKey);

int machineTagKeyReturned = baseClient.addMachineTag(entityKey, machineTag);
assertEquals(machineTagKey, machineTagKeyReturned);
machineTag.setKey(machineTagKeyReturned);

when(getMockMachineTagService().listMachineTags(entityKey))
when(getMockBaseService().listMachineTags(entityKey))
.thenReturn(Collections.singletonList(machineTag));
List<MachineTag> machineTags = baseClient.listMachineTags(entityKey);
assertEquals(1, machineTags.size());
assertEquals(machineTagKey, machineTags.get(0).getKey());
assertEquals("value", machineTags.get(0).getValue());

doNothing().when(getMockMachineTagService()).deleteMachineTag(entityKey, machineTagKey);
doNothing().when(getMockBaseService()).deleteMachineTag(entityKey, machineTagKey);
assertDoesNotThrow(() -> baseClient.deleteMachineTag(entityKey, machineTagKey));
}

Expand All @@ -223,20 +219,20 @@ public void identifiersTest() {
identifier.setType(IdentifierType.LSID);

int identifierKey = 1;
when(getMockIdentifierService().addIdentifier(entityKey, identifier)).thenReturn(identifierKey);
when(getMockBaseService().addIdentifier(entityKey, identifier)).thenReturn(identifierKey);
int identifierKeyReturned = baseClient.addIdentifier(entityKey, identifier);
assertEquals(identifierKey, identifierKeyReturned);
identifier.setKey(identifierKeyReturned);

when(getMockIdentifierService().listIdentifiers(entityKey))
when(getMockBaseService().listIdentifiers(entityKey))
.thenReturn(Collections.singletonList(identifier));
List<Identifier> identifiers = baseClient.listIdentifiers(entityKey);
assertEquals(1, identifiers.size());
assertEquals(identifierKey, identifiers.get(0).getKey());
assertEquals("identifier", identifiers.get(0).getIdentifier());
assertEquals(IdentifierType.LSID, identifiers.get(0).getType());

doNothing().when(getMockIdentifierService()).deleteIdentifier(entityKey, identifierKey);
doNothing().when(getMockBaseService()).deleteIdentifier(entityKey, identifierKey);
assertDoesNotThrow(() -> baseClient.deleteIdentifier(entityKey, identifierKey));
}

Expand All @@ -247,33 +243,25 @@ public void commentsTest() {
Comment comment = new Comment();
comment.setContent("test comment");
int commentKey = 1;
when(getMockCommentService().addComment(entityKey, comment)).thenReturn(commentKey);
when(getMockBaseService().addComment(entityKey, comment)).thenReturn(commentKey);
int commentKeyReturned = baseClient.addComment(entityKey, comment);
assertEquals(commentKey, commentKeyReturned);
comment.setKey(commentKey);

when(getMockCommentService().listComments(entityKey))
when(getMockBaseService().listComments(entityKey))
.thenReturn(Collections.singletonList(comment));
List<Comment> comments = baseClient.listComments(entityKey);
assertEquals(1, comments.size());
assertEquals(commentKey, comments.get(0).getKey());
assertEquals(comment.getContent(), comments.get(0).getContent());

doNothing().when(getMockCommentService()).deleteComment(entityKey, commentKey);
doNothing().when(getMockBaseService()).deleteComment(entityKey, commentKey);
assertDoesNotThrow(() -> baseClient.deleteComment(entityKey, commentKey));
}

void mockGetEntity(UUID key, T entityToReturn) {
when(getMockCrudService().get(key)).thenReturn(entityToReturn);
when(getMockBaseService().get(key)).thenReturn(entityToReturn);
}

protected abstract CrudService<T> getMockCrudService();

protected abstract TagService getMockTagService();

protected abstract MachineTagService getMockMachineTagService();

protected abstract IdentifierService getMockIdentifierService();

protected abstract CommentService getMockCommentService();
protected abstract CollectionEntityService<T> getMockBaseService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
import org.gbif.api.model.common.paging.PagingRequest;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.search.collections.KeyCodeNameResult;
import org.gbif.api.service.collections.CollectionEntityService;
import org.gbif.api.service.collections.CollectionService;
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.registry.CommentService;
import org.gbif.api.service.registry.IdentifierService;
import org.gbif.api.service.registry.MachineTagService;
import org.gbif.api.service.registry.TagService;
import org.gbif.api.service.collections.PrimaryCollectionEntityService;
import org.gbif.api.vocabulary.Country;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.duplicates.CollectionDuplicatesService;
Expand Down Expand Up @@ -109,37 +104,7 @@ public void listDeletedTest() {
// TODO: merge, suggestions

@Override
protected CrudService<Collection> getMockCrudService() {
return collectionService;
}

@Override
protected TagService getMockTagService() {
return collectionService;
}

@Override
protected MachineTagService getMockMachineTagService() {
return collectionService;
}

@Override
protected IdentifierService getMockIdentifierService() {
return collectionService;
}

@Override
protected CommentService getMockCommentService() {
return collectionService;
}

@Override
protected ContactService getMockContactService() {
return collectionService;
}

@Override
protected OccurrenceMappingService getMockOccurrenceMappingService() {
protected PrimaryCollectionEntityService<Collection> getMockPrimaryEntityService() {
return collectionService;
}

Expand All @@ -156,4 +121,9 @@ protected CollectionClient getClient() {
void mockGetEntity(UUID key, Collection entityToReturn) {
when(collectionService.getCollectionView(key)).thenReturn(new CollectionView(entityToReturn));
}

@Override
protected CollectionEntityService<Collection> getMockBaseService() {
return collectionService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
import org.gbif.api.model.common.paging.PagingRequest;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.search.collections.KeyCodeNameResult;
import org.gbif.api.service.collections.ContactService;
import org.gbif.api.service.collections.CrudService;
import org.gbif.api.service.collections.CollectionEntityService;
import org.gbif.api.service.collections.InstitutionService;
import org.gbif.api.service.collections.OccurrenceMappingService;
import org.gbif.api.service.registry.CommentService;
import org.gbif.api.service.registry.IdentifierService;
import org.gbif.api.service.registry.MachineTagService;
import org.gbif.api.service.registry.TagService;
import org.gbif.api.service.collections.PrimaryCollectionEntityService;
import org.gbif.api.vocabulary.Country;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.duplicates.DuplicatesService;
Expand Down Expand Up @@ -110,37 +105,7 @@ public void listDeletedTest() {
// TODO: merge, suggestions

@Override
protected CrudService<Institution> getMockCrudService() {
return institutionService;
}

@Override
protected TagService getMockTagService() {
return institutionService;
}

@Override
protected MachineTagService getMockMachineTagService() {
return institutionService;
}

@Override
protected IdentifierService getMockIdentifierService() {
return institutionService;
}

@Override
protected CommentService getMockCommentService() {
return institutionService;
}

@Override
protected ContactService getMockContactService() {
return institutionService;
}

@Override
protected OccurrenceMappingService getMockOccurrenceMappingService() {
protected PrimaryCollectionEntityService<Institution> getMockPrimaryEntityService() {
return institutionService;
}

Expand All @@ -152,4 +117,9 @@ protected DuplicatesService getMockDuplicatesService() {
protected InstitutionClient getClient() {
return (InstitutionClient) baseClient;
}

@Override
protected CollectionEntityService<Institution> getMockBaseService() {
return institutionService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.search.collections.PersonSuggestResult;
import org.gbif.api.service.collections.CrudService;
import org.gbif.api.service.collections.CollectionEntityService;
import org.gbif.api.service.collections.PersonService;
import org.gbif.api.service.registry.CommentService;
import org.gbif.api.service.registry.IdentifierService;
import org.gbif.api.service.registry.MachineTagService;
import org.gbif.api.service.registry.TagService;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.registry.search.test.EsManageServer;
Expand Down Expand Up @@ -124,32 +120,12 @@ public void getPersonWithAddressTest() {
assertEquals(IdentifierType.IH_IRN, personSaved.getIdentifiers().get(0).getType());
}

@Override
protected CrudService<Person> getMockCrudService() {
return personService;
}

@Override
protected TagService getMockTagService() {
return personService;
}

@Override
protected MachineTagService getMockMachineTagService() {
return personService;
}

@Override
protected IdentifierService getMockIdentifierService() {
return personService;
protected PersonClient getClient() {
return (PersonClient) baseClient;
}

@Override
protected CommentService getMockCommentService() {
protected CollectionEntityService<Person> getMockBaseService() {
return personService;
}

protected PersonClient getClient() {
return (PersonClient) baseClient;
}
}
Loading

0 comments on commit b9526c6

Please sign in to comment.