Example usage for org.springframework.mock.web MockHttpServletRequest setRemoteUser

List of usage examples for org.springframework.mock.web MockHttpServletRequest setRemoteUser

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setRemoteUser.

Prototype

public void setRemoteUser(@Nullable String remoteUser) 

Source Link

Usage

From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java

/**
 * Test on submit./*  w  w w. jav  a2 s . c  o m*/
 */
@Test
public void testOnSubmit() {
    final String caseId = "550e4713-e22b-11d4-a716-446655440000";
    final String cardId = "440e4816-e01b-74d4-a716-449955440092";
    final String fileName = "doesnotcompute.file";
    final String mimeType = "text/plain";
    final byte[] content = "roflcopter".getBytes();

    final MockHttpServletRequest request = this.newGet("/cardfileupload");
    request.setRemoteUser("admin");
    request.addParameter("case", caseId);
    request.addParameter("card", cardId);
    final FileUpload fileUpload = this.ctrl.showForm(request);
    fileUpload.setFile(content);

    final MockMultipartHttpServletRequest upload = new MockMultipartHttpServletRequest();
    upload.setRemoteUser("admin");
    final MockMultipartFile file = new MockMultipartFile("file", fileName, mimeType, content);
    upload.addFile(file);
    upload.addParameter("case", caseId);
    upload.addParameter("card", cardId);

    /*
     * Sadly enough we would need a flush() within the
     * PayloadManagerImpl.saveNewPayload() function for this test to succeed
     * since we moved to saving the payload via its own manager/dao.
     */

    // BindingResult errors = new DataBinder(fileUpload).getBindingResult();
    // String result = "";
    // try {
    // result = ctrl.onSubmit(fileUpload, errors, upload);
    // } catch (IOException e) {
    // fail("Should not fail on fail upload");
    // }
    //
    // assertFalse(errors.hasErrors());
    // assertNotNull(upload.getSession().getAttribute("successMessages"));
    //
    // AlphaCard myCard = alphaCardManager.get(new
    // AlphaCardIdentifier(caseId, cardId));
    // assertNotNull(myCard);
    // assertNotNull(myCard.getPayload());
    // Assert.assertArrayEquals(content, myCard.getPayload().getContent());
    // Assert.assertEquals(fileName, myCard.getPayload().getFilename());
    // Assert.assertEquals(mimeType, myCard.getPayload().getMimeType());

    // Assert.assertEquals("redirect:/caseform?caseId=" + caseId +
    // "&activeCardId=" + cardId, result);
}

From source file:alpha.portal.webapp.controller.CaseFormControllerTest.java

/**
 * Test add.//from   ww  w. ja  v a  2  s . c  om
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testAdd() throws Exception {
    MockHttpServletRequest request = this.newGet("/caseform");
    request.setRemoteUser("admin");
    final ModelAndView mv = this.form.showForm(this.filters, request, new MockHttpServletResponse());

    request = this.newPost("/caseform");
    request.setRemoteUser("admin");
    final AlphaCase aCase = (AlphaCase) mv.getModel().get("case");
    aCase.setName("test case which does not exist yet");
    final BindingResult errors = new DataBinder(aCase).getBindingResult();
    final String view = this.form.addCase(aCase, errors, request, new MockHttpServletResponse());

    final List<AlphaCase> dbCases = this.caseManager.findByName(aCase.getName());
    Assert.assertNotNull(dbCases);
    Assert.assertTrue(dbCases.size() >= 1);
    final AlphaCase dbCase = dbCases.get(0);
    Assert.assertNotNull(dbCase);
    Assert.assertEquals("redirect:/caseform?caseId=" + dbCase.getCaseId(), view);
    Assert.assertFalse(errors.hasErrors());
    Assert.assertNotNull(request.getSession().getAttribute("successMessages"));

    final Locale locale = request.getLocale();
    final ArrayList<Object> msgs = (ArrayList<Object>) request.getSession().getAttribute("successMessages");
    Assert.assertTrue(msgs.contains(this.form.getText("case.added", locale)));
}

From source file:alpha.portal.webapp.controller.CaseFormControllerTest.java

/**
 * Test delete.// w w  w .j av  a2 s .  co  m
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testDelete() throws Exception {
    MockHttpServletRequest request = this.newGet("/caseform");
    request.setParameter("caseId", CaseFormControllerTest.caseId);
    request.setRemoteUser("admin");
    final ModelAndView mv = this.form.showForm(this.filters, request, new MockHttpServletResponse());
    final AlphaCase myCase = (AlphaCase) mv.getModel().get("case");

    request = this.newPost("/caseform");
    request.setRemoteUser("admin");
    request.addParameter("delete", "");

    final BindingResult errors = new DataBinder(myCase).getBindingResult();
    final String view = this.form.deleteCase(myCase, errors, request);
    Assert.assertEquals(this.form.getCancelView(), view);
    Assert.assertNotNull(request.getSession().getAttribute("successMessages"));

    final Locale locale = request.getLocale();
    final ArrayList<Object> msgs = (ArrayList<Object>) request.getSession().getAttribute("successMessages");
    Assert.assertTrue(msgs.contains(this.form.getText("case.deleted", locale)));

    Assert.assertFalse(this.caseManager.exists(CaseFormControllerTest.caseId));
}

From source file:alpha.portal.webapp.controller.CaseFormControllerTest.java

/**
 * Test last./*ww w .j  a v  a  2 s . c o  m*/
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testLast() throws Exception {
    MockHttpServletRequest request = this.newGet("/caseform");
    request.setParameter("caseId", "550e4713-e22b-11d4-a716-446655440002");
    request.setRemoteUser("admin");
    ModelAndView mv = this.form.showForm(this.filters, request, new MockHttpServletResponse());

    request = this.newGet("/caseform");
    request.setParameter("caseId", "last");
    request.setRemoteUser("admin");

    final AlphaCase aCase = this.caseManager.get("550e4713-e22b-11d4-a716-446655440002");
    mv = this.form.showForm(this.filters, request, new MockHttpServletResponse());
    Assert.assertEquals("caseform", mv.getViewName());
    Assert.assertEquals(aCase, mv.getModel().get("case"));
}

From source file:alpha.portal.webapp.controller.CaseFormControllerTest.java

/**
 * Test edit.//from   w w  w.  j a  va2 s  .c  om
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testEdit() throws Exception {
    MockHttpServletRequest request = this.newGet("/caseform");
    request.setParameter("caseId", CaseFormControllerTest.caseId);
    request.setRemoteUser("admin");
    final ModelAndView mv = this.form.showForm(this.filters, request, new MockHttpServletResponse());
    Assert.assertEquals("caseform", mv.getViewName());
    final AlphaCase aCase = (AlphaCase) mv.getModel().get("case");
    AlphaCase dbCase = this.caseManager.get(CaseFormControllerTest.caseId);
    Assert.assertEquals(dbCase, aCase);
    Assert.assertEquals(dbCase.getAlphaCards(), mv.getModel().get("cards"));
    Assert.assertEquals(dbCase.getListOfParticipants(), mv.getModel().get("participants"));

    request = this.newPost("/caseform");
    request.setRemoteUser("admin");
    aCase.setName("test case with a new name");
    final BindingResult errors = new DataBinder(aCase).getBindingResult();
    final String view = this.form.saveCase(aCase, errors, request, new MockHttpServletResponse());
    Assert.assertEquals("redirect:/caseform?caseId=" + aCase.getCaseId(), view);
    Assert.assertFalse(errors.hasErrors());
    Assert.assertNotNull(request.getSession().getAttribute("successMessages"));

    final Locale locale = request.getLocale();
    final ArrayList<Object> msgs = (ArrayList<Object>) request.getSession().getAttribute("successMessages");
    Assert.assertTrue(msgs.contains(this.form.getText("case.updated", locale)));

    dbCase = this.caseManager.get(CaseFormControllerTest.caseId);
    /* FIXME: something is broken (return structure is an empty thing) */
    // Assert.assertEquals(dbCase, aCase);
}

From source file:alpha.portal.webapp.controller.ContributorRoleControllerTest.java

/**
 * Test basic page call./*from   www . ja  v  a2  s.  co m*/
 * 
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
@Test
public void testBasicPage() throws Exception {
    final MockHttpServletRequest request = this.newGet("/contributorRole");
    request.setRemoteUser(this.testUserName);

    final int numberOfRoles = this.contributorRoleManager.getAll().size();
    /**
     * Only test precondition!
     */
    Assert.assertTrue("No roles to test!", numberOfRoles > 0);

    final ModelAndView result = this.ctrl.showPage(request);
    final Map<String, Object> resModel = result.getModel();

    Assert.assertTrue(resModel.containsKey("contributorRolesList"));
    final Object contribListObj = resModel.get("contributorRolesList");
    final List<ContributorRole> contribList = (List<ContributorRole>) contribListObj;
    Assert.assertEquals(numberOfRoles, contribList.size());
}

From source file:org.carewebframework.ui.test.MockEnvironment.java

/**
 * Initialize the mock servlet request./* w  ww . j  ava2  s .com*/
 * 
 * @param request The mock request.
 * @return The initialized mock request.
 */
protected MockHttpServletRequest init(MockHttpServletRequest request) {
    request.setRemoteAddr("127.0.0.1");
    request.setRemoteHost("mock");
    request.setRemotePort(8080);
    request.setRemoteUser("mockuser");
    request.setRequestURI("/zkau/mock");
    return request;
}

From source file:alpha.portal.webapp.controller.ContributorRoleControllerTest.java

/**
 * "Umlaut"-Test./*from w w  w.  j  a v  a  2 s  .  c o m*/
 * 
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
@Test
public void testUmlaute() throws Exception {
    final String newCRName = "'";

    MockHttpServletRequest request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_new", "button.save");
    request.addParameter("newContributorRole", newCRName);
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.ctrl.saveNew(request, response);
    Assert.assertTrue(StringUtils.isBlank(response.getErrorMessage()));

    request = this.newGet("/contributorRole");
    request.setRemoteUser(this.testUserName);
    final ModelAndView result = this.ctrl.showPage(request);
    final Map<String, Object> resModel = result.getModel();
    final Object contribListObj = resModel.get("contributorRolesList");
    final List<ContributorRole> contribList = (List<ContributorRole>) contribListObj;
    boolean contains = false;
    for (int c = 0; (c < contribList.size()) && (contains == false); c++) {
        if (contribList.get(c).getName().equals(newCRName)) {
            contains = true;
        }
    }
    Assert.assertTrue("Umlaut-Test failed", contains);
}

From source file:alpha.portal.webapp.controller.ContributorRoleControllerTest.java

/**
 * Test for get error redirects.//w  ww  . j  av a2  s .  c  o  m
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testGetErrors() throws Exception {
    MockHttpServletRequest request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    ModelAndView result = this.ctrl.showPage(request);
    Map<String, Object> resModel = result.getModel();
    Assert.assertTrue(resModel.containsKey("contributorRolesList"));

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("delete", "1234567890");
    result = this.ctrl.showPage(request);
    resModel = result.getModel();
    Assert.assertTrue(resModel.containsKey("contributorRolesList"));

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("delete", this.contributorRoleManager.getContributorRoleByName("Radiologe")
            .getContributorRoleId().toString());
    request.addParameter("edit", "1234567890");
    result = this.ctrl.showPage(request);
    resModel = result.getModel();

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("edit", "1234567890");
    result = this.ctrl.showPage(request);
    resModel = result.getModel();
    Assert.assertFalse(resModel.containsKey("showEditingForm"));
    Assert.assertFalse(resModel.containsKey("roleToEditId"));
    Assert.assertFalse(resModel.containsKey("roleToEdit"));
}

From source file:alpha.portal.webapp.controller.CaseMenuControllerTest.java

/**
 * Test handle request.//from w w w .j a  v  a 2 s .  c  o m
 * 
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleRequest() throws Exception {
    final MockHttpServletRequest request = this.newGet("/caseMenu");

    User u = new User("ichbineintollertesterdenesnochnichtgibt");
    u.setEmail("l@m.d");
    u.setFirstName("l");
    u.setLastName("m");
    u.setPassword("123");
    u = this.userManager.save(u);

    AlphaCase c = new AlphaCase();
    c.setName("blablabla");
    c.addParticipant(u);
    c = this.caseManager.save(c);

    request.setRemoteUser("ichbineintollertesterdenesnochnichtgibt");

    ModelAndView result = this.ctrl.handleRequest(request);

    List<AlphaCase> lCases = (List<AlphaCase>) result.getModel().get("caseList");
    Assert.assertEquals(1, lCases.size());
    final AlphaCase c2 = lCases.get(0);
    Assert.assertEquals(c, c2);

    this.caseManager.remove(c.getCaseId());
    result = this.ctrl.handleRequest(request);
    lCases = (List<AlphaCase>) result.getModel().get("caseList");
    Assert.assertTrue((lCases == null) || lCases.isEmpty());

    this.userManager.remove(u.getId());
}