/** * {@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; }
/** * {@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 noEnvironment() { when(configurationHolder.isInitialized()).thenReturn(false); job.setEnvironment(null); job.run(); verify(instrumentationService, times(1)).removeInstrumentationPoints(); verify(configurationHolder, times(1)).update(null, PLATFORM_ID); verifyNoMoreInteractions(instrumentationService, eventPublisher); }
@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 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); }
@Test public void onlyConfigurationUpdate() throws RemoteException, BusinessException { job.setEnvironmentUpdateEvent(event); job.run(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); verify(agentConfiguration, times(0)).setInitialInstrumentationResults(Matchers.anyMap()); verify(agentConfiguration, times(0)).setClassCacheExistsOnCmr(Matchers.anyBoolean()); verifyZeroInteractions(classCache, environment, classCacheSearchNarrower, agentConfiguration, instrumentationService, eventPublisher); }
@Test public void updateReset() { long platformId = 11; Environment environment = mock(Environment.class); AgentConfig configuration = mock(AgentConfig.class); IInstrumentationApplier applier = mock(IInstrumentationApplier.class); JmxMonitoringApplier jmxApplier = mock(JmxMonitoringApplier.class); when(configurationCreator.environmentToConfiguration(environment, platformId)).thenReturn(configuration); when(configurationResolver.getInstrumentationAppliers(environment)).thenReturn(Collections.singleton(applier)); when(configurationResolver.getJmxMonitoringAppliers(environment)).thenReturn(Collections.singleton(jmxApplier)); holder.update(environment, platformId); holder.update(null, platformId); assertThat(holder.isInitialized(), is(false)); // only one time verifications verify(configurationCreator).environmentToConfiguration(environment, platformId); verify(configurationResolver).getInstrumentationAppliers(environment); verify(configurationResolver).getJmxMonitoringAppliers(environment); verifyNoMoreInteractions(configurationCreator, configurationResolver); } }
@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 @SuppressWarnings({ "unchecked", "rawtypes" }) public void yesEnvironment() { when(configurationHolder.isInitialized()).thenReturn(true); when(configurationHolder.getAgentConfiguration()).thenReturn(updateConfiguration); doReturn(Collections.singletonList(classType)).when(instrumentationService).addInstrumentationPoints(any(AgentConfig.class), any(Collection.class)); job.setEnvironment(updateEnvironment); job.run(); verify(instrumentationService, times(1)).removeInstrumentationPoints(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); Collection<IInstrumentationApplier> appliers = configurationHolder.getInstrumentationAppliers(); verify(instrumentationService, times(1)).addInstrumentationPoints(updateConfiguration, appliers); ArgumentCaptor<Collection> typeCaptor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService).getInstrumentationResults(typeCaptor.capture()); assertThat((Collection<Type>) typeCaptor.getValue(), hasItems((Type) classType)); verify(eventPublisher).publishEvent(any(ClassInstrumentationChangedEvent.class)); verifyNoMoreInteractions(instrumentationService, updateConfiguration, updateEnvironment, eventPublisher); } }
return null; }).when(configurationHolder).update(environment, id); verify(configurationResolver).getEnvironmentForAgent(definedIPs, agentName); verify(registrationService).registerPlatformIdent(definedIPs, agentName, version); verify(configurationHolder).update(environment, id); verify(configurationHolder).getEnvironment(); verify(configurationHolder).getAgentConfiguration();
@Test public void update() { long platformId = 11; Environment environment = mock(Environment.class); AgentConfig configuration = mock(AgentConfig.class); IInstrumentationApplier applier = mock(IInstrumentationApplier.class); JmxMonitoringApplier jmxApplier = mock(JmxMonitoringApplier.class); when(configurationCreator.environmentToConfiguration(environment, platformId)).thenReturn(configuration); when(configurationResolver.getInstrumentationAppliers(environment)).thenReturn(Collections.singleton(applier)); when(configurationResolver.getJmxMonitoringAppliers(environment)).thenReturn(Collections.singleton(jmxApplier)); holder.update(environment, platformId); assertThat(holder.isInitialized(), is(true)); assertThat(holder.getEnvironment(), is(environment)); assertThat(holder.getAgentConfiguration(), is(configuration)); assertThat(holder.getInstrumentationAppliers(), hasSize(1)); assertThat(holder.getInstrumentationAppliers(), hasItem(applier)); assertThat(holder.getJmxMonitoringAppliers(), hasSize(1)); assertThat(holder.getJmxMonitoringAppliers(), hasItem(jmxApplier)); verify(configurationCreator).environmentToConfiguration(environment, platformId); verify(configurationResolver).getInstrumentationAppliers(environment); verify(configurationResolver).getJmxMonitoringAppliers(environment); verifyNoMoreInteractions(configurationCreator, configurationResolver); }
@Test public void existingAgentNoEnvironment() throws BusinessException { long id = 10; List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; final AgentConfig configuration = mock(AgentConfig.class); Environment environment = mock(Environment.class); when(configurationResolver.getEnvironmentForAgent(definedIPs, agentName)).thenReturn(environment); when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(id); when(configurationHolder.isInitialized()).thenReturn(false); when(configurationHolder.getEnvironment()).thenReturn(environment); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); return null; } }).when(configurationHolder).update(environment, id); AgentConfig result = manager.register(definedIPs, agentName, version); assertThat(result, is(configuration)); verify(configurationResolver).getEnvironmentForAgent(definedIPs, agentName); verify(registrationService).registerPlatformIdent(definedIPs, agentName, version); verify(configurationHolder).update(environment, id); verify(configurationHolder).getEnvironment(); verify(configurationHolder).getAgentConfiguration(); verify(configurationHolder).isInitialized(); verifyNoMoreInteractions(configurationResolver, registrationService, configurationHolder); verifyZeroInteractions(classCache); }
/** * {@inheritDoc} */ @Override protected Collection<ImmutableType> execute() { Collection<ImmutableType> changedClassTypes = new HashSet<>(); // add current instrumentation changedClassTypes.addAll(getClassCache().getLookupService().findInstrumentedTypes()); // first remove all existing instrumentation points getClassCache().getInstrumentationService().removeInstrumentationPoints(); // then update configuration holder ConfigurationHolder configurationHolder = getConfigurationHolder(); configurationHolder.update(environment, getAgentId()); // if we are initialized analyze the complete class cache if (configurationHolder.isInitialized()) { // then add instrumentation points Collection<IInstrumentationApplier> instrumentationAppliers = configurationHolder.getInstrumentationAppliers(); Collection<? extends ImmutableClassType> instrumentedTypes = getClassCache().getInstrumentationService().addInstrumentationPoints(getAgentConfiguration(), instrumentationAppliers); changedClassTypes.addAll(instrumentedTypes); } return changedClassTypes; }
configurationHolder.update(environment, id);