From aa83f7b1ad9110978302cd67a2b5d291b9e16e29 Mon Sep 17 00:00:00 2001 From: sureshms Date: Wed, 10 Nov 2021 13:25:25 -0800 Subject: [PATCH] Fixing test failures due to code merge --- .../operations/IngestionResource.java | 25 +++++++++++++------ .../catalog/resources/EntityResourceTest.java | 22 +++++++++++++--- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/operations/IngestionResource.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/operations/IngestionResource.java index 2b32e8295ff..3db669d511b 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/operations/IngestionResource.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/operations/IngestionResource.java @@ -68,9 +68,11 @@ import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.text.ParseException; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.UUID; @Path("operations/v1/ingestion") @@ -79,18 +81,22 @@ import java.util.UUID; @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "ingestion") public class IngestionResource { - public static final String INGESTION_COLLECTION_PATH = "operations/v1/ingestion/"; + public static final String COLLECTION_PATH = "operations/v1/ingestion/"; private final IngestionRepository dao; private final CatalogAuthorizer authorizer; private AirflowRESTClient airflowRESTClient; private CatalogApplicationConfig config; public static void addHref(UriInfo uriInfo, EntityReference ref) { - ref.withHref(RestUtil.getHref(uriInfo, INGESTION_COLLECTION_PATH, ref.getId())); + ref.withHref(RestUtil.getHref(uriInfo, COLLECTION_PATH, ref.getId())); + } + + public static List addHref(UriInfo uriInfo, List ingestions) { + Optional.ofNullable(ingestions).orElse(Collections.emptyList()).forEach(i -> addHref(uriInfo, i)); + return ingestions; } public static Ingestion addHref(UriInfo uriInfo, Ingestion ingestion) { - ingestion.setHref(RestUtil.getHref(uriInfo, INGESTION_COLLECTION_PATH, ingestion.getId())); Entity.withHref(uriInfo, ingestion.getOwner()); Entity.withHref(uriInfo, ingestion.getService()); return ingestion; @@ -162,6 +168,7 @@ public class IngestionResource { } else { // Forward paging or first page ingestions = dao.listAfter(uriInfo, fields, null, limitParam, after); } + addHref(uriInfo, ingestions.getData()); return ingestions; } @@ -198,7 +205,7 @@ public class IngestionResource { schema = @Schema(type = "string", example = FIELDS)) @QueryParam("fields") String fieldsParam) throws IOException, ParseException { Fields fields = new Fields(FIELD_LIST, fieldsParam); - return dao.get(uriInfo, id, fields); + return addHref(uriInfo, dao.get(uriInfo, id, fields)); } @GET @@ -238,7 +245,7 @@ public class IngestionResource { schema = @Schema(type = "string", example = FIELDS)) @QueryParam("fields") String fieldsParam) throws IOException, ParseException { Fields fields = new Fields(FIELD_LIST, fieldsParam); - return dao.getByName(uriInfo, fqn, fields); + return addHref(uriInfo, dao.getByName(uriInfo, fqn, fields)); } @@ -257,7 +264,7 @@ public class IngestionResource { Ingestion ingestion = getIngestion(securityContext, create); deploy(ingestion); // write to db only when the deployment is successful - ingestion = dao.create(uriInfo, ingestion); + ingestion = addHref(uriInfo, dao.create(uriInfo, ingestion)); return Response.created(ingestion.getHref()).entity(ingestion).build(); } @@ -282,7 +289,8 @@ public class IngestionResource { Ingestion ingestion = dao.get(uriInfo, id, fields); SecurityUtil.checkAdminRoleOrPermissions(authorizer, securityContext, dao.getOwnerReference(ingestion)); - return dao.patch(uriInfo, UUID.fromString(id), securityContext.getUserPrincipal().getName(), patch); + ingestion = dao.patch(uriInfo, UUID.fromString(id), securityContext.getUserPrincipal().getName(), patch); + return addHref(uriInfo, ingestion); } @PUT @@ -301,6 +309,7 @@ public class IngestionResource { deploy(ingestion); // write to db only when the deployment is successful PutResponse response = dao.createOrUpdate(uriInfo, ingestion); + addHref(uriInfo, response.getEntity()); return response.toResponse(); } @@ -319,7 +328,7 @@ public class IngestionResource { Fields fields = new Fields(FIELD_LIST, ""); Ingestion ingestion = dao.get(uriInfo, id, fields); airflowRESTClient.runPipeline(ingestion.getName()); - return dao.get(uriInfo, id, fields); + return addHref(uriInfo, dao.get(uriInfo, id, fields)); } diff --git a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/EntityResourceTest.java b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/EntityResourceTest.java index 7f07da8ce0c..622eb8c34b9 100644 --- a/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/EntityResourceTest.java +++ b/catalog-rest-service/src/test/java/org/openmetadata/catalog/resources/EntityResourceTest.java @@ -636,7 +636,13 @@ public abstract class EntityResourceTest extends CatalogApplicationTest { T getEntity = getEntity(entityInterface.getId(), authHeaders); validateUpdatedEntity(getEntity, request, authHeaders); validateChangeDescription(getEntity, updateType, changeDescription); - validateChangeEvents(entityInterface, updateType, authHeaders); + + // Check if the entity change events are record + if (updateType != NO_CHANGE) { + EventType expectedEventType = updateType == UpdateType.CREATED ? + EventType.ENTITY_CREATED : EventType.ENTITY_UPDATED; + validateChangeEvents(entityInterface, expectedEventType, authHeaders); + } return updated; } @@ -718,7 +724,12 @@ public abstract class EntityResourceTest extends CatalogApplicationTest { } } - protected final void validateChangeEvents(EntityInterface entityInterface, UpdateType updateType, + /** + * This method validates the change event created after POST, PUT, and PATCH operations + * and ensures entityCreate, entityUpdated, and entityDeleted change events are created in the system with + * valid date. + */ + protected final void validateChangeEvents(EntityInterface entityInterface, EventType expectedEventType, Map authHeaders) throws IOException { ResultList changeEvents = getChangeEvents(entityName, entityName, null, entityInterface.getUpdatedAt(), authHeaders); @@ -727,6 +738,7 @@ public abstract class EntityResourceTest extends CatalogApplicationTest { // Top most changeEvent corresponds to the update ChangeEvent changeEvent = changeEvents.getData().get(0); + assertEquals(expectedEventType, changeEvent.getEventType()); assertEquals(changeEvent.getDateTime().getTime(), entityInterface.getUpdatedAt().getTime()); assertEquals(changeEvent.getEntityId(), entityInterface.getId()); @@ -734,13 +746,15 @@ public abstract class EntityResourceTest extends CatalogApplicationTest { assertEquals(changeEvent.getUserName(), entityInterface.getUpdatedBy()); assertEquals(changeEvent.getEntityType(), entityName); - if (updateType == UpdateType.CREATED) { + if (expectedEventType == EventType.ENTITY_CREATED) { assertEquals(changeEvent.getEventType(), EventType.ENTITY_CREATED); assertEquals(changeEvent.getPreviousVersion(), 0.1); assertNull(changeEvent.getChangeDescription()); compareEntities(entityInterface.getEntity(), JsonUtils.readValue((String)changeEvent.getEntity(), entityClass), authHeaders); - } else if (updateType == MINOR_UPDATE) { + } else if (expectedEventType == EventType.ENTITY_UPDATED) { + // TODO + } else if (expectedEventType == EventType.ENTITY_DELETED) { } }