mirror of
				https://github.com/open-metadata/OpenMetadata.git
				synced 2025-10-31 02:29:03 +00:00 
			
		
		
		
	Added tests for entityUpdated event for entities upated using PUT
This commit is contained in:
		
							parent
							
								
									5810c33930
								
							
						
					
					
						commit
						87ce20cf79
					
				| @ -82,14 +82,14 @@ public class CatalogApplication extends Application<CatalogApplicationConfig> { | ||||
|     final JdbiFactory factory = new JdbiFactory(); | ||||
|     final Jdbi jdbi = factory.build(environment, catalogConfig.getDataSourceFactory(), "mysql3"); | ||||
| 
 | ||||
|     SqlLogger sqlLogger = new SqlLogger() { | ||||
|       @Override | ||||
|       public void logAfterExecution(StatementContext context) { | ||||
|         LOG.info("sql {}, parameters {}, timeTaken {} ms", context.getRenderedSql(), | ||||
|                 context.getBinding().toString(), context.getElapsedTime(ChronoUnit.MILLIS)); | ||||
|       } | ||||
|     }; | ||||
|     jdbi.setSqlLogger(sqlLogger); | ||||
| //    SqlLogger sqlLogger = new SqlLogger() { | ||||
| //      @Override | ||||
| //      public void logAfterExecution(StatementContext context) { | ||||
| //        LOG.info("sql {}, parameters {}, timeTaken {} ms", context.getRenderedSql(), | ||||
| //                context.getBinding().toString(), context.getElapsedTime(ChronoUnit.MILLIS)); | ||||
| //      } | ||||
| //    }; | ||||
| //    jdbi.setSqlLogger(sqlLogger); | ||||
| 
 | ||||
| 
 | ||||
|     // Register Authorizer | ||||
|  | ||||
| @ -82,7 +82,6 @@ public final class Entity { | ||||
| 
 | ||||
|   public static void registerEntity(String entity, EntityDAO<?> dao, | ||||
|                                     EntityRepository<?> entityRepository) { | ||||
|     System.out.println("Registering entity " + entity); | ||||
|     daoMap.put(entity.toLowerCase(Locale.ROOT), dao); | ||||
|     entityRepositoryMap.put(entity, entityRepository); | ||||
|   } | ||||
|  | ||||
| @ -58,7 +58,6 @@ public class ChangeEventHandler implements  EventHandler { | ||||
|         if (responseCode == Status.CREATED.getStatusCode()) { | ||||
|           EntityInterface entityInterface = Entity.getEntityInterface(entity); | ||||
|           EntityReference entityReference = Entity.getEntityReference(entity); | ||||
|           System.out.println("Entity created at " + entityInterface.getUpdatedAt().getTime()); | ||||
|           changeEvent = new ChangeEvent() | ||||
|                   .withEventType(EventType.ENTITY_CREATED) | ||||
|                   .withEntityId(entityInterface.getId()) | ||||
| @ -72,7 +71,6 @@ public class ChangeEventHandler implements  EventHandler { | ||||
|         } else if (changeType.equals(RestUtil.ENTITY_UPDATED)) { | ||||
|           EntityInterface entityInterface = Entity.getEntityInterface(entity); | ||||
|           EntityReference entityReference = Entity.getEntityReference(entity); | ||||
|           System.out.println("Entity updated at " + entityInterface.getUpdatedAt().getTime()); | ||||
|           changeEvent = new ChangeEvent() | ||||
|                   .withEventType(EventType.ENTITY_UPDATED) | ||||
|                   .withEntityId(entityInterface.getId()) | ||||
| @ -91,9 +89,6 @@ public class ChangeEventHandler implements  EventHandler { | ||||
| 
 | ||||
|         if (changeEvent != null) { | ||||
|           dao.changeEventDAO().insert(JsonUtils.pojoToJson(changeEvent)); | ||||
|           System.out.println("Adding change event " + changeEvent); | ||||
|         } else { | ||||
|           System.out.println("Change event not recorded"); | ||||
|         } | ||||
|       } catch(Exception e) { | ||||
|         LOG.error("Failed to capture change event for method {} due to {}", method, e); | ||||
|  | ||||
| @ -484,10 +484,8 @@ public abstract class EntityRepository<T> { | ||||
|                 JsonUtils.pojoToJson(original.getEntity())); | ||||
| 
 | ||||
|         // Store the new version | ||||
|         System.out.println("Storing new version"); | ||||
|         EntityRepository.this.store(updated.getEntity(), true); | ||||
|       } else { | ||||
|         System.out.println("Restoring old version"); | ||||
|         updated.setUpdateDetails(original.getUpdatedBy(), original.getUpdatedAt()); | ||||
|       } | ||||
|     } | ||||
|  | ||||
| @ -620,7 +620,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
|     validateCreatedEntity(getEntity, create, authHeaders); | ||||
| 
 | ||||
|     // Validate that change event was created | ||||
|     validateChangeEvents(entityInterface, EventType.ENTITY_CREATED, authHeaders); | ||||
|     validateChangeEvents(entityInterface, EventType.ENTITY_CREATED, null, authHeaders); | ||||
|     return entity; | ||||
|   } | ||||
| 
 | ||||
| @ -643,7 +643,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
|     if (updateType != NO_CHANGE) { | ||||
|       EventType expectedEventType = updateType == UpdateType.CREATED ? | ||||
|               EventType.ENTITY_CREATED : EventType.ENTITY_UPDATED; | ||||
|       validateChangeEvents(entityInterface, expectedEventType, authHeaders); | ||||
|       validateChangeEvents(entityInterface, expectedEventType, changeDescription, authHeaders); | ||||
|     } | ||||
|     return updated; | ||||
|   } | ||||
| @ -717,21 +717,24 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
|     TestUtils.validateUpdate(expectedChange.getPreviousVersion(), updatedEntityInterface.getVersion(), updateType); | ||||
| 
 | ||||
|     if (updateType != UpdateType.NO_CHANGE) { | ||||
|       ChangeDescription change = updatedEntityInterface.getChangeDescription(); | ||||
|       assertEquals(expectedChange.getPreviousVersion(), change.getPreviousVersion()); | ||||
| 
 | ||||
|       assertFieldLists(expectedChange.getFieldsAdded(), change.getFieldsAdded()); | ||||
|       assertFieldLists(expectedChange.getFieldsUpdated(), change.getFieldsUpdated()); | ||||
|       assertFieldLists(expectedChange.getFieldsDeleted(), change.getFieldsDeleted()); | ||||
|       assertChangeDescription(expectedChange, updatedEntityInterface.getChangeDescription()); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   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 | ||||
|    * and ensures entityCreate, entityUpdated, and entityDeleted change events are created in the system with | ||||
|    * valid date. | ||||
|    */ | ||||
|   protected final void validateChangeEvents(EntityInterface<T> entityInterface, EventType expectedEventType, | ||||
|                                             ChangeDescription expectedChangeDescription, | ||||
|                                             Map<String, String> authHeaders) throws IOException { | ||||
|     ResultList<ChangeEvent> changeEvents = getChangeEvents(entityName, entityName, null, | ||||
|             entityInterface.getUpdatedAt(), authHeaders); | ||||
| @ -741,12 +744,13 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
|     // Top most changeEvent corresponds to the update | ||||
|     ChangeEvent changeEvent = changeEvents.getData().get(0); | ||||
|     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()); | ||||
|     assertEquals(changeEvent.getEntityId(), entityInterface.getId()); | ||||
|     assertEquals(changeEvent.getCurrentVersion(), entityInterface.getVersion()); | ||||
|     assertEquals(changeEvent.getUserName(), entityInterface.getUpdatedBy()); | ||||
|     assertEquals(changeEvent.getEntityType(), entityName); | ||||
|     // previous, entity, changeDescription | ||||
| 
 | ||||
|     if (expectedEventType == EventType.ENTITY_CREATED) { | ||||
|       assertEquals(changeEvent.getEventType(), EventType.ENTITY_CREATED); | ||||
| @ -755,9 +759,12 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
|       compareEntities(entityInterface.getEntity(), | ||||
|               JsonUtils.readValue((String)changeEvent.getEntity(), entityClass), authHeaders); | ||||
|     } 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) { | ||||
| 
 | ||||
|       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()); | ||||
|       } | ||||
| 
 | ||||
|       boolean owning = false; | ||||
|       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()); | ||||
|       TestUtils.existsInEntityReferenceList(ownsList, entityId, expectedOwning); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
| @ -875,14 +874,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
| 
 | ||||
|     assertEquals(totalFollowerCount, followers.size()); | ||||
|     TestUtils.validateEntityReference(followers); | ||||
|     boolean followerFound = false; | ||||
|     for (EntityReference follower : followers) { | ||||
|       if (follower.getId().equals(userId)) { | ||||
|         followerFound = true; | ||||
|         break; | ||||
|       } | ||||
|     } | ||||
|     assertTrue(followerFound, "Follower added was not found in " + entityClass + " get response"); | ||||
|     TestUtils.existsInEntityReferenceList(followers, userId, true); | ||||
| 
 | ||||
|     // GET .../users/{userId} shows user as following the entity | ||||
|     checkUserFollowing(userId, entityId, true, authHeaders); | ||||
| @ -905,14 +897,7 @@ public abstract class EntityResourceTest<T> extends CatalogApplicationTest { | ||||
|     T getEntity = getEntity(entityId, authHeaders); | ||||
|     List<EntityReference> followers = getEntityInterface(getEntity).getFollowers(); | ||||
|     TestUtils.validateEntityReference(followers); | ||||
|     boolean followerFound = false; | ||||
|     for (EntityReference follower : followers) { | ||||
|       if (follower.getId().equals(userId)) { | ||||
|         followerFound = true; | ||||
|         break; | ||||
|       } | ||||
|     } | ||||
|     assertFalse(followerFound, "Follower deleted is still found in " + entityClass + " get response"); | ||||
|     TestUtils.existsInEntityReferenceList(followers, userId, false); | ||||
| 
 | ||||
|     // GET .../users/{userId} shows user as following the entity | ||||
|     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())); | ||||
|     LOG.info("before {} after {} ", list.getPaging().getBefore(), list.getPaging().getAfter()); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -281,10 +281,9 @@ public class TableResourceTest extends EntityResourceTest<Table> { | ||||
| 
 | ||||
|     // Test PUT operation - put operation to create | ||||
|     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); | ||||
| 
 | ||||
|     // Test PUT operation again without any change | ||||
|     System.out.println("Put with no change"); | ||||
|     ChangeDescription change = getChangeDescription(table2.getVersion()); | ||||
|     updateAndCheckEntity(create2, Status.OK, adminAuthHeaders(), NO_CHANGE, change); | ||||
| 
 | ||||
|  | ||||
| @ -71,6 +71,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
| import static org.openmetadata.catalog.exception.CatalogExceptionMessage.entityNotFound; | ||||
| 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.adminAuthHeaders; | ||||
| 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 | ||||
|         boolean following = false; | ||||
|         User user = UserResourceTest.getUser(userId, "follows", authHeaders); | ||||
|         for (EntityReference follows : user.getFollows()) { | ||||
|             TestUtils.validateEntityReference(follows); | ||||
|             if (follows.getId().equals(locationId)) { | ||||
|                 following = true; | ||||
|                 break; | ||||
|             } | ||||
|         } | ||||
|         assertEquals(expectedFollowing, following, "Follower list for the user is invalid"); | ||||
|         existsInEntityReferenceList(user.getFollows(), locationId, expectedFollowing); | ||||
|     } | ||||
| 
 | ||||
|     private void deleteAndCheckFollower(Location location, UUID userId, int totalFollowerCount, | ||||
| @ -769,14 +763,7 @@ public class LocationResourceTest extends CatalogApplicationTest { | ||||
|             throws HttpResponseException { | ||||
|         Location getLocation = getLocation(locationId, "followers", authHeaders); | ||||
|         TestUtils.validateEntityReference(getLocation.getFollowers()); | ||||
|         boolean followerFound = 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"); | ||||
|         existsInEntityReferenceList(getLocation.getFollowers(), locationId, false); | ||||
| 
 | ||||
|         // GET .../users/{userId} shows user as following location | ||||
|         checkUserFollowing(userId, locationId, false, authHeaders); | ||||
|  | ||||
| @ -45,7 +45,6 @@ import java.net.URI; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.Collections; | ||||
| import java.util.Comparator; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Optional; | ||||
| @ -348,14 +347,7 @@ public class TeamResourceTest extends EntityResourceTest<Team> { | ||||
|       assertEquals(expectedUsers.size(), team.getUsers().size()); | ||||
|       for (EntityReference user : team.getUsers()) { | ||||
|         TestUtils.validateEntityReference(user); | ||||
|         boolean foundUser = false; | ||||
|         for (EntityReference expected : expectedUsers) { | ||||
|           if (expected.getId().equals(user.getId())) { | ||||
|             foundUser = true; | ||||
|             break; | ||||
|           } | ||||
|         } | ||||
|         assertTrue(foundUser); | ||||
|         TestUtils.existsInEntityReferenceList(expectedUsers, user.getId(), true); | ||||
|       } | ||||
|     } | ||||
|     TestUtils.validateEntityReference(team.getOwns()); | ||||
| @ -431,16 +423,8 @@ public class TeamResourceTest extends EntityResourceTest<Team> { | ||||
|     List<EntityReference> actualUsers = Optional.ofNullable(team.getUsers()).orElse(Collections.emptyList()); | ||||
|     if (!expectedUsers.isEmpty()) { | ||||
|       assertEquals(expectedUsers.size(), actualUsers.size()); | ||||
|       for (EntityReference user : actualUsers) { | ||||
|         TestUtils.validateEntityReference(user); | ||||
|         boolean foundUser = false; | ||||
|         for (EntityReference expectedEntity : expectedUsers) { | ||||
|           if (expectedEntity.getId().equals(user.getId())) { | ||||
|             foundUser = true; | ||||
|             break; | ||||
|           } | ||||
|         } | ||||
|         assertTrue(foundUser); | ||||
|       for (EntityReference user : expectedUsers) { | ||||
|         TestUtils.existsInEntityReferenceList(actualUsers, user.getId(), true); | ||||
|       } | ||||
|     } | ||||
|     TestUtils.validateEntityReference(team.getOwns()); | ||||
|  | ||||
| @ -539,16 +539,8 @@ public class UserResourceTest extends EntityResourceTest<User> { | ||||
| 
 | ||||
|     if (!expectedTeams.isEmpty()) { | ||||
|       assertEquals(expectedTeams.size(), user.getTeams().size()); | ||||
|       for (EntityReference team : user.getTeams()) { | ||||
|         TestUtils.validateEntityReference(team); | ||||
|         boolean foundTeam = false; | ||||
|         for (EntityReference expected : expectedTeams) { | ||||
|           if (expected.getId().equals(team.getId())) { | ||||
|             foundTeam = true; | ||||
|             break; | ||||
|           } | ||||
|         } | ||||
|         assertTrue(foundTeam); | ||||
|       for (EntityReference team : expectedTeams) { | ||||
|         TestUtils.existsInEntityReferenceList(user.getTeams(), team.getId(), true); | ||||
|       } | ||||
|     } | ||||
|     if (createRequest.getProfile() != null) { | ||||
|  | ||||
| @ -39,7 +39,6 @@ import java.net.URI; | ||||
| import java.net.URISyntaxException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.Comparator; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| @ -274,14 +273,7 @@ public final class TestUtils { | ||||
|     // GET .../users/{userId} shows user as following table | ||||
|     boolean following = false; | ||||
|     User user = UserResourceTest.getUser(userId, "follows", authHeaders); | ||||
|     for (EntityReference follows : user.getFollows()) { | ||||
|       TestUtils.validateEntityReference(follows); | ||||
|       if (follows.getId().equals(entityId)) { | ||||
|         following = true; | ||||
|         break; | ||||
|       } | ||||
|     } | ||||
|     assertEquals(expectedFollowing, following, "Follower list for the user is invalid"); | ||||
|     existsInEntityReferenceList(user.getFollows(), entityId, expectedFollowing); | ||||
|   } | ||||
| 
 | ||||
|   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 | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   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); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 sureshms
						sureshms