Search in sources :

Example 1 with SpringServlet

use of com.sun.jersey.spi.spring.container.servlet.SpringServlet in project pentaho-platform by pentaho.

the class JAXRSServletTest method serviceTest.

@Test
public void serviceTest() {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getMethod()).thenReturn("POST");
    try {
        Mockito.doNothing().when((SpringServlet) jaxrsServlet).service(request, response);
        jaxrsServlet.service(request, response);
        verify((SpringServlet) jaxrsServlet).service(eq(request), eq(response));
        when(request.getMethod()).thenReturn(JAXRSServlet.GET);
        String MIME_TYPE = "MIME_TYPE";
        String NOT_MIME_TYPE = "NOT_MIME_TYPE";
        when(request.getHeader(JAXRSServlet.ACCEPT)).thenReturn(NOT_MIME_TYPE);
        when(request.getParameter(JAXRSServlet.MIME_TYPE)).thenReturn(null);
        final AtomicBoolean fail = new AtomicBoolean(false);
        checkMime(request, response, MIME_TYPE, fail);
        assertFalse("Mime type was incorrectly changed", fail.get());
        when(request.getParameter(JAXRSServlet.MIME_TYPE)).thenReturn(JAXRSServlet.MIME_TYPE);
        checkMime(request, response, NOT_MIME_TYPE, fail);
        assertFalse("Mime type was incorrectly changed", fail.get());
    } catch (ServletException e) {
        e.printStackTrace();
        fail("ServletException appeared");
    } catch (IOException e) {
        e.printStackTrace();
        fail("IOException appeared");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SpringServlet(com.sun.jersey.spi.spring.container.servlet.SpringServlet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with SpringServlet

use of com.sun.jersey.spi.spring.container.servlet.SpringServlet in project pentaho-platform by pentaho.

the class JAXRSServletTest method serviceTestWriter.

@Test
public void serviceTestWriter() throws ServletException {
    callSuperIterate.set(true);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ResourceConfig resourceConfig = mock(ResourceConfig.class);
    String contextPath = "/context";
    String servletPath = "/servlet";
    StringBuffer requestUrl = new StringBuffer(contextPath + servletPath + URL);
    ServletConfig servletConfig = mock(ServletConfig.class);
    WebServletConfig webServletConfig = mock(WebServletConfig.class);
    ServletContext servletContext = mock(ServletContext.class);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    String[] beans = {};
    HashSet<Class> classes = new HashSet<>();
    classes.add(ResourseClass.class);
    when(request.getMethod()).thenReturn("POST");
    when(request.getRequestURL()).thenReturn(requestUrl);
    when(request.getRequestURI()).thenReturn(URL);
    when(request.getContextPath()).thenReturn(contextPath);
    when(request.getServletPath()).thenReturn(servletPath);
    doReturn(webServletConfig).when(jaxrsServlet).createWebConfig(jaxrsServlet);
    doReturn(servletConfig).when(jaxrsServlet).getServletConfig();
    doReturn(context).when(jaxrsServlet).getContext();
    doReturn(beans).when(context).getBeanNamesForType((Class<?>) any());
    doReturn(Collections.emptyEnumeration()).when(webServletConfig).getInitParameterNames();
    doReturn(null).when(servletContext).getAttribute("javax.enterprise.inject.spi.BeanManager");
    doReturn(servletContext).when(webServletConfig).getServletContext();
    doReturn(resourceConfig).when(webServletConfig).getDefaultResourceConfig(any());
    doReturn(classes).when(resourceConfig).getRootResourceClasses();
    doReturn(context).when(jaxrsServlet).getAppContext();
    doReturn(Collections.emptyEnumeration()).when(request).getHeaderNames();
    try {
        jaxrsServlet.init();
        jaxrsServlet.service(request, response);
        verify((SpringServlet) jaxrsServlet).service(eq(request), eq(response));
        verify(response).setStatus(404, "Not Found");
        jaxrsServlet.setBackWardSendErrorCompatible(true);
        jaxrsServlet.service(request, response);
        verify(response, never()).setStatus(404);
        verify(response, never()).sendError(404);
    } catch (ServletException e) {
        e.printStackTrace();
        fail("ServletException appeared");
    } catch (IOException e) {
        e.printStackTrace();
        fail("IOException appeared");
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebServletConfig(com.sun.jersey.spi.container.servlet.WebServletConfig) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebServletConfig(com.sun.jersey.spi.container.servlet.WebServletConfig) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletContext(javax.servlet.ServletContext) ResourceConfig(com.sun.jersey.api.core.ResourceConfig) HashSet(java.util.HashSet) SpringServlet(com.sun.jersey.spi.spring.container.servlet.SpringServlet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SpringServlet (com.sun.jersey.spi.spring.container.servlet.SpringServlet)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ResourceConfig (com.sun.jersey.api.core.ResourceConfig)1 WebServletConfig (com.sun.jersey.spi.container.servlet.WebServletConfig)1 HashSet (java.util.HashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ServletConfig (javax.servlet.ServletConfig)1 ServletContext (javax.servlet.ServletContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1