/** * @return Returns class cache based on the {@link AgentCacheEntry}. */ protected ClassCache getClassCache() { return agentCacheEntry.getClassCache(); }
/** * @return Returns configuration holder based on the {@link AgentCacheEntry}. */ protected ConfigurationHolder getConfigurationHolder() { return agentCacheEntry.getConfigurationHolder(); }
/** * @return Returns agent id based on the {@link AgentCacheEntry}. */ protected long getAgentId() { return agentCacheEntry.getId(); }
@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); }
@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 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 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); } }
/** * Updates the defined configuration in the holder with following tasks:<br> * 1. Creates the new {@link #agentConfiguration} for given environment and platform id<br> * 2. Resolves all {@link #instrumentationAppliers} for given environment<br> * 3. sets the passes environment to the holder. * <p> * If <code>null</code> is passed then everything saved in the holder will be reset to * <code>null</code> as well. * * @param environment * Environment to update the configuration and appliers with. * @param platformId * Agent id needed for resolving configuration. */ public void update(Environment environment, long platformId) { if (null != environment) { this.environment = environment; this.agentConfiguration = configurationCreator.environmentToConfiguration(environment, platformId); this.instrumentationAppliers = configurationResolver.getInstrumentationAppliers(environment); this.jmxMonitoringAppliers = configurationResolver.getJmxMonitoringAppliers(environment); } else { this.environment = null; // NOPMD this.agentConfiguration = null; // NOPMD this.instrumentationAppliers = null; // NOPMD this.jmxMonitoringAppliers = null; // NOPMD } }
@BeforeMethod public void beforeMethod() { cacheMap = new HashMap<>(); when(nextGenInstrumentationManager.getAgentCacheMap()).thenReturn(cacheMap); when(cacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(configurationHolder.getEnvironment()).thenReturn(environment); }
@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); }
/** * @return Returns environment based on the {@link ConfigurationHolder}. */ protected Environment getEnvironment() { return getConfigurationHolder().getEnvironment(); }
/** * @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(newEnvironment, id);
@Test public void notInitialziedWithoutEnvironment() { boolean initialized = holder.isInitialized(); assertThat(initialized, is(false)); } }
/** * Collects all realizing classes that are sub-class of given class type or any of its * sub-classes and adds them to the given results list. This method is recursive. * * @param results * List to store classes to. * @param classType * Type to check. */ private void collectClassesFromSubClasses(Collection<ImmutableClassType> results, ImmutableClassType classType) { for (ImmutableClassType subClassType : classType.getImmutableSubClasses()) { if (subClassType.isInitialized()) { results.add(subClassType); } collectClassesFromSubClasses(results, subClassType); } }
@BeforeMethod public void beforeMethod() { cacheMap = new HashMap<>(); when(nextGenInstrumentationManager.getAgentCacheMap()).thenReturn(cacheMap); when(cacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(configurationHolder.getEnvironment()).thenReturn(environment); }
@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); }
@Override public Void answer(InvocationOnMock invocation) throws Throwable { when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); return null; } }).when(configurationHolder).update(environment, id);
@BeforeMethod public void beforeMethod() { cacheMap = new HashMap<>(); when(nextGenInstrumentationManager.getAgentCacheMap()).thenReturn(cacheMap); when(cacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(configurationHolder.getEnvironment()).thenReturn(cachedEnvironment); when(platformIdentDao.load(10L)).thenReturn(platformIdent); }
@Override public Void answer(InvocationOnMock invocation) throws Throwable { when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); return null; } }).when(configurationHolder).update(environment, id);