/** * @return Returns agent configuration based on the {@link ConfigurationHolder}. */ protected AgentConfig getAgentConfiguration() { return getConfigurationHolder().getAgentConfiguration(); }
@Override public Void answer(InvocationOnMock invocation) throws Throwable { when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); return null; } }).when(configurationHolder).update(environment, id);
@Override public Void answer(InvocationOnMock invocation) throws Throwable { when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); return null; } }).when(configurationHolder).update(environment, id);
@Override public Void answer(InvocationOnMock invocation) throws Throwable { when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); return null; } }).when(configurationHolder).update(newEnvironment, id);
for (JmxAttributeDescriptor descriptor : attributeDescriptors) { for (JmxMonitoringApplier applier : jmxMonitoringAppliers) { if (applier.addMonitoringPoint(configurationHolder.getAgentConfiguration(), descriptor)) { results.add(descriptor); break;
@Test public void applierDoesNothing() throws BusinessException, ClassCacheModificationException { List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(ID); manager.register(definedIPs, agentName, version); AgentConfig configuration = mock(AgentConfig.class); when(configurationHolder.isInitialized()).thenReturn(true); when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); when(configurationHolder.getJmxMonitoringAppliers()).thenReturn(Collections.singleton(applier)); when(applier.addMonitoringPoint(configuration, descriptor)).thenReturn(false); Collection<JmxAttributeDescriptor> toMonitor = manager.analyzeJmxAttributes(ID, Collections.singleton(descriptor)); assertThat(toMonitor, is(empty())); verify(applier).addMonitoringPoint(configuration, descriptor); }
@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 applierAddsMonitoringPoint() throws BusinessException, ClassCacheModificationException { List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(ID); manager.register(definedIPs, agentName, version); AgentConfig configuration = mock(AgentConfig.class); when(configurationHolder.isInitialized()).thenReturn(true); when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); when(configurationHolder.getJmxMonitoringAppliers()).thenReturn(Collections.singleton(applier)); when(applier.addMonitoringPoint(configuration, descriptor)).thenReturn(true); Collection<JmxAttributeDescriptor> toMonitor = manager.analyzeJmxAttributes(ID, Collections.singleton(descriptor)); assertThat(toMonitor, hasSize(1)); assertThat(toMonitor, hasItem(descriptor)); verify(applier).addMonitoringPoint(configuration, descriptor); }
@Test public void ensureNoDoubleRegistration() throws BusinessException, ClassCacheModificationException { List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(ID); manager.register(definedIPs, agentName, version); AgentConfig configuration = mock(AgentConfig.class); when(configurationHolder.isInitialized()).thenReturn(true); when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); List<JmxMonitoringApplier> appliers = new ArrayList<>(); appliers.add(applier); appliers.add(applier); when(configurationHolder.getJmxMonitoringAppliers()).thenReturn(appliers); when(applier.addMonitoringPoint(configuration, descriptor)).thenReturn(true); Collection<JmxAttributeDescriptor> toMonitor = manager.analyzeJmxAttributes(ID, Collections.singleton(descriptor)); assertThat(toMonitor, hasSize(1)); assertThat(toMonitor, hasItem(descriptor)); verify(applier).addMonitoringPoint(configuration, descriptor); }
@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); } }
@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(10L); when(classCache.getInstrumentationService()).thenReturn(instrumentationService); when(classTypeOne.isClass()).thenReturn(true); when(classTypeTwo.isClass()).thenReturn(true); when(classTypeOne.castToClass()).thenReturn(immutableClassTypeOne); when(classTypeTwo.castToClass()).thenReturn(immutableClassTypeTwo); when(immutableClassTypeOne.hasInstrumentationPoints()).thenReturn(true); when(immutableClassTypeOne.hasInstrumentationPoints()).thenReturn(false); when(classTypeOne.getFQN()).thenReturn("fqnOne"); when(classTypeTwo.getFQN()).thenReturn("fqnTwo"); }
@Test public void existingClassTypeInstrumented() throws BusinessException, ClassCacheModificationException { List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(ID); manager.register(definedIPs, agentName, version); ClassType classType = mock(ClassType.class); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(lookupService.findByHash(HASH)).thenReturn(classType); when(configurationHolder.isInitialized()).thenReturn(true); AgentConfig configuration = mock(AgentConfig.class); Collection<IInstrumentationApplier> appliers = mock(Collection.class); InstrumentationDefinition instrumentationResult = mock(InstrumentationDefinition.class); when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); when(configurationHolder.getInstrumentationAppliers()).thenReturn(appliers); when(instrumentationService.addAndGetInstrumentationResult(classType, configuration, appliers)).thenReturn(instrumentationResult); InstrumentationDefinition result = manager.analyze(ID, HASH, type); assertThat(result, is(instrumentationResult)); verify(instrumentationService).addAndGetInstrumentationResult(classType, configuration, appliers); verifyNoMoreInteractions(instrumentationService); verifyZeroInteractions(modificationService); }
@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); }
verify(configurationHolder).update(environment, id); verify(configurationHolder).getEnvironment(); verify(configurationHolder).getAgentConfiguration(); verify(configurationHolder).isInitialized(); verify(executor).submit(any(Runnable.class));
@Test public void existingAgent() 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(true); when(configurationHolder.getEnvironment()).thenReturn(environment); when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); Map<Collection<String>, InstrumentationDefinition> initialInstrumentations = mock(Map.class); when(instrumentationService.getInstrumentationResultsWithHashes()).thenReturn(initialInstrumentations); AgentConfig result = manager.register(definedIPs, agentName, version); assertThat(result, is(configuration)); verify(configurationResolver).getEnvironmentForAgent(definedIPs, agentName); verify(registrationService).registerPlatformIdent(definedIPs, agentName, version); verify(configurationHolder).getEnvironment(); verify(configurationHolder).getAgentConfiguration(); verify(configurationHolder).isInitialized(); verify(configuration).setInitialInstrumentationResults(initialInstrumentations); verify(configuration).setClassCacheExistsOnCmr(true); verify(classCache).getInstrumentationService(); verify(instrumentationService).getInstrumentationResultsWithHashes(); verifyNoMoreInteractions(configurationResolver, registrationService, configurationHolder, instrumentationService, classCache); }
@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); }
verify(configurationHolder).update(newEnvironment, id); verify(configurationHolder).getEnvironment(); verify(configurationHolder).getAgentConfiguration(); verify(configurationHolder).isInitialized(); verifyNoMoreInteractions(configurationResolver, registrationService, configurationHolder);
AgentConfig agentConfiguration = configurationHolder.getAgentConfiguration(); Map<Collection<String>, InstrumentationDefinition> initial = classCache.getInstrumentationService().getInstrumentationResultsWithHashes(); agentConfiguration.setInitialInstrumentationResults(initial); return configurationHolder.getAgentConfiguration();
return classCache.getInstrumentationService().addAndGetInstrumentationResult(classType, configurationHolder.getAgentConfiguration(), configurationHolder.getInstrumentationAppliers());