/** * Notifies listeners about environment update. * * @param old * Old environment instance. * @param updated * Updated environment instance. */ private void publishEnvironmentUpdateEvent(Environment old, Environment updated) { Collection<Profile> removedProfiles = getProfileDifference(old, updated); Collection<Profile> addedProfiles = getProfileDifference(updated, old); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, addedProfiles, removedProfiles); eventPublisher.publishEvent(event); }
/** * {@inheritDoc} */ @Override protected Collection<ImmutableType> execute() { // always update with new Environment getConfigurationHolder().update(environmentUpdateEvent.getAfter(), getAgentId()); Collection<ImmutableType> changedClassTypes = new HashSet<>(); // then process removed and added assignments changedClassTypes.addAll(super.processRemovedAssignments(environmentUpdateEvent.getRemovedSensorAssignments(functionalAssignmentFactory))); changedClassTypes.addAll(super.processAddedAssignments(environmentUpdateEvent.getAddedSensorAssignments(functionalAssignmentFactory))); return changedClassTypes; }
/** * Returns all {@link AbstractClassSensorAssignment} that are contained in the added profiles. * Only active profiles are taken into account. Also includes the functional assignments that * might be added as the result of changes in the environment. * * @param functionalAssignmentFactory * SpecialMethodSensorAssignmentFactory for resolving functional assignment updates. * * @return Returns all {@link AbstractClassSensorAssignment} that are "added". */ public Collection<AbstractClassSensorAssignment<?>> getAddedSensorAssignments(SpecialMethodSensorAssignmentFactory functionalAssignmentFactory) { Collection<AbstractClassSensorAssignment<?>> addedAssignments = getSensorAssignments(addedProfiles); addedAssignments.addAll(getFunctionalAssignmentsDifference(functionalAssignmentFactory, after, before)); return addedAssignments; }
@Test public void profileRemoved() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); AbstractClassSensorAssignment<?> assignment = mock(AbstractClassSensorAssignment.class); doReturn(Collections.singletonList(assignment)).when(profileData).getData(SensorAssignmentProfileData.class); when(profile.isActive()).thenReturn(true); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, null, Collections.singletonList(profile)); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(functionalAssignmentFactory); assertThat(removed, hasSize(1)); assertThat(removed, hasItem(assignment)); }
@Test public void profileAdded() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); AbstractClassSensorAssignment<?> assignment = mock(AbstractClassSensorAssignment.class); doReturn(Collections.singletonList(assignment)).when(profileData).getData(SensorAssignmentProfileData.class); when(profile.isActive()).thenReturn(true); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, Collections.singletonList(profile), null); Collection<AbstractClassSensorAssignment<?>> added = event.getAddedSensorAssignments(functionalAssignmentFactory); assertThat(added, hasSize(1)); assertThat(added, hasItem(assignment)); }
@Test public void environmentsAreNotEqual() { cacheMap.put(1L, cacheEntry); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getId()).thenReturn("id"); when(event.getEnvironmentId()).thenReturn("otherId"); eventListener.onApplicationEvent(event); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).isInitialized(); verify(configurationHolder).getEnvironment(); verify(environment).getId(); verify(event).getEnvironmentId(); verifyNoMoreInteractions(nextGenInstrumentationManager, cacheEntry, configurationHolder, environment, event); verifyZeroInteractions(objectFactory, updateJob, executor, future); }
@Test public void removedAssignmentNoChange() { Collection<ClassType> types = Collections.singleton(classType); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(Collections.emptyList()).when(instrumentationService).removeInstrumentationPoints(eq(types), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singletonList(sensorAssignment)).when(event).getRemovedSensorAssignments(functionalAssignmentFactory); job.setEnvironmentUpdateEvent(event); job.run(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).removeInstrumentationPoints(types, Collections.singleton(instrumentationApplier)); verifyNoMoreInteractions(instrumentationService); verifyZeroInteractions(environment, agentConfiguration, eventPublisher); } }
@BeforeMethod public void setup() throws Exception { when(configurationHolder.getAgentConfiguration()).thenReturn(agentConfiguration); when(configurationHolder.getEnvironment()).thenReturn(environment); when(configurationHolder.getInstrumentationAppliers()).thenReturn(Collections.singletonList(holdedInstrumentationApplier)); when(agentCacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(agentCacheEntry.getClassCache()).thenReturn(classCache); when(agentCacheEntry.getId()).thenReturn(PLATFORM_ID); when(classCache.getInstrumentationService()).thenReturn(instrumentationService); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(immutableClassType); when(classType.getFQN()).thenReturn("fqn"); when(event.getAfter()).thenReturn(updateEnvironment); }
@Test public void addedAssignment() throws RemoteException, BusinessException { Collection<ClassType> types = Collections.singleton(classType); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(types).when(instrumentationService).addInstrumentationPoints(eq(types), eq(agentConfiguration), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singletonList(sensorAssignment)).when(event).getAddedSensorAssignments(functionalAssignmentFactory); job.setEnvironmentUpdateEvent(event); job.run(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).addInstrumentationPoints(eq(types), eq(agentConfiguration), captor.capture()); assertThat((Collection<IInstrumentationApplier>) captor.getValue(), hasSize(1)); assertThat(((Collection<IInstrumentationApplier>) captor.getValue()).iterator().next(), is(instrumentationApplier)); ArgumentCaptor<ClassInstrumentationChangedEvent> eventCaptor = ArgumentCaptor.forClass(ClassInstrumentationChangedEvent.class); verify(eventPublisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getAgentId(), is(equalTo(PLATFORM_ID))); assertThat(eventCaptor.getValue().getInstrumentationDefinitions(), contains(org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqn")))); verifyNoMoreInteractions(eventPublisher); verifyZeroInteractions(environment, agentConfiguration); }
EnvironmentUpdateEvent event = (EnvironmentUpdateEvent) captor.getValue(); assertThat(event.getAddedProfiles(), hasSize(1)); assertThat(event.getAddedProfiles(), hasItem(profile2)); assertThat(event.getRemovedProfiles(), hasSize(1)); assertThat(event.getRemovedProfiles(), hasItem(profile1));
@Test public void functionalNoChange() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); SpecialMethodSensorAssignment functionalAssignment = mock(SpecialMethodSensorAssignment.class); when(functionalAssignmentFactory.getSpecialAssignments(old)).thenReturn(Collections.singletonList(functionalAssignment)); when(functionalAssignmentFactory.getSpecialAssignments(updated)).thenReturn(Collections.singletonList(functionalAssignment)); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, null, null); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(functionalAssignmentFactory); assertThat(removed, is(empty())); }
@Test public void profileRemovedNotActive() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); AbstractClassSensorAssignment<?> assignment = mock(AbstractClassSensorAssignment.class); doReturn(Collections.singletonList(assignment)).when(profileData).getData(SensorAssignmentProfileData.class); when(profile.isActive()).thenReturn(false); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, Collections.singletonList(profile), null); Collection<AbstractClassSensorAssignment<?>> added = event.getAddedSensorAssignments(functionalAssignmentFactory); assertThat(added, is(empty())); }
if (!Objects.equals(environment.getId(), event.getEnvironmentId())) { continue;
@Test public void removedAssignment() throws RemoteException, BusinessException { Collection<ClassType> types = Collections.singleton(classType); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(types).when(instrumentationService).removeInstrumentationPoints(eq(types), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singletonList(sensorAssignment)).when(event).getRemovedSensorAssignments(functionalAssignmentFactory); job.setEnvironmentUpdateEvent(event); job.run(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); verify(instrumentationService, times(1)).removeInstrumentationPoints(types, Collections.singleton(instrumentationApplier)); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); Collection<IInstrumentationApplier> appliers = configurationHolder.getInstrumentationAppliers(); verify(instrumentationService, times(1)).addInstrumentationPoints(captor.capture(), eq(agentConfiguration), eq(appliers)); assertThat((Collection<ClassType>) captor.getValue(), hasSize(1)); assertThat(((Collection<ClassType>) captor.getValue()).iterator().next(), is(classType)); ArgumentCaptor<ClassInstrumentationChangedEvent> eventCaptor = ArgumentCaptor.forClass(ClassInstrumentationChangedEvent.class); verify(eventPublisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getAgentId(), is(equalTo(PLATFORM_ID))); assertThat(eventCaptor.getValue().getInstrumentationDefinitions(), contains(org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqn")))); verifyNoMoreInteractions(eventPublisher); verifyZeroInteractions(environment, agentConfiguration); }
@Test public void functionalRemoved() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); SpecialMethodSensorAssignment functionalAssignment = mock(SpecialMethodSensorAssignment.class); when(functionalAssignmentFactory.getSpecialAssignments(old)).thenReturn(Collections.singletonList(functionalAssignment)); when(functionalAssignmentFactory.getSpecialAssignments(updated)).thenReturn(Collections.<SpecialMethodSensorAssignment> emptyList()); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, null, null); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(functionalAssignmentFactory); assertThat(removed, hasSize(1)); assertThat(removed, hasItem(functionalAssignment)); }
@Test public void functionalNoChange() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); SpecialMethodSensorAssignment functionalAssignment = mock(SpecialMethodSensorAssignment.class); when(functionalAssignmentFactory.getSpecialAssignments(old)).thenReturn(Collections.singletonList(functionalAssignment)); when(functionalAssignmentFactory.getSpecialAssignments(updated)).thenReturn(Collections.singletonList(functionalAssignment)); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, null, null); Collection<AbstractClassSensorAssignment<?>> added = event.getAddedSensorAssignments(functionalAssignmentFactory); assertThat(added, is(empty())); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void successful() throws InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getId()).thenReturn("id"); when(event.getEnvironmentId()).thenReturn("id"); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); eventListener.onApplicationEvent(event); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).isInitialized(); verify(configurationHolder).getEnvironment(); verify(environment).getId(); verify(event).getEnvironmentId(); verify(objectFactory).getObject(); verify(updateJob).setAgentCacheEntry(cacheEntry); verify(updateJob).setEnvironmentUpdateEvent(event); verify(executor).submit(updateJob); verify(future).get(1, TimeUnit.MINUTES); verifyNoMoreInteractions(nextGenInstrumentationManager, cacheEntry, configurationHolder, environment, event, objectFactory, updateJob, executor, future); }
@Test(expectedExceptions = IllegalArgumentException.class) public void idsNotMatching() { when(updated.getId()).thenReturn(ID); when(old.getId()).thenReturn("smth"); new EnvironmentUpdateEvent(this, old, updated, null, null); } }
/** * Returns all {@link AbstractClassSensorAssignment} that are contained in the removed profiles. * Only active profiles are taken into account. Also includes the functional assignments that * might be removed as the result of changes in the environment. * * @param functionalAssignmentFactory * SpecialMethodSensorAssignmentFactory for resolving functional assignment updates. * * @return Returns all {@link AbstractClassSensorAssignment} that are "removed". */ public Collection<AbstractClassSensorAssignment<?>> getRemovedSensorAssignments(SpecialMethodSensorAssignmentFactory functionalAssignmentFactory) { Collection<AbstractClassSensorAssignment<?>> removedAssignments = getSensorAssignments(removedProfiles); removedAssignments.addAll(getFunctionalAssignmentsDifference(functionalAssignmentFactory, before, after)); return removedAssignments; }
@Test public void profileRemovedNotActive() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); AbstractClassSensorAssignment<?> assignment = mock(AbstractClassSensorAssignment.class); doReturn(Collections.singletonList(assignment)).when(profileData).getData(SensorAssignmentProfileData.class); when(profile.isActive()).thenReturn(false); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, null, Collections.singletonList(profile)); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(functionalAssignmentFactory); assertThat(removed, is(empty())); }