private FilterAndTelemetryClientMock createInitializedFilterWithTelemetryClient() throws ServletException, NoSuchFieldException, IllegalAccessException { return createInitializedFilterWithMockTelemetryClient(true, false); }
@Test public void testUnhandledIOExceptionWithTelemetryClient() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithTelemetryClient(); testException(createdData, new IOException()); }
@Test public void testUnhandledRuntimeExceptionWithTelemetryClientThatThrows() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithTelemetryClientThatThrows(); testException(createdData, new RuntimeException()); }
@Test public void testUnhandledIOExceptionWithoutTelemetryClient() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithoutTelemetryClient(); testException(createdData, new IOException()); }
private FilterAndTelemetryClientMock createInitializedFilterWithExposedTelemetryClient() throws Exception { Filter filter = createInitializedFilter(); Field field = WebRequestTrackingFilter.class.getDeclaredField("telemetryClient"); field.setAccessible(true); return new FilterAndTelemetryClientMock(filter, (TelemetryClient) field.get(filter)); } // endregion Private methods
@Test public void testWebAppNameInitializerAppNameHandoff() throws Exception { FilterAndTelemetryClientMock mocks = createInitializedFilterWithExposedTelemetryClient(); FilterChain chain = mock(FilterChain.class); mocks.filter.doFilter(ServletUtils.generateDummyServletRequest(), ServletUtils.generateDummyServletResponse(), chain); String role = mocks.mockTelemetryClient.getContext().getCloud().getRole(); assertNotNull(role); assertEquals(this.mockContextPath.replace("/", ""), role); }
@Test public void testUnhandledRuntimeExceptionWithoutTelemetryClient() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithoutTelemetryClient(); testException(createdData, new RuntimeException()); }
private FilterAndTelemetryClientMock createInitializedFilterWithMockTelemetryClient(boolean withTelemetryClient, boolean clientThrows) throws ServletException, NoSuchFieldException, IllegalAccessException { Filter filter = createInitializedFilter(); Field field = WebRequestTrackingFilter.class.getDeclaredField("telemetryClient"); field.setAccessible(true); TelemetryClient mockTelemetryClient = null; if (withTelemetryClient) { mockTelemetryClient = spy(new TelemetryClient()); if (clientThrows) { doThrow(new RuntimeException()).when(mockTelemetryClient).trackException(any(Exception.class)); } } field.set(filter, mockTelemetryClient); return new FilterAndTelemetryClientMock(filter, mockTelemetryClient); }
@Test public void testUnhandledIOExceptionWithTelemetryClientThatThrows() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithTelemetryClientThatThrows(); testException(createdData, new IOException()); }
@Test public void testUnhandledServletExceptionWithTelemetryClient() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithTelemetryClient(); testException(createdData, new ServletException()); }
@Test public void testUnhandledServletExceptionWithoutTelemetryClient() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithoutTelemetryClient(); testException(createdData, new ServletException()); }
@Test public void testFiltersChainWhenExceptionIsThrownOnModulesInvocation() throws Exception { Filter filter = createInitializedFilter(); // mocking WebModulesContainer containerMock = ServletUtils.setMockWebModulesContainer(filter); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { throw new Exception("FATAL!"); } }).when(containerMock).invokeOnBeginRequest(any(ServletRequest.class), any(ServletResponse.class)); FilterChain chain = mock(FilterChain.class); ServletRequest request = ServletUtils.generateDummyServletRequest(); // execute filter.doFilter(request, ServletUtils.generateDummyServletResponse(), chain); // validate verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); }
private FilterAndTelemetryClientMock createInitializedFilterWithTelemetryClientThatThrows() throws ServletException, NoSuchFieldException, IllegalAccessException { return createInitializedFilterWithMockTelemetryClient(true, true); }
@Test public void testUnhandledServletExceptionWithTelemetryClientThatThrows() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithTelemetryClientThatThrows(); testException(createdData, new ServletException()); }
@Test public void testUnhandledRuntimeExceptionWithTelemetryClient() throws IllegalAccessException, NoSuchFieldException, ServletException { FilterAndTelemetryClientMock createdData = createInitializedFilterWithTelemetryClient(); testException(createdData, new java.lang.IllegalArgumentException()); }
@Test public void testFilterInitializedSuccessfullyFromConfiguration() throws ServletException { Filter filter = createInitializedFilter(); WebModulesContainer container = ServletUtils.getWebModuleContainer(filter); assertNotNull("Container shouldn't be null", container); Assert.assertTrue("Modules container shouldn't be empty", container.getModulesCount() > 0); }
private FilterAndTelemetryClientMock createInitializedFilterWithoutTelemetryClient() throws ServletException, NoSuchFieldException, IllegalAccessException { return createInitializedFilterWithMockTelemetryClient(false, false); }