Added tests for entityUpdated event for entities upated using PUT

This commit is contained in:
sureshms 2021-11-10 14:41:42 -08:00
parent 5810c33930
commit 87ce20cf79
10 changed files with 58 additions and 112 deletions

View File

@ -82,14 +82,14 @@ public class CatalogApplication extends Application<CatalogApplicationConfig> {
final JdbiFactory factory = new JdbiFactory(); final JdbiFactory factory = new JdbiFactory();
final Jdbi jdbi = factory.build(environment, catalogConfig.getDataSourceFactory(), "mysql3"); final Jdbi jdbi = factory.build(environment, catalogConfig.getDataSourceFactory(), "mysql3");
SqlLogger sqlLogger = new SqlLogger() { // SqlLogger sqlLogger = new SqlLogger() {
@Override // @Override
public void logAfterExecution(StatementContext context) { // public void logAfterExecution(StatementContext context) {
LOG.info("sql {}, parameters {}, timeTaken {} ms", context.getRenderedSql(), // LOG.info("sql {}, parameters {}, timeTaken {} ms", context.getRenderedSql(),
context.getBinding().toString(), context.getElapsedTime(ChronoUnit.MILLIS)); // context.getBinding().toString(), context.getElapsedTime(ChronoUnit.MILLIS));
} // }
}; // };
jdbi.setSqlLogger(sqlLogger); // jdbi.setSqlLogger(sqlLogger);
// Register Authorizer // Register Authorizer

View File

@ -82,7 +82,6 @@ public final class Entity {
public static void registerEntity(String entity, EntityDAO<?> dao, public static void registerEntity(String entity, EntityDAO<?> dao,
EntityRepository<?> entityRepository) { EntityRepository<?> entityRepository) {
System.out.println("Registering entity " + entity);
daoMap.put(entity.toLowerCase(Locale.ROOT), dao); daoMap.put(entity.toLowerCase(Locale.ROOT), dao);
entityRepositoryMap.put(entity, entityRepository); entityRepositoryMap.put(entity, entityRepository);
} }

View File

@ -58,7 +58,6 @@ public class ChangeEventHandler implements EventHandler {
if (responseCode == Status.CREATED.getStatusCode()) { if (responseCode == Status.CREATED.getStatusCode()) {
EntityInterface entityInterface = Entity.getEntityInterface(entity); EntityInterface entityInterface = Entity.getEntityInterface(entity);
EntityReference entityReference = Entity.getEntityReference(entity); EntityReference entityReference = Entity.getEntityReference(entity);
System.out.println("Entity created at " + entityInterface.getUpdatedAt().getTime());
changeEvent = new ChangeEvent() changeEvent = new ChangeEvent()
.withEventType(EventType.ENTITY_CREATED) .withEventType(EventType.ENTITY_CREATED)
.withEntityId(entityInterface.getId()) .withEntityId(entityInterface.getId())
@ -72,7 +71,6 @@ public class ChangeEventHandler implements EventHandler {
} else if (changeType.equals(RestUtil.ENTITY_UPDATED)) { } else if (changeType.equals(RestUtil.ENTITY_UPDATED)) {
EntityInterface entityInterface = Entity.getEntityInterface(entity); EntityInterface entityInterface = Entity.getEntityInterface(entity);
EntityReference entityReference = Entity.getEntityReference(entity); EntityReference entityReference = Entity.getEntityReference(entity);
System.out.println("Entity updated at " + entityInterface.getUpdatedAt().getTime());
changeEvent = new ChangeEvent() changeEvent = new ChangeEvent()
.withEventType(EventType.ENTITY_UPDATED) .withEventType(EventType.ENTITY_UPDATED)
.withEntityId(entityInterface.getId()) .withEntityId(entityInterface.getId())
@ -91,9 +89,6 @@ public class ChangeEventHandler implements EventHandler {
if (changeEvent != null) { if (changeEvent != null) {
dao.changeEventDAO().insert(JsonUtils.pojoToJson(changeEvent)); dao.changeEventDAO().insert(JsonUtils.pojoToJson(changeEvent));
System.out.println("Adding change event " + changeEvent);
} else {
System.out.println("Change event not recorded");
} }
} catch(Exception e) { } catch(Exception e) {
LOG.error("Failed to capture change event for method {} due to {}", method, e); LOG.error("Failed to capture change event for method {} due to {}", method, e);

View File

@ -484,10 +484,8 @@ public abstract class EntityRepository<T> {
JsonUtils.pojoToJson(original.getEntity())); JsonUtils.pojoToJson(original.getEntity()));
// Store the new version // Store the new version
System.out.println("Storing new version");
EntityRepository.this.store(updated.getEntity(), true); EntityRepository.this.store(updated.getEntity(), true);
} else { } else {
System.out.println("Restoring old version");
updated.setUpdateDetails(original.getUpdatedBy(), original.getUpdatedAt()); updated.setUpdateDetails(original.getUpdatedBy(), original.getUpdatedAt());
} }
} }

View File

@ -620,7 +620,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
validateCreatedEntity(getEntity, create, authHeaders); validateCreatedEntity(getEntity, create, authHeaders);
// Validate that change event was created // Validate that change event was created
validateChangeEvents(entityInterface, EventType.ENTITY_CREATED, authHeaders); validateChangeEvents(entityInterface, EventType.ENTITY_CREATED, null, authHeaders);
return entity; return entity;
} }
@ -643,7 +643,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
if (updateType != NO_CHANGE) { if (updateType != NO_CHANGE) {
EventType expectedEventType = updateType == UpdateType.CREATED ? EventType expectedEventType = updateType == UpdateType.CREATED ?
EventType.ENTITY_CREATED : EventType.ENTITY_UPDATED; EventType.ENTITY_CREATED : EventType.ENTITY_UPDATED;
validateChangeEvents(entityInterface, expectedEventType, authHeaders); validateChangeEvents(entityInterface, expectedEventType, changeDescription, authHeaders);
} }
return updated; return updated;
} }
@ -717,21 +717,24 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
TestUtils.validateUpdate(expectedChange.getPreviousVersion(), updatedEntityInterface.getVersion(), updateType); TestUtils.validateUpdate(expectedChange.getPreviousVersion(), updatedEntityInterface.getVersion(), updateType);
if (updateType != UpdateType.NO_CHANGE) { if (updateType != UpdateType.NO_CHANGE) {
ChangeDescription change = updatedEntityInterface.getChangeDescription(); assertChangeDescription(expectedChange, updatedEntityInterface.getChangeDescription());
assertEquals(expectedChange.getPreviousVersion(), change.getPreviousVersion());
assertFieldLists(expectedChange.getFieldsAdded(), change.getFieldsAdded());
assertFieldLists(expectedChange.getFieldsUpdated(), change.getFieldsUpdated());
assertFieldLists(expectedChange.getFieldsDeleted(), change.getFieldsDeleted());
} }
} }
private void assertChangeDescription(ChangeDescription expected, ChangeDescription actual) throws IOException {
assertEquals(expected.getPreviousVersion(), actual.getPreviousVersion());
assertFieldLists(expected.getFieldsAdded(), actual.getFieldsAdded());
assertFieldLists(expected.getFieldsUpdated(), actual.getFieldsUpdated());
assertFieldLists(expected.getFieldsDeleted(), actual.getFieldsDeleted());
}
/** /**
* This method validates the change event created after POST, PUT, and PATCH operations * 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 * and ensures entityCreate, entityUpdated, and entityDeleted change events are created in the system with
* valid date. * valid date.
*/ */
protected final void validateChangeEvents(EntityInterface<T> entityInterface, EventType expectedEventType, protected final void validateChangeEvents(EntityInterface<T> entityInterface, EventType expectedEventType,
ChangeDescription expectedChangeDescription,
Map<String, String> authHeaders) throws IOException { Map<String, String> authHeaders) throws IOException {
ResultList<ChangeEvent> changeEvents = getChangeEvents(entityName, entityName, null, ResultList<ChangeEvent> changeEvents = getChangeEvents(entityName, entityName, null,
entityInterface.getUpdatedAt(), authHeaders); entityInterface.getUpdatedAt(), authHeaders);
@ -741,12 +744,13 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
// Top most changeEvent corresponds to the update // Top most changeEvent corresponds to the update
ChangeEvent changeEvent = changeEvents.getData().get(0); ChangeEvent changeEvent = changeEvents.getData().get(0);
assertEquals(expectedEventType, changeEvent.getEventType()); assertEquals(expectedEventType, changeEvent.getEventType());
assertEquals(entityName, changeEvent.getEntityType());
assertEquals(entityInterface.getId(), changeEvent.getEntityId());
assertEquals(entityInterface.getVersion(), changeEvent.getCurrentVersion());
assertEquals(entityInterface.getUpdatedBy(), changeEvent.getUserName());
assertEquals(entityInterface.getUpdatedAt().getTime(), changeEvent.getDateTime().getTime());
assertEquals(changeEvent.getDateTime().getTime(), entityInterface.getUpdatedAt().getTime()); // previous, entity, changeDescription
assertEquals(changeEvent.getEntityId(), entityInterface.getId());
assertEquals(changeEvent.getCurrentVersion(), entityInterface.getVersion());
assertEquals(changeEvent.getUserName(), entityInterface.getUpdatedBy());
assertEquals(changeEvent.getEntityType(), entityName);
if (expectedEventType == EventType.ENTITY_CREATED) { if (expectedEventType == EventType.ENTITY_CREATED) {
assertEquals(changeEvent.getEventType(), EventType.ENTITY_CREATED); assertEquals(changeEvent.getEventType(), EventType.ENTITY_CREATED);
@ -755,9 +759,12 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
compareEntities(entityInterface.getEntity(), compareEntities(entityInterface.getEntity(),
JsonUtils.readValue((String)changeEvent.getEntity(), entityClass), authHeaders); JsonUtils.readValue((String)changeEvent.getEntity(), entityClass), authHeaders);
} else if (expectedEventType == EventType.ENTITY_UPDATED) { } else if (expectedEventType == EventType.ENTITY_UPDATED) {
// TODO assertNull(changeEvent.getEntity());
assertChangeDescription(expectedChangeDescription, changeEvent.getChangeDescription());
assertEquals(entityInterface.getChangeDescription().getPreviousVersion(), changeEvent.getPreviousVersion());
} else if (expectedEventType == EventType.ENTITY_DELETED) { } else if (expectedEventType == EventType.ENTITY_DELETED) {
assertNull(changeEvent.getEntity());
assertNull(changeEvent.getChangeDescription());
} }
} }
@ -851,15 +858,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
throw new IllegalArgumentException("Invalid owner type " + owner.getType()); throw new IllegalArgumentException("Invalid owner type " + owner.getType());
} }
boolean owning = false; TestUtils.existsInEntityReferenceList(ownsList, entityId, expectedOwning);
for (EntityReference owns : ownsList) {
TestUtils.validateEntityReference(owns);
if (owns.getId().equals(entityId)) {
owning = true;
break;
}
}
assertEquals(expectedOwning, owning, "Ownership not correct in the owns list for " + owner.getType());
} }
} }
@ -875,14 +874,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
assertEquals(totalFollowerCount, followers.size()); assertEquals(totalFollowerCount, followers.size());
TestUtils.validateEntityReference(followers); TestUtils.validateEntityReference(followers);
boolean followerFound = false; TestUtils.existsInEntityReferenceList(followers, userId, true);
for (EntityReference follower : followers) {
if (follower.getId().equals(userId)) {
followerFound = true;
break;
}
}
assertTrue(followerFound, "Follower added was not found in " + entityClass + " get response");
// GET .../users/{userId} shows user as following the entity // GET .../users/{userId} shows user as following the entity
checkUserFollowing(userId, entityId, true, authHeaders); checkUserFollowing(userId, entityId, true, authHeaders);
@ -905,14 +897,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
T getEntity = getEntity(entityId, authHeaders); T getEntity = getEntity(entityId, authHeaders);
List<EntityReference> followers = getEntityInterface(getEntity).getFollowers(); List<EntityReference> followers = getEntityInterface(getEntity).getFollowers();
TestUtils.validateEntityReference(followers); TestUtils.validateEntityReference(followers);
boolean followerFound = false; TestUtils.existsInEntityReferenceList(followers, userId, false);
for (EntityReference follower : followers) {
if (follower.getId().equals(userId)) {
followerFound = true;
break;
}
}
assertFalse(followerFound, "Follower deleted is still found in " + entityClass + " get response");
// GET .../users/{userId} shows user as following the entity // GET .../users/{userId} shows user as following the entity
checkUserFollowing(userId, entityId, false, authHeaders); checkUserFollowing(userId, entityId, false, authHeaders);
@ -941,4 +926,5 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest {
list.getData().forEach(entity -> LOG.info("{} {}", entityClass, getEntityInterface(entity).getFullyQualifiedName())); list.getData().forEach(entity -> LOG.info("{} {}", entityClass, getEntityInterface(entity).getFullyQualifiedName()));
LOG.info("before {} after {} ", list.getPaging().getBefore(), list.getPaging().getAfter()); LOG.info("before {} after {} ", list.getPaging().getBefore(), list.getPaging().getAfter());
} }
} }

View File

@ -281,10 +281,9 @@ public class TableResourceTest extends EntityResourceTest<Table> {
// Test PUT operation - put operation to create // Test PUT operation - put operation to create
CreateTable create2 = create(test, 2).withColumns(Arrays.asList(c1, c2)).withName("put_complexColumnType"); CreateTable create2 = create(test, 2).withColumns(Arrays.asList(c1, c2)).withName("put_complexColumnType");
System.out.println("Put to create");
Table table2 = updateAndCheckEntity(create2, CREATED, adminAuthHeaders(), UpdateType.CREATED, null); Table table2 = updateAndCheckEntity(create2, CREATED, adminAuthHeaders(), UpdateType.CREATED, null);
// Test PUT operation again without any change // Test PUT operation again without any change
System.out.println("Put with no change");
ChangeDescription change = getChangeDescription(table2.getVersion()); ChangeDescription change = getChangeDescription(table2.getVersion());
updateAndCheckEntity(create2, Status.OK, adminAuthHeaders(), NO_CHANGE, change); updateAndCheckEntity(create2, Status.OK, adminAuthHeaders(), NO_CHANGE, change);

View File

@ -71,6 +71,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openmetadata.catalog.exception.CatalogExceptionMessage.entityNotFound; import static org.openmetadata.catalog.exception.CatalogExceptionMessage.entityNotFound;
import static org.openmetadata.catalog.exception.CatalogExceptionMessage.readOnlyAttribute; import static org.openmetadata.catalog.exception.CatalogExceptionMessage.readOnlyAttribute;
import static org.openmetadata.catalog.util.TestUtils.existsInEntityReferenceList;
import static org.openmetadata.catalog.util.TestUtils.NON_EXISTENT_ENTITY; import static org.openmetadata.catalog.util.TestUtils.NON_EXISTENT_ENTITY;
import static org.openmetadata.catalog.util.TestUtils.adminAuthHeaders; import static org.openmetadata.catalog.util.TestUtils.adminAuthHeaders;
import static org.openmetadata.catalog.util.TestUtils.assertEntityPagination; import static org.openmetadata.catalog.util.TestUtils.assertEntityPagination;
@ -745,14 +746,7 @@ public class LocationResourceTest extends CatalogApplicationTest {
// GET .../users/{userId} shows user as following location // GET .../users/{userId} shows user as following location
boolean following = false; boolean following = false;
User user = UserResourceTest.getUser(userId, "follows", authHeaders); User user = UserResourceTest.getUser(userId, "follows", authHeaders);
for (EntityReference follows : user.getFollows()) { existsInEntityReferenceList(user.getFollows(), locationId, expectedFollowing);
TestUtils.validateEntityReference(follows);
if (follows.getId().equals(locationId)) {
following = true;
break;
}
}
assertEquals(expectedFollowing, following, "Follower list for the user is invalid");
} }
private void deleteAndCheckFollower(Location location, UUID userId, int totalFollowerCount, private void deleteAndCheckFollower(Location location, UUID userId, int totalFollowerCount,
@ -769,14 +763,7 @@ public class LocationResourceTest extends CatalogApplicationTest {
throws HttpResponseException { throws HttpResponseException {
Location getLocation = getLocation(locationId, "followers", authHeaders); Location getLocation = getLocation(locationId, "followers", authHeaders);
TestUtils.validateEntityReference(getLocation.getFollowers()); TestUtils.validateEntityReference(getLocation.getFollowers());
boolean followerFound = false; existsInEntityReferenceList(getLocation.getFollowers(), locationId, false);
for (EntityReference followers : getLocation.getFollowers()) {
if (followers.getId().equals(userId)) {
followerFound = true;
break;
}
}
assertFalse(followerFound, "Follower deleted is still found in location get response");
// GET .../users/{userId} shows user as following location // GET .../users/{userId} shows user as following location
checkUserFollowing(userId, locationId, false, authHeaders); checkUserFollowing(userId, locationId, false, authHeaders);

View File

@ -45,7 +45,6 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -348,14 +347,7 @@ public class TeamResourceTest extends EntityResourceTest<Team> {
assertEquals(expectedUsers.size(), team.getUsers().size()); assertEquals(expectedUsers.size(), team.getUsers().size());
for (EntityReference user : team.getUsers()) { for (EntityReference user : team.getUsers()) {
TestUtils.validateEntityReference(user); TestUtils.validateEntityReference(user);
boolean foundUser = false; TestUtils.existsInEntityReferenceList(expectedUsers, user.getId(), true);
for (EntityReference expected : expectedUsers) {
if (expected.getId().equals(user.getId())) {
foundUser = true;
break;
}
}
assertTrue(foundUser);
} }
} }
TestUtils.validateEntityReference(team.getOwns()); TestUtils.validateEntityReference(team.getOwns());
@ -431,16 +423,8 @@ public class TeamResourceTest extends EntityResourceTest<Team> {
List<EntityReference> actualUsers = Optional.ofNullable(team.getUsers()).orElse(Collections.emptyList()); List<EntityReference> actualUsers = Optional.ofNullable(team.getUsers()).orElse(Collections.emptyList());
if (!expectedUsers.isEmpty()) { if (!expectedUsers.isEmpty()) {
assertEquals(expectedUsers.size(), actualUsers.size()); assertEquals(expectedUsers.size(), actualUsers.size());
for (EntityReference user : actualUsers) { for (EntityReference user : expectedUsers) {
TestUtils.validateEntityReference(user); TestUtils.existsInEntityReferenceList(actualUsers, user.getId(), true);
boolean foundUser = false;
for (EntityReference expectedEntity : expectedUsers) {
if (expectedEntity.getId().equals(user.getId())) {
foundUser = true;
break;
}
}
assertTrue(foundUser);
} }
} }
TestUtils.validateEntityReference(team.getOwns()); TestUtils.validateEntityReference(team.getOwns());

View File

@ -539,16 +539,8 @@ public class UserResourceTest extends EntityResourceTest<User> {
if (!expectedTeams.isEmpty()) { if (!expectedTeams.isEmpty()) {
assertEquals(expectedTeams.size(), user.getTeams().size()); assertEquals(expectedTeams.size(), user.getTeams().size());
for (EntityReference team : user.getTeams()) { for (EntityReference team : expectedTeams) {
TestUtils.validateEntityReference(team); TestUtils.existsInEntityReferenceList(user.getTeams(), team.getId(), true);
boolean foundTeam = false;
for (EntityReference expected : expectedTeams) {
if (expected.getId().equals(team.getId())) {
foundTeam = true;
break;
}
}
assertTrue(foundTeam);
} }
} }
if (createRequest.getProfile() != null) { if (createRequest.getProfile() != null) {

View File

@ -39,7 +39,6 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -274,14 +273,7 @@ public final class TestUtils {
// GET .../users/{userId} shows user as following table // GET .../users/{userId} shows user as following table
boolean following = false; boolean following = false;
User user = UserResourceTest.getUser(userId, "follows", authHeaders); User user = UserResourceTest.getUser(userId, "follows", authHeaders);
for (EntityReference follows : user.getFollows()) { existsInEntityReferenceList(user.getFollows(), entityId, expectedFollowing);
TestUtils.validateEntityReference(follows);
if (follows.getId().equals(entityId)) {
following = true;
break;
}
}
assertEquals(expectedFollowing, following, "Follower list for the user is invalid");
} }
public static String getPrincipal(Map<String, String> authHeaders) { public static String getPrincipal(Map<String, String> authHeaders) {
@ -305,4 +297,18 @@ public final class TestUtils {
assertEquals(Math.round((previousVersion + 1.0) * 10.0)/10.0, newVersion); // Major version change assertEquals(Math.round((previousVersion + 1.0) * 10.0)/10.0, newVersion); // Major version change
} }
} }
public static void existsInEntityReferenceList(List<EntityReference> list, UUID id, boolean expectedExistsInList) {
boolean exists = false;
for (EntityReference ref : list) {
validateEntityReference(ref);
if (ref.getId().equals(id)) {
exists = true;
break;
}
}
assertEquals(expectedExistsInList, exists,
"Entry exists in list - expected:" + expectedExistsInList + " actual:" + exists);
}
} }