/** * {@inheritDoc} */ @Override protected Collection<ImmutableType> execute() { // always update configuration getConfigurationHolder().update(getEnvironment(), getAgentId()); Collection<ImmutableType> changedClassTypes = new HashSet<>(); // first process all removed and added assignments changedClassTypes.addAll(super.processRemovedAssignments(profileUpdateEvent.getRemovedSensorAssignments())); changedClassTypes.addAll(super.processAddedAssignments(profileUpdateEvent.getAddedSensorAssignments())); return changedClassTypes; }
@Test public void activated() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); doReturn(Collections.singletonList(assignment)).when(oldProfileData).getData(SensorAssignmentProfileData.class); doReturn(Collections.singletonList(assignment)).when(updatedProfileData).getData(SensorAssignmentProfileData.class); when(old.isActive()).thenReturn(false); when(updated.isActive()).thenReturn(true); ProfileUpdateEvent event = new ProfileUpdateEvent(this, old, updated); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(); assertThat(removed, is(empty())); }
@Test public void noChange() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); doReturn(Collections.singletonList(assignment)).when(oldProfileData).getData(SensorAssignmentProfileData.class); doReturn(Collections.singletonList(assignment)).when(updatedProfileData).getData(SensorAssignmentProfileData.class); when(old.isActive()).thenReturn(true); when(updated.isActive()).thenReturn(true); ProfileUpdateEvent event = new ProfileUpdateEvent(this, old, updated); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(); assertThat(removed, is(empty())); }
@Test public void wrongProfileData() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); when(oldProfileData.isOfType(SensorAssignmentProfileData.class)).thenReturn(false); when(updatedProfileData.isOfType(SensorAssignmentProfileData.class)).thenReturn(false); when(old.isActive()).thenReturn(true); when(updated.isActive()).thenReturn(true); ProfileUpdateEvent event = new ProfileUpdateEvent(this, old, updated); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(); assertThat(removed, is(empty())); } }
@Test public void deactivated() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); doReturn(Collections.singletonList(assignment)).when(oldProfileData).getData(SensorAssignmentProfileData.class); doReturn(Collections.singletonList(assignment)).when(updatedProfileData).getData(SensorAssignmentProfileData.class); when(old.isActive()).thenReturn(true); when(updated.isActive()).thenReturn(false); ProfileUpdateEvent event = new ProfileUpdateEvent(this, old, updated); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(); assertThat(removed, hasSize(1)); assertThat(removed, hasItem(assignment)); }
@Test public void removedAssignments() { when(old.getId()).thenReturn(ID); when(updated.getId()).thenReturn(ID); doReturn(Collections.singletonList(assignment)).when(oldProfileData).getData(SensorAssignmentProfileData.class); doReturn(Collections.<AbstractClassSensorAssignment<?>> emptyList()).when(updatedProfileData).getData(SensorAssignmentProfileData.class); when(old.isActive()).thenReturn(true); when(updated.isActive()).thenReturn(true); ProfileUpdateEvent event = new ProfileUpdateEvent(this, old, updated); Collection<AbstractClassSensorAssignment<?>> removed = event.getRemovedSensorAssignments(); assertThat(removed, hasSize(1)); assertThat(removed, hasItem(assignment)); }
@Test public void removedAssignmentNoChange() throws RemoteException { Collection<ClassType> types = ImmutableList.of(classTypeOne, classTypeTwo); 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.singleton(sensorAssignment)).when(event).getRemovedSensorAssignments(); job.setProfileUpdateEvent(event); job.run(); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).removeInstrumentationPoints(eq(types), captor.capture()); assertThat((Collection<IInstrumentationApplier>) captor.getValue(), hasSize(1)); assertThat(((Collection<IInstrumentationApplier>) captor.getValue()).iterator().next(), is(instrumentationApplier)); verifyNoMoreInteractions(instrumentationService); verifyZeroInteractions(environment, eventPublisher); } }
@Test public void removedAssignment() throws RemoteException { Collection<ClassType> types = ImmutableList.of(classTypeOne, classTypeTwo); 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.singleton(sensorAssignment)).when(event).getRemovedSensorAssignments(); job.setProfileUpdateEvent(event); job.run(); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).removeInstrumentationPoints(eq(types), captor.capture()); assertThat((Collection<IInstrumentationApplier>) captor.getValue(), hasSize(1)); assertThat(((Collection<IInstrumentationApplier>) captor.getValue()).iterator().next(), is(instrumentationApplier)); ArgumentCaptor<Collection> typeCaptor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService).getInstrumentationResults(typeCaptor.capture()); assertThat((Collection<ClassType>) typeCaptor.getValue(), hasItems(classTypeOne, classTypeTwo)); Collection<IInstrumentationApplier> appliers = configurationHolder.getInstrumentationAppliers(); verify(instrumentationService, times(1)).addInstrumentationPoints(captor.capture(), eq(agentConfiguration), eq(appliers)); assertThat((Collection<ClassType>) captor.getValue(), hasSize(2)); assertThat(((Collection<ClassType>) captor.getValue()).iterator().next(), is(classTypeOne)); ArgumentCaptor<ClassInstrumentationChangedEvent> eventCaptor = ArgumentCaptor.forClass(ClassInstrumentationChangedEvent.class); verify(eventPublisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getAgentId(), is(equalTo(10L))); Matcher<InstrumentationDefinition> matcherOne = org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqnOne")); Matcher<InstrumentationDefinition> matcherTwo = org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqnTwo")); assertThat(eventCaptor.getValue().getInstrumentationDefinitions(), hasItems(matcherOne, matcherTwo)); verifyNoMoreInteractions(instrumentationService, eventPublisher); verifyZeroInteractions(environment); }
@Test public void updateProfileMethodAssignmentsChange() throws Exception { MethodSensorAssignment methodSensorAssignment1 = mock(MethodSensorAssignment.class); MethodSensorAssignment methodSensorAssignment2 = mock(MethodSensorAssignment.class); Profile profile = new Profile(); profile.setName("test"); SensorAssignmentProfileData profileData = new SensorAssignmentProfileData(); profileData.setMethodSensorAssignments(Collections.singletonList(methodSensorAssignment1)); profile.setProfileData(profileData); Profile created = manager.createProfile(profile); assertThat((Collection<MethodSensorAssignment>) profile.getProfileData().getData(SensorAssignmentProfileData.class), hasItem(methodSensorAssignment1)); profile = new Profile(); profile.setId(created.getId()); profile.setRevision(created.getRevision()); profile.setName(created.getName()); profileData = new SensorAssignmentProfileData(); profileData.setMethodSensorAssignments(Collections.singletonList(methodSensorAssignment2)); profile.setProfileData(profileData); Profile updated = manager.updateProfile(profile); assertThat((Collection<MethodSensorAssignment>) profile.getProfileData().getData(SensorAssignmentProfileData.class), hasItem(methodSensorAssignment2)); assertThat(updated.getRevision(), is(2)); ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor.forClass(ApplicationEvent.class); verify(eventPublisher).publishEvent(captor.capture()); assertThat(captor.getValue(), is(instanceOf(ProfileUpdateEvent.class))); ProfileUpdateEvent profileUpdateEvent = (ProfileUpdateEvent) captor.getValue(); assertThat(profileUpdateEvent.getRemovedSensorAssignments(), hasItem(methodSensorAssignment1)); assertThat(profileUpdateEvent.getAddedSensorAssignments(), hasItem(methodSensorAssignment2)); }
@Test public void updateProfileExceptionAssignmentsChange() throws Exception { ExceptionSensorAssignment exceptionSensorAssignment1 = mock(ExceptionSensorAssignment.class); ExceptionSensorAssignment exceptionSensorAssignment2 = mock(ExceptionSensorAssignment.class); Profile profile = new Profile(); profile.setName("test"); SensorAssignmentProfileData profileData = new SensorAssignmentProfileData(); profileData.setExceptionSensorAssignments(Collections.singletonList(exceptionSensorAssignment1)); profile.setProfileData(profileData); Profile created = manager.createProfile(profile); assertThat((Collection<ExceptionSensorAssignment>) profile.getProfileData().getData(SensorAssignmentProfileData.class), hasItem(exceptionSensorAssignment1)); profile = new Profile(); profile.setId(created.getId()); profile.setRevision(created.getRevision()); profile.setName(created.getName()); profileData = new SensorAssignmentProfileData(); profileData.setExceptionSensorAssignments(Collections.singletonList(exceptionSensorAssignment2)); profile.setProfileData(profileData); Profile updated = manager.updateProfile(profile); assertThat((Collection<ExceptionSensorAssignment>) profile.getProfileData().getData(SensorAssignmentProfileData.class), hasItem(exceptionSensorAssignment2)); assertThat(updated.getRevision(), is(2)); ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor.forClass(ApplicationEvent.class); verify(eventPublisher).publishEvent(captor.capture()); assertThat(captor.getValue(), is(instanceOf(ProfileUpdateEvent.class))); ProfileUpdateEvent profileUpdateEvent = (ProfileUpdateEvent) captor.getValue(); assertThat(profileUpdateEvent.getRemovedSensorAssignments(), hasItem(exceptionSensorAssignment1)); assertThat(profileUpdateEvent.getAddedSensorAssignments(), hasItem(exceptionSensorAssignment2)); }