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.ContributorRoleControllerTest.java

/**
 * Test for post error redirects.//from  w  w w  .  ja  va  2 s . c  om
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testPostErrors() throws Exception {
    MockHttpServletRequest request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    MockHttpServletResponse response = new MockHttpServletResponse();
    String redirect = this.ctrl.doPost(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_new", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveNew(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_new", "button.save");
    request.addParameter("netContributorRole", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveNew(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_new", "button.save");
    request.addParameter("newContributorRole", this.contribRole.getName());
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveNew(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveNew(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("newContributorRole", "");
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveNew(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("newContributorRole", "TESTTESTTEST");
    request.addParameter("oldContribRoleId", "");
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveNew(request, response);
    Assert.assertEquals("redirect:/contributorRole", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("oldContribRoleId", "1");
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveEdit(request, response);
    Assert.assertEquals("redirect:/contributorRole?edit=1", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("newContributorRole", this.contribRole.getName());
    request.addParameter("oldContribRoleId", "1");
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveEdit(request, response);
    Assert.assertEquals("redirect:/contributorRole?edit=1", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("newContributorRole", "TESTTESTTEST");
    request.addParameter("oldContribRoleId", "12345667890");
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveEdit(request, response);
    Assert.assertEquals("redirect:/contributorRole?edit=12345667890", redirect);

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("newContributorRole", "TESTTESTTEST");
    request.addParameter("oldContribRoleId", "NotA-Long");
    request.addParameter("save_edit", "button.save");
    response = new MockHttpServletResponse();
    redirect = this.ctrl.saveEdit(request, response);
    Assert.assertEquals("redirect:/contributorRole?edit=NotA-Long", redirect);
}

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

/**
 * V-Test (save, show, edit, delete)./*ww  w .  j  av a2s  .c  o m*/
 * 
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
@Test
public void testVTest() throws Exception {
    final String newCRName = "New Test Contributor Role";

    final int numberOfRolesBeforeAdd = this.contributorRoleManager.getAll().size();

    /**
     * Add
     */
    MockHttpServletRequest request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_new", "button.save");
    request.addParameter("newContributorRole", newCRName);

    MockHttpServletResponse response = new MockHttpServletResponse();

    this.ctrl.saveNew(request, response);
    Assert.assertTrue(StringUtils.isBlank(response.getErrorMessage()));

    /**
     * Show
     */
    request = this.newGet("/contributorRole");
    request.setRemoteUser(this.testUserName);

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

    final Object contribListObj = resModel.get("contributorRolesList");
    final List<ContributorRole> contribList = (List<ContributorRole>) contribListObj;
    Assert.assertEquals((numberOfRolesBeforeAdd + 1), contribList.size());
    ContributorRole newCR = this.contributorRoleManager.getContributorRoleByName(newCRName);
    boolean contains = false;
    for (int c = 0; (c < contribList.size()) && (contains == false); c++) {
        if (contribList.get(c).getName().equals(newCRName)) {
            contains = true;
        }
    }
    Assert.assertTrue("New contrib.role in roles list", contains);

    /**
     * Edit-Page
     */
    request = this.newGet("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("edit", newCR.getContributorRoleId().toString());

    result = this.ctrl.showPage(request);
    resModel = result.getModel();

    Assert.assertTrue(resModel.containsKey("showEditingForm"));
    Assert.assertTrue(resModel.containsKey("roleToEditId"));
    Assert.assertTrue(resModel.containsKey("roleToEdit"));
    Assert.assertFalse(resModel.containsKey("messageId"));

    Assert.assertEquals(newCR.getContributorRoleId().toString(), resModel.get("roleToEditId"));
    Assert.assertEquals(newCR.getName(), resModel.get("roleToEdit"));

    /**
     * Edit-Post
     */
    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_edit", "button.save");
    request.addParameter("newContributorRole", newCRName + " - Changed");
    request.addParameter("oldContribRoleId", newCR.getContributorRoleId().toString());

    response = new MockHttpServletResponse();

    this.ctrl.saveEdit(request, response);
    Assert.assertTrue(StringUtils.isBlank(response.getErrorMessage()));

    newCR = this.contributorRoleManager.get(newCR.getContributorRoleId());
    Assert.assertEquals(newCRName + " - Changed", newCR.getName());

    /**
     * Delete
     */
    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("delete", newCR.getContributorRoleId().toString());

    result = this.ctrl.showPage(request);
    resModel = result.getModel();

    Assert.assertTrue(resModel.containsKey("contributorRolesList"));
    Assert.assertTrue(StringUtils.isBlank(response.getErrorMessage()));

    boolean isException = false;
    try {
        newCR = this.contributorRoleManager.get(newCR.getContributorRoleId());
    } catch (final ObjectRetrievalFailureException e) {
        isException = true;
    }
    Assert.assertTrue("role not deleted", isException);
}

From source file:com.mtgi.analytics.BehaviorTrackingManagerTest.java

/** test use of default SpringSessionContext when none is specified in configuration */
@Test//from  w w w . j  a  v  a  2  s.  c  o  m
public void testDefaultSessionContext() throws Exception {
    BehaviorTrackingManagerImpl impl = new BehaviorTrackingManagerImpl();
    impl.setApplication(manager.getApplication());
    impl.setExecutor(executor);
    impl.setPersister(persister);
    impl.setFlushThreshold(5);
    impl.afterPropertiesSet();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteUser("testUser");
    ServletRequestAttributes atts = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(atts);

    BehaviorEvent event = impl.createEvent("foo", "testEvent");
    assertNotNull("event created", event);

    assertEquals("testBT", event.getApplication());
    assertEquals("foo", event.getType());
    assertEquals("testEvent", event.getName());
    assertEquals("testUser", event.getUserId());
    assertEquals(request.getSession().getId(), event.getSessionId());
    assertNull(event.getParent());
    assertTrue(event.getData().isNull());
    assertNull(event.getError());
    assertNull(event.getStart());
    assertNull(event.getDurationNs());
    assertTrue(event.isRoot());
    assertFalse(event.isStarted());
    assertFalse(event.isEnded());

    //start the event.
    impl.start(event);
    impl.stop(event);

    assertEquals("all events now pending flush", 1, impl.getEventsPendingFlush());
    impl.flush();

    assertEquals("event queue flushed", 0, impl.getEventsPendingFlush());
}

From source file:com.liferay.document.library.webdav.test.BaseWebDAVTestCase.java

public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {//w ww  .  j a v  a 2s  . c  o  m
        mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
        Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
        headers = new HashMap<>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();

        for (StackTraceElement stackTraceElement : stackTraceElements) {
            String methodName = stackTraceElement.getMethodName();

            if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) {

                String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

                testName = StringUtil.removeSubstrings(testName, "WebDAV", "Test");

                headers.put("X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":"
                        + stackTraceElement.getLineNumber() + ")");

                break;
            }
        }
    }

    if (data != null) {
        mockHttpServletRequest.setContent(data);

        String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

        if (contentType != null) {
            mockHttpServletRequest.setContentType(contentType);
        } else {
            mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
        }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        mockHttpServletRequest.addHeader(key, value);
    }

    try {
        MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

        webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

        int statusCode = mockHttpServletResponse.getStatus();
        byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

        Map<String, String> responseHeaders = new HashMap<>();

        for (String name : mockHttpServletResponse.getHeaderNames()) {
            responseHeaders.put(name, mockHttpServletResponse.getHeader(name));
        }

        return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.finra.dm.ui.RequestLoggingFilterTest.java

private MockHttpServletRequest createServletRequest() {
    MockHttpServletRequest request = new MockHttpServletRequest(null, "/test");
    request.setQueryString("param=value");
    request.setMethod("POST");
    MockHttpSession session = new MockHttpSession();
    request.setContent(PAYLOAD_CONTENT.getBytes());
    request.setSession(session);/* w w w.  ja  va  2  s . c o  m*/
    request.setRemoteUser("Test Remote User");
    return request;
}