changeSet0.setDriftHandlingMode(DriftConfigurationDefinition.DriftHandlingMode.normal);
final JPADriftFile driftFile1 = new JPADriftFile(NAME_PREFIX + "a1b2c3");
final JPADriftFile driftFile2 = new JPADriftFile(NAME_PREFIX + "1a2b3c");
JPADrift drift1 = new JPADrift(changeSet0, "drift.1", FILE_ADDED, null, driftFile1);
JPADrift drift2 = new JPADrift(changeSet0, "drift.2", FILE_ADDED, null, driftFile2);
final JPADriftSet driftSet = new JPADriftSet();
driftSet.addDrift(drift1);
driftSet.addDrift(drift2);
// create the template
final DriftDefinition templateDef = new DriftDefinition(new Configuration());
templateDef.setName(NAME_PREFIX + "createUnpinnedDefinition");
templateDef.setEnabled(true);
templateDef.setDriftHandlingMode(normal);
templateDef.setInterval(2400L);
templateDef.setBasedir(new DriftDefinition.BaseDirectory(fileSystem, "/foo/bar/test"));
templateDef.setPinned(true);
final DriftDefinitionTemplate template = templateMgr.createTemplate(getOverlord(), resourceType.getId(), true,
templateDef);
executeInTransaction(false, new TransactionCallback() {
@Override
public void execute() throws Exception {
EntityManager em = getEntityManager();
em.persist(driftFile1);
em.persist(driftFile2);
em.persist(changeSet0);
em.persist(driftSet);
changeSet0.setInitialDriftSet(driftSet);
em.merge(changeSet0);
// setting the change set id on the template is the last and the
// most important step in making the template pinned
template.setChangeSetId(changeSet0.getId());
em.merge(template);
}
});
// Create and persist a resource-level definition.
final DriftDefinition definition = template.createDefinition();
definition.setTemplate(template);
final AtomicBoolean agentInvoked = new AtomicBoolean(false);
agentServiceContainer.driftService = new TestDefService() {
@Override
public void updateDriftDetection(int resourceId, DriftDefinition driftDef, DriftSnapshot snapshot) {
try {
HibernateDetachUtility.nullOutUninitializedFields(driftDef,
HibernateDetachUtility.SerializationType.SERIALIZATION);
HibernateDetachUtility.nullOutUninitializedFields(snapshot,
HibernateDetachUtility.SerializationType.SERIALIZATION);
agentInvoked.set(true);
assertNotNull("Expected snapshot drift instances collection to be non-null",
snapshot.getDriftInstances());
assertEquals("Expected snapshot to contain two drift entries", 2, snapshot.getDriftInstances()
.size());
} catch (Exception e) {
String msg = "Do not pass attached entites to agent since those entities are outside of "
+ "Hibernate's control. The persistence context should be flushed and cleared to ensure that "
+ "only detached objects are sent to the agent";
throw new RuntimeException(msg, e);
}
}
};
driftMgr.updateDriftDefinition(getOverlord(), EntityContext.forResource(resource.getId()), definition);
DriftDefinition newDef = loadDefinition(definition.getName());
// verify that the definition is marked as pinned
assertTrue("The drift definition should be marked as pinned", newDef.isPinned());
// verify that the initial change set is generated for the definition
JPADriftChangeSetCriteria criteria = new JPADriftChangeSetCriteria();
criteria.addFilterDriftDefinitionId(definition.getId());
criteria.addFilterCategory(COVERAGE);
criteria.fetchDrifts(true);
PageList<? extends DriftChangeSet<?>> changeSets = driftMgr.findDriftChangeSetsByCriteria(getOverlord(),
criteria);
assertEquals("Expected to find one change set", 1, changeSets.size());
JPADriftChangeSet expectedChangeSet = new JPADriftChangeSet(resource, 1, COVERAGE, null);
List<? extends Drift> expectedDrifts = asList(new JPADrift(expectedChangeSet, drift1.getPath(), FILE_ADDED,
null, driftFile1), new JPADrift(expectedChangeSet, drift2.getPath(), FILE_ADDED, null, driftFile2));
DriftChangeSet<?> actualChangeSet = changeSets.get(0);
List<? extends Drift> actualDrifts = new ArrayList(actualChangeSet.getDrifts());
AssertUtils.assertCollectionMatchesNoOrder(