@Test(expectedExceptions = { BusinessException.class }) public void noMappings() throws BusinessException { when(agentMappings.getMappings()).thenReturn(Collections.<AgentMapping> emptyList()); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); }
@Test(expectedExceptions = { BusinessException.class }) public void nullMappings() throws BusinessException { when(agentMappings.getMappings()).thenReturn(null); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); }
@Test(expectedExceptions = BusinessException.class) public void noMappingsForAgent() throws BusinessException { List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; when(configurationResolver.getEnvironmentForAgent(definedIPs, agentName)).thenThrow(new BusinessException(null)); manager.register(definedIPs, agentName, version); }
@Test(expectedExceptions = { BusinessException.class }) public void noMatchingMappingsName() throws BusinessException { AgentMapping mapping = mock(AgentMapping.class); when(agentMappings.getMappings()).thenReturn(Collections.singletonList(mapping)); when(mapping.getAgentName()).thenReturn("something else"); when(mapping.isActive()).thenReturn(true); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); }
@Test(expectedExceptions = { BusinessException.class }) public void noMatchingMappingsNameWildcard() throws BusinessException { AgentMapping mapping = mock(AgentMapping.class); when(agentMappings.getMappings()).thenReturn(Collections.singletonList(mapping)); when(mapping.getAgentName()).thenReturn("ins*TT"); when(mapping.isActive()).thenReturn(true); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); }
@Test(expectedExceptions = { BusinessException.class }) public void noMatchingMappingsIp() throws BusinessException { AgentMapping mapping = mock(AgentMapping.class); when(agentMappings.getMappings()).thenReturn(Collections.singletonList(mapping)); when(mapping.getAgentName()).thenReturn("*"); when(mapping.getIpAddress()).thenReturn("128.0.0.1"); when(mapping.isActive()).thenReturn(true); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void futureThrowsExecutionException() throws InterruptedException, ExecutionException, BusinessException, TimeoutException { cacheMap.put(1L, cacheEntry); when(cacheEntry.getId()).thenReturn(10L); when(configurationResolver.getEnvironmentForAgent(any(List.class), any(String.class))).thenReturn(newEnvironment); when(cachedEnvironment.getId()).thenReturn("id"); when(newEnvironment.getId()).thenReturn("newId"); 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(expectedExceptions = { BusinessException.class }) public void noMatchingMappingsIpWildcard() throws BusinessException { AgentMapping mapping = mock(AgentMapping.class); when(agentMappings.getMappings()).thenReturn(Collections.singletonList(mapping)); when(mapping.getAgentName()).thenReturn("*"); when(mapping.getIpAddress()).thenReturn("127.*.2"); when(mapping.isActive()).thenReturn(true); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void futureThrowsInterruptedException() throws InterruptedException, ExecutionException, BusinessException, TimeoutException { cacheMap.put(1L, cacheEntry); when(cacheEntry.getId()).thenReturn(10L); when(configurationResolver.getEnvironmentForAgent(any(List.class), any(String.class))).thenReturn(newEnvironment); when(cachedEnvironment.getId()).thenReturn("id"); when(newEnvironment.getId()).thenReturn("newId"); 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(expectedExceptions = { BusinessException.class }) public void inactiveMapping() throws BusinessException { AgentMapping mapping1 = mock(AgentMapping.class); when(agentMappings.getMappings()).thenReturn(Collections.singleton(mapping1)); when(mapping1.getAgentName()).thenReturn("ins*"); when(mapping1.isActive()).thenReturn(false); when(mapping1.getIpAddress()).thenReturn("*"); when(mapping1.getEnvironmentId()).thenReturn("env1"); Environment environment = mock(Environment.class); when(configurationInterfaceManager.getEnvironment("env1")).thenReturn(environment); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); } }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void noEnvironmentAssigned() throws InterruptedException, ExecutionException, BusinessException, TimeoutException { cacheMap.put(1L, cacheEntry); when(cacheEntry.getId()).thenReturn(10L); when(configurationHolder.getEnvironment()).thenReturn(null); when(configurationResolver.getEnvironmentForAgent(any(List.class), any(String.class))).thenReturn(newEnvironment); when(cachedEnvironment.getId()).thenReturn("id"); when(newEnvironment.getId()).thenReturn("newId"); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); eventListener.onApplicationEvent(event); verify(configurationHolder).getEnvironment(); verify(future).get(1L, TimeUnit.MINUTES); verifyNoMoreInteractions(configurationHolder, future); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void resolverThrowsBusinessException() throws BusinessException, InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(cacheEntry.getId()).thenReturn(10L); when(configurationResolver.getEnvironmentForAgent(any(List.class), any(String.class))).thenThrow(BusinessException.class); when(cachedEnvironment.getId()).thenReturn("id"); when(newEnvironment.getId()).thenReturn("newId"); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); eventListener.onApplicationEvent(event); verify(configurationResolver).getEnvironmentForAgent(any(List.class), any(String.class)); verify(objectFactory).getObject(); verify(updateJob).setAgentCacheEntry(cacheEntry); verify(executor).submit(updateJob); verify(future).get(1L, TimeUnit.MINUTES); verifyNoMoreInteractions(configurationResolver, objectFactory, updateJob, executor, future); verifyZeroInteractions(cachedEnvironment, newEnvironment); }
@Test public void oneMatchingMapping() throws BusinessException { AgentMapping mapping1 = mock(AgentMapping.class); AgentMapping mapping2 = mock(AgentMapping.class); List<AgentMapping> mappings = new ArrayList<>(); mappings.add(mapping1); mappings.add(mapping2); when(agentMappings.getMappings()).thenReturn(mappings); when(mapping1.getAgentName()).thenReturn("ins*"); when(mapping1.isActive()).thenReturn(true); when(mapping1.getIpAddress()).thenReturn("*"); when(mapping2.getAgentName()).thenReturn("something else"); when(mapping2.isActive()).thenReturn(true); when(mapping1.getEnvironmentId()).thenReturn("env1"); Environment environment = mock(Environment.class); when(configurationInterfaceManager.getEnvironment("env1")).thenReturn(environment); assertThat(configurationResolver.getEnvironmentForAgent(definedIPs, agentName), is(environment)); }
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); doAnswer(new Answer<Void>() { verify(eventPublisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getPlatformId(), is(equalTo(id))); verify(configurationResolver).getEnvironmentForAgent(definedIPs, agentName); verify(registrationService).registerPlatformIdent(definedIPs, agentName, version); verify(configurationHolder).update(environment, id);
@Test public void twoMatchingMappings() throws BusinessException { AgentMapping mapping1 = mock(AgentMapping.class); AgentMapping mapping2 = mock(AgentMapping.class); List<AgentMapping> mappings = new ArrayList<>(); mappings.add(mapping1); mappings.add(mapping2); when(agentMappings.getMappings()).thenReturn(mappings); when(mapping1.getAgentName()).thenReturn("inspectit"); when(mapping1.isActive()).thenReturn(true); when(mapping1.getIpAddress()).thenReturn("127.0.0.1"); when(mapping1.getEnvironmentId()).thenReturn("env1"); when(mapping2.getAgentName()).thenReturn("*"); when(mapping2.getIpAddress()).thenReturn("*"); when(mapping2.isActive()).thenReturn(true); when(mapping2.getEnvironmentId()).thenReturn("env2"); Environment environment1 = mock(Environment.class); Environment environment2 = mock(Environment.class); when(configurationInterfaceManager.getEnvironment("env1")).thenReturn(environment1); when(configurationInterfaceManager.getEnvironment("env2")).thenReturn(environment2); configurationResolver.getEnvironmentForAgent(definedIPs, agentName); assertThat(configurationResolver.getEnvironmentForAgent(definedIPs, agentName), is(environment1)); }
Environment environment = mock(Environment.class); Environment newEnvironment = mock(Environment.class); when(configurationResolver.getEnvironmentForAgent(definedIPs, agentName)).thenReturn(newEnvironment); when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(id); when(configurationHolder.isInitialized()).thenReturn(true); verify(configurationResolver).getEnvironmentForAgent(definedIPs, agentName); verify(registrationService).registerPlatformIdent(definedIPs, agentName, version); verify(configurationHolder).update(newEnvironment, id);
@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); }
@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 @SuppressWarnings({ "unchecked", "rawtypes" }) public void successful() throws BusinessException, InterruptedException, ExecutionException, TimeoutException { cacheMap.put(1L, cacheEntry); when(cacheEntry.getId()).thenReturn(10L); when(configurationResolver.getEnvironmentForAgent(any(List.class), any(String.class))).thenReturn(newEnvironment); when(cachedEnvironment.getId()).thenReturn("id"); when(newEnvironment.getId()).thenReturn("newId"); when(objectFactory.getObject()).thenReturn(updateJob); when(executor.submit(updateJob)).thenReturn((Future) future); eventListener.onApplicationEvent(event); verify(nextGenInstrumentationManager).getAgentCacheMap(); verify(cacheEntry).getConfigurationHolder(); verify(cacheEntry).getId(); verify(configurationHolder).getEnvironment(); verify(platformIdentDao).load(10L); verify(platformIdent).getAgentName(); verify(platformIdent).getDefinedIPs(); verify(configurationResolver).getEnvironmentForAgent(any(List.class), any(String.class)); verify(cachedEnvironment).getId(); verify(newEnvironment).getId(); verify(objectFactory).getObject(); verify(updateJob).setAgentCacheEntry(cacheEntry); verify(updateJob).setEnvironment(newEnvironment); verify(executor).submit(updateJob); verify(future).get(1L, TimeUnit.MINUTES); verifyNoMoreInteractions(nextGenInstrumentationManager, cacheEntry, configurationHolder, platformIdentDao, platformIdent, configurationResolver, cachedEnvironment, newEnvironment, event, objectFactory, updateJob, executor, future); }
Environment environment = configurationResolver.getEnvironmentForAgent(definedIPs, agentName);