/** * {@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 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 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())); }
@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 public void functionalAdded() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); SpecialMethodSensorAssignment functionalAssignment = mock(SpecialMethodSensorAssignment.class); when(functionalAssignmentFactory.getSpecialAssignments(old)).thenReturn(Collections.<SpecialMethodSensorAssignment> emptyList()); 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, hasSize(1)); assertThat(added, hasItem(functionalAssignment)); }
@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<?>> added = event.getAddedSensorAssignments(functionalAssignmentFactory); assertThat(added, is(empty())); } }
@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); }