Example usage for org.springframework.mock.web MockHttpServletResponse getWriter

List of usage examples for org.springframework.mock.web MockHttpServletResponse getWriter

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getWriter.

Prototype

@Override
    public PrintWriter getWriter() throws UnsupportedEncodingException 

Source Link

Usage

From source file:guru.nidi.ramltester.HighlevelTestBase.java

protected MockHttpServletResponse jsonResponse(int code, String json, String contentType)
        throws UnsupportedEncodingException {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setStatus(code);/*from  ww  w.j ava 2s.  com*/
    response.setContentType(contentType);
    response.getWriter().print(json);
    return response;
}

From source file:org.pentaho.platform.dataaccess.datasource.wizard.csv.FileUploadServiceTest.java

@Test
public void testUpload() throws Exception {

    PentahoSystemHelper.init();/*from   w  w  w .j av a  2 s.c  o  m*/
    StandaloneSession pSession = new StandaloneSession("12345678901234567890");
    PentahoSessionHolder.setSession(pSession);

    UUID uuid = UUIDUtil.getUUID();
    String fileName = uuid.toString();
    MockHttpSession session = new MockHttpSession(null, "12345678901234567890"); //$NON-NLS-1$
    MockHttpServletRequest request = new MockHttpServletRequest("POST", ""); //$NON-NLS-1$ //$NON-NLS-2$
    request.setSession(session);
    request.addParameter("file_name", fileName); //$NON-NLS-1$
    request.addParameter("mark_temporary", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    request.setContentType("multipart/form-data; boundary=boundary"); //$NON-NLS-1$
    StringBuffer content = new StringBuffer();
    content.append("--boundary\r\n"); //$NON-NLS-1$
    content.append(
            "Content-Disposition: form-data; name=uploadFormElement; filename=test_file.csv\r\nContent-Type: multipart/form-data\r\n\r\n"); //$NON-NLS-1$ 

    content.append("REGIONC,NWEIGHT,HD65,xdate,Location,charlen,xfactor,Flag\r\n"); //$NON-NLS-1$
    content.append("3,25677.96525,1231,1/1/10,Afghanistan,11,111.9090909,0\r\n"); //$NON-NLS-1$
    content.append("4,24261.81026,1663,1/2/10,Albania,7,237.5714286,0\r\n"); //$NON-NLS-1$
    content.append("2,31806.29502,5221,1/3/10,Algeria,7,745.8571429,1\r\n");//$NON-NLS-1$
    content.append("4,22345.39749,5261,1/4/10,American Samoa,14,375.7857143,1\r\n");//$NON-NLS-1$
    content.append("4,22345.39749,5261,1/4/10,American Samoa,14,375.7857143,1\r\n");//$NON-NLS-1$
    content.append("3,25677.96525,1231,1/1/10,Afghanistan,11,111.9090909,0\r\n");//$NON-NLS-1$
    content.append("4,24261.81026,1663,1/2/10,Albania,7,237.5714286,0\r\n");//$NON-NLS-1$
    content.append("2,31806.29502,5221,1/3/10,Algeria,7,745.8571429,1\r\n");//$NON-NLS-1$
    content.append("4,22345.39749,5261,1/4/10,American Samoa,14,375.7857143,1\r\n");//$NON-NLS-1$

    content.append("--boundary--\r\n"); //$NON-NLS-1$
    request.setContent(content.toString().getBytes());
    UploadFileDebugServlet uploadServlet = new UploadFileDebugServlet();
    MockHttpServletResponse response = new MockHttpServletResponse();
    uploadServlet.service(request, response);

    response.getWriter().flush();
    response.getWriter().close();
    fileName = response.getContentAsString();
    String path = PentahoSystem.getApplicationContext().getSolutionPath(TMP_FILE_PATH);

    String filenameWithPath = path + File.separatorChar + fileName;
    File file = new File(filenameWithPath);
    assertTrue(file.exists());
    if (file.exists()) {
        file.delete();
    }
}

From source file:org.pentaho.test.platform.web.ui.servlet.MondrianCatalogPublisherTests.java

protected MockHttpServletResponse simulateRequest(Map<String, String> map) throws Exception {

    // prepare request
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType("multipart/form-data; boundary=---1234"); //$NON-NLS-1$
    request.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
    String mondrianSchemaFile = map.get("mondrianSchemaFile");
    if (logger.isDebugEnabled()) {
        logger.debug("uploading mondrian schema file named \"" + mondrianSchemaFile + "\"");
    }//from ww w. j  a  v  a  2s  .c  o m
    String content = MessageFormat.format(DEFAULT_CONTENT_TEMPLATE, mondrianSchemaFile, DEFAULT_FILE_CONTENT);
    if (logger.isDebugEnabled()) {
        logger.debug("content=" + content); //$NON-NLS-1$
    }
    request.setContent(content.getBytes("UTF-8")); //$NON-NLS-1$
    request.addParameter("publishPath", map.get("publishPath")); //$NON-NLS-1$ //$NON-NLS-2$
    request.addParameter("publishKey", map.get("publishKey")); //$NON-NLS-1$ //$NON-NLS-2$
    request.addParameter("overwrite", map.get("overwrite")); //$NON-NLS-1$ //$NON-NLS-2$
    request.addParameter("jndiName", map.get("jndiName")); //$NON-NLS-1$ //$NON-NLS-2$

    MockHttpSession httpSession = new MockHttpSession();

    httpSession.setAttribute("pentaho-session", pentahoSession); //$NON-NLS-1$

    request.setSession(httpSession);

    // prepare response
    MockHttpServletResponse response = new MockHttpServletResponse();

    // prepare mondrian catalog service
    MondrianCatalogHelper catService = new MondrianCatalogHelper();
    catService.setDataSourcesConfig("file:" + destFile.getAbsolutePath()); //$NON-NLS-1$
    //    catService.afterPropertiesSet();

    // prepare mondrian catalog publisher
    MondrianCatalogPublisher pub = new MondrianCatalogPublisher();
    pub.setMondrianCatalogService(catService);
    pub.setFullyQualifiedServerURL("http://localhost:8080/pentaho"); //$NON-NLS-1$
    //    pub.afterPropertiesSet();

    // process request
    // TODO We need to figure out how to test this . doGet is a protected method now  
    //pub.doGet(request, response); 

    // assertions
    response.getWriter().flush();
    String responseContent = response.getContentAsString();
    if (logger.isDebugEnabled()) {
        logger.debug("response=" + responseContent); //$NON-NLS-1$
    }
    return response;
}