/** * {@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; }
@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())); }
@Test public void wrongProfileData() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); when(profileData.isOfType(SensorAssignmentProfileData.class)).thenReturn(false); when(profile.isActive()).thenReturn(true); EnvironmentUpdateEvent event = new EnvironmentUpdateEvent(this, old, updated, null, Collections.singletonList(profile)); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(functionalAssignmentFactory); assertThat(removed, is(empty())); }
@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 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 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 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); } }
@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); }