Skip to content

Commit

Permalink
PrimaryCollectionEntity + minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed May 4, 2021
1 parent b7964d9 commit 02aacf6
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@

import static org.gbif.registry.ws.it.fixtures.TestConstants.IT_APP_KEY2;

// TODO: los scan meterlos en clases de config separados y luego q importe cada uno las q quiera

/** Base class for IT tests that initializes data sources and basic security settings. */
@ExtendWith(SpringExtension.class)
@SpringBootTest(
Expand All @@ -81,7 +79,6 @@ public static class EsContainerContextInitializer
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("elasticsearch.mock=true")
// .and("spring.main.allow-bean-definition-overriding=true")
.applyTo(configurableApplicationContext.getEnvironment());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,26 @@
/** Base class for IT tests that initializes data sources and basic security settings. */
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = RegistryIntegrationTestsConfiguration.class)
@ContextConfiguration(initializers = {BaseServiceIT.ContextInitializer.class})
@ContextConfiguration(
initializers = {
BaseServiceIT.ContextInitializer.class,
BaseServiceIT.EsContainerContextInitializer.class
})
@ActiveProfiles("test")
@AutoConfigureMockMvc
@DirtiesContext
public class BaseServiceIT {

public static class EsContainerContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("elasticsearch.mock=true")
.applyTo(configurableApplicationContext.getEnvironment());
}
}

/** Prepares a Tests database using an embedded Postgres instance. */
public static class EmbeddedDataBaseInitializer {
private final DataSource dataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.gbif.registry.persistence.mapper.collections.params.DuplicatesSearchParams;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.duplicates.CollectionDuplicatesService;
import org.gbif.registry.ws.resources.collections.CollectionResource;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import java.util.ArrayList;
Expand All @@ -57,7 +56,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/** Tests the {@link CollectionResource}. */
/** Tests the {@link CollectionService}. */
public class CollectionServiceIT extends PrimaryCollectionEntityServiceIT<Collection> {

private final CollectionService collectionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.gbif.registry.identity.service.IdentityService;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.service.collections.duplicates.InstitutionDuplicatesService;
import org.gbif.registry.ws.resources.collections.InstitutionResource;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import java.util.Collections;
Expand All @@ -47,7 +46,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/** Tests the {@link InstitutionResource}. */
/** Tests the {@link InstitutionService}. */
public class InstitutionServiceIT extends PrimaryCollectionEntityServiceIT<Institution> {

private final InstitutionService institutionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.gbif.registry.ws.it.collections.service;

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.collections.PrimaryCollectionEntity;
import org.gbif.api.model.collections.duplicates.Duplicate;
import org.gbif.api.model.collections.duplicates.DuplicatesResult;
import org.gbif.api.model.registry.Commentable;
Expand Down Expand Up @@ -79,8 +79,8 @@

public abstract class PrimaryCollectionEntityServiceIT<
T extends
CollectionEntity & Taggable & MachineTaggable & Identifiable & Contactable & Commentable
& OccurrenceMappeable & LenientEquals<T>>
PrimaryCollectionEntity & Taggable & MachineTaggable & Identifiable & Contactable
& Commentable & OccurrenceMappeable & LenientEquals<T>>
extends BaseCollectionEntityServiceIT<T> {

protected final PersonService personService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
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.registry.ws.it.collections.service.BaseServiceIT;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import java.util.UUID;
Expand All @@ -71,7 +72,7 @@ public abstract class BaseMergeServiceIT<
T extends
CollectionEntity & Identifiable & MachineTaggable & OccurrenceMappeable & Contactable
& Taggable & Commentable>
extends BaseItTest {
extends BaseServiceIT {

protected final MergeService mergeService;
protected final CrudService<T> crudService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.gbif.api.vocabulary.Country;
import org.gbif.registry.database.TestCaseDatabaseInitializer;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.ws.it.BaseItTest;
import org.gbif.registry.ws.it.collections.service.BaseServiceIT;
import org.gbif.ws.client.filter.SimplePrincipalProvider;

import java.util.Collections;
Expand All @@ -49,7 +49,7 @@
/** Tests the {@link ChangeSuggestionService}. */
public abstract class BaseChangeSuggestionServiceIT<
T extends CollectionEntity & Contactable & LenientEquals<T>, R extends ChangeSuggestion<T>>
extends BaseItTest {
extends BaseServiceIT {

protected static final String PROPOSER = "proposer";
protected static final Pageable DEFAULT_PAGE = new PagingRequest(0L, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public class GrSciCollEditorAuthorizationFilterTest {
new GrSciCollEditorAuthorizationService(
mockUserRightsMapper, mockCollectionMapper, mockInstitutionMapper, mockPersonMapper);

// TODO
private final GrSciCollEditorAuthorizationFilter filter =
new GrSciCollEditorAuthorizationFilter(authService, mockAuthenticationFacade, objectMapper, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import static org.gbif.registry.security.UserRoles.GRSCICOLL_ADMIN_ROLE;
import static org.gbif.registry.security.UserRoles.GRSCICOLL_EDITOR_ROLE;

// TODO: Move @Secured ??
@Validated
public abstract class BaseCollectionEntityService<
T extends CollectionEntity & Taggable & Identifiable & MachineTaggable & Commentable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.gbif.registry.service.collections;

import org.gbif.api.model.collections.Address;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.CollectionEntity;
import org.gbif.api.model.collections.Contactable;
import org.gbif.api.model.collections.Institution;
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.collections.PrimaryCollectionEntity;
import org.gbif.api.model.registry.Commentable;
import org.gbif.api.model.registry.Identifiable;
import org.gbif.api.model.registry.Identifier;
Expand Down Expand Up @@ -56,8 +55,8 @@
@Validated
public abstract class PrimaryCollectionEntityService<
T extends
CollectionEntity & Taggable & Identifiable & MachineTaggable & Contactable & Commentable
& OccurrenceMappeable>
PrimaryCollectionEntity & Taggable & Identifiable & MachineTaggable & Contactable
& Commentable & OccurrenceMappeable>
extends BaseCollectionEntityService<T> implements ContactService, OccurrenceMappingService {

private final ContactableMapper contactableMapper;
Expand Down Expand Up @@ -261,27 +260,13 @@ public void deleteOccurrenceMapping(UUID entityKey, int occurrenceMappingKey) {
entityKey, objectClass, OccurrenceMapping.class));
}

// TODO: hacer interface PrimaryCollectionEntity

/**
* Some iDigBio collections and institutions don't have code and we allow that in the DB but not
* in the API.
*/
protected void checkCodeUpdate(T newEntity, T oldEntity) {
if (newEntity instanceof Institution) {
Institution newInstitution = (Institution) newEntity;
Institution oldInstitution = (Institution) oldEntity;

if (newInstitution.getCode() == null && oldInstitution.getCode() != null) {
throw new IllegalArgumentException("Not allowed to delete the code of an institution");
}
} else if (newEntity instanceof Collection) {
Collection newCollection = (Collection) newEntity;
Collection oldCollection = (Collection) oldEntity;

if (newCollection.getCode() == null && oldCollection.getCode() != null) {
throw new IllegalArgumentException("Not allowed to delete the code of a collection");
}
if (newEntity.getCode() == null && oldEntity.getCode() != null) {
throw new IllegalArgumentException("Not allowed to delete the code of a primary entity");
}
}

Expand All @@ -290,28 +275,23 @@ protected void checkCodeUpdate(T newEntity, T oldEntity) {
* converted in an update
*/
protected void checkReplacedEntitiesUpdate(T newEntity, T oldEntity) {
if (oldEntity.getReplacedBy() != null) {
throw new IllegalArgumentException("Not allowed to update a replaced entity");
} else if (newEntity.getReplacedBy() != null) {
throw new IllegalArgumentException("Not allowed to replace an entity while updating");
}

if (newEntity instanceof Institution) {
Institution newInstitution = (Institution) newEntity;
Institution oldInstitution = (Institution) oldEntity;

if (oldInstitution.getReplacedBy() != null
|| oldInstitution.getConvertedToCollection() != null) {
if (oldInstitution.getConvertedToCollection() != null) {
throw new IllegalArgumentException(
"Not allowed to update a replaced or converted institution");
} else if (newInstitution.getReplacedBy() != null
|| newInstitution.getConvertedToCollection() != null) {
} else if (newInstitution.getConvertedToCollection() != null) {
throw new IllegalArgumentException(
"Not allowed to replace or convert an institution while updating");
}
} else if (newEntity instanceof Collection) {
Collection newCollection = (Collection) newEntity;
Collection oldCollection = (Collection) oldEntity;

if (oldCollection.getReplacedBy() != null) {
throw new IllegalArgumentException("Not allowed to update a replaced collection");
} else if (newCollection.getReplacedBy() != null) {
throw new IllegalArgumentException("Not allowed to replace a collection while updating");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ public ApplySuggestionResult applyChangeSuggestion(@PathVariable("key") int key)
return result;
}

// TODO: move to gbif-api
public static class ApplySuggestionResult {
private UUID entityCreatedKey;

Expand Down

0 comments on commit 02aacf6

Please sign in to comment.