@Test public void notInitialziedWithoutEnvironment() { boolean initialized = holder.isInitialized(); assertThat(initialized, is(false)); } }
@Test public void configurationNotInitialized() { cacheMap.put(1L, cacheEntry); when(configurationHolder.isInitialized()).thenReturn(false); eventListener.onApplicationEvent(event); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).isInitialized(); verifyNoMoreInteractions(nextGenInstrumentationManager, cacheEntry, configurationHolder); verifyZeroInteractions(environment, event, objectFactory, updateJob, executor, future); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void futureThrowsExecutionException() throws InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getProfileIds()).thenReturn(Sets.newHashSet("id_1")); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); when(future.get(1L, TimeUnit.MINUTES)).thenThrow(ExecutionException.class); eventListener.onApplicationEvent(event); verify(future).get(1, TimeUnit.MINUTES); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void futureThrowsInterruptedException() throws InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getProfileIds()).thenReturn(Sets.newHashSet("id_1")); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); when(future.get(1L, TimeUnit.MINUTES)).thenThrow(InterruptedException.class); eventListener.onApplicationEvent(event); verify(future).get(1, TimeUnit.MINUTES); } }
@Test public void configurationHolderNotInitialized() 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); when(configurationHolder.isInitialized()).thenReturn(false); Collection<JmxAttributeDescriptor> toMonitor = manager.analyzeJmxAttributes(ID, Collections.singleton(descriptor)); assertThat(toMonitor, is(empty())); }
@Test public void configurationHolderHasNoAppliers() 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); when(configurationHolder.isInitialized()).thenReturn(true); when(configurationHolder.getJmxMonitoringAppliers()).thenReturn(null); Collection<JmxAttributeDescriptor> toMonitor = manager.analyzeJmxAttributes(ID, Collections.singleton(descriptor)); assertThat(toMonitor, is(empty())); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void futureThrowsExecutionException() throws InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(event.isProfileActive()).thenReturn(true); when(event.getProfileId()).thenReturn("id_1"); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getProfileIds()).thenReturn(Sets.newHashSet("id_1")); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); when(future.get(1L, TimeUnit.MINUTES)).thenThrow(ExecutionException.class); eventListener.onApplicationEvent(event); verify(future).get(1L, TimeUnit.MINUTES); }
@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 @SuppressWarnings({ "unchecked", "rawtypes" }) public void futureThrowsInterruptedException() throws InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(event.isProfileActive()).thenReturn(true); when(event.getProfileId()).thenReturn("id_1"); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getProfileIds()).thenReturn(Sets.newHashSet("id_1")); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); when(future.get(1L, TimeUnit.MINUTES)).thenThrow(InterruptedException.class); eventListener.onApplicationEvent(event); verify(future).get(1L, TimeUnit.MINUTES); } }
@Test public void configurationNotInitialized() { cacheMap.put(1L, cacheEntry); when(event.isProfileActive()).thenReturn(true); when(event.getProfileId()).thenReturn("id_1"); when(configurationHolder.isInitialized()).thenReturn(false); eventListener.onApplicationEvent(event); verify(event).isProfileActive(); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).isInitialized(); verifyNoMoreInteractions(event, cacheEntry, configurationHolder, nextGenInstrumentationManager); verifyZeroInteractions(environment, updateJob, future, objectFactory, executor); }
@Test public void existingClassTypeConfigurationNotInitialized() 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(false); InstrumentationDefinition result = manager.analyze(ID, HASH, type); assertThat(result, is(nullValue())); verifyZeroInteractions(modificationService, instrumentationService); }
@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); }
@Test public void environmentsAreNotEqual() { cacheMap.put(1L, cacheEntry); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getId()).thenReturn("id"); when(event.getEnvironmentId()).thenReturn("otherId"); eventListener.onApplicationEvent(event); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).isInitialized(); verify(configurationHolder).getEnvironment(); verify(environment).getId(); verify(event).getEnvironmentId(); verifyNoMoreInteractions(nextGenInstrumentationManager, cacheEntry, configurationHolder, environment, event); verifyZeroInteractions(objectFactory, updateJob, executor, future); }
@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") public void emptyEnvironment() { cacheMap.put(1L, cacheEntry); when(event.isProfileActive()).thenReturn(true); when(event.getProfileId()).thenReturn("id_1"); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getProfileIds()).thenReturn(Collections.EMPTY_SET); eventListener.onApplicationEvent(event); verify(event).isProfileActive(); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).getEnvironment(); verify(configurationHolder).isInitialized(); verify(environment).getProfileIds(); verifyNoMoreInteractions(event, cacheEntry, configurationHolder, environment, nextGenInstrumentationManager); verifyZeroInteractions(updateJob, future, objectFactory, executor); }
@Test public void unknownProfile() { cacheMap.put(1L, cacheEntry); when(event.isProfileActive()).thenReturn(true); when(event.getProfileId()).thenReturn("id_unknown"); when(configurationHolder.isInitialized()).thenReturn(true); when(environment.getProfileIds()).thenReturn(Sets.newHashSet("id_1")); eventListener.onApplicationEvent(event); verify(event).isProfileActive(); verify(event).getProfileId(); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(configurationHolder).getEnvironment(); verify(configurationHolder).isInitialized(); verify(environment, times(2)).getProfileIds(); verifyNoMoreInteractions(event, cacheEntry, configurationHolder, environment, nextGenInstrumentationManager); verifyZeroInteractions(updateJob, future, objectFactory, executor); }
@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); } }
@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); } }
/** * {@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; }