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

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

Introduction

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

Prototype

public MockHttpServletRequest() 

Source Link

Document

Create a new MockHttpServletRequest with a default MockServletContext .

Usage

From source file:nl.surfnet.coin.teams.service.interceptor.LoginInterceptorTest.java

@Test
public void testPreHandle() throws Exception {
    String remoteUser = "urn:collab:person:surfnet.nl:hansz";

    LoginInterceptor interceptor = new LoginInterceptor();

    OpenConextOAuthClient apiClient = mock(OpenConextOAuthClient.class);
    Person person = new Person();
    person.setId(remoteUser);/*  ww  w .  ja  v  a  2  s  . c  o  m*/
    when(apiClient.getPerson(remoteUser, null)).thenReturn(person);
    MemberAttributeService memberAttributeService = mock(MemberAttributeService.class);
    when(memberAttributeService.findAttributesForMemberId(person.getId()))
            .thenReturn(new ArrayList<MemberAttribute>());
    interceptor.setMemberAttributeService(memberAttributeService);

    interceptor.setApiClient(apiClient);
    interceptor.setTeamEnvironment(new TeamEnvironment());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("REMOTE_USER", remoteUser);
    request.addHeader("coin-user-status", "member");
    MockHttpServletResponse response = new MockHttpServletResponse();
    boolean loggedIn = interceptor.preHandle(request, response, null);
    assertTrue(loggedIn);
    Assert.assertNotNull(request.getSession().getAttribute("person"));
}

From source file:org.openmrs.module.feedback.web.StatusFormControllerTest.java

@Before
public void setUp() throws Exception {

    /* executed before the test is run */
    this.service = Context.getService(FeedbackService.class);
    this.controller = new StatusFormController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();

    /* this file is in the same folder of test resources where the hibernate mapping file is located */
    initializeInMemoryDatabase();//from w  ww. j a v a  2s.  c  o m
    executeDataSet("StatusDataset.xml");

    /* Sample data is loaded into the system */
    authenticate();
}

From source file:org.tonguetied.web.RequestUtilsTest.java

/**
 * Test method for {@link org.tonguetied.web.RequestUtils#getLongParameter(javax.servlet.http.HttpServletRequest, String)}.
 *///from   w  ww.ja v  a 2  s.c  o  m
@Test
public final void testGetLongParameter() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("test", "1");
    final Long value = RequestUtils.getLongParameter(request, "test");
    assertArrayEquals(new Long[] { 1L }, new Long[] { value });
}

From source file:org.n52.iceland.request.RequestContextTest.java

@Test
public void ip6to4Addresses() {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setRemoteAddr("2002:836b:1714::836b:1714");
    RequestContext fromRequest = RequestContext.fromRequest(mockRequest);
    Assert.assertEquals("131.107.23.20", fromRequest.getIPAddress().get().toString());
}

From source file:org.tonguetied.web.PaginationUtilsTest.java

License:asdf

/**
 * @throws java.lang.Exception/*w  w w .ja va 2  s .co m*/
 */
@Before
public void setUp() throws Exception {
    request = new MockHttpServletRequest();
    request.setParameter(PAGE_PARAM, "3");
    request.setParameter(PAGE_SORT, "test");
    request.setParameter(PAGE_ORDER, "2");

    MockHttpSession session = new MockHttpSession();
    session.setAttribute(PAGE_PARAM_SESSION, Integer.valueOf("9"));
    request.setSession(session);
}

From source file:edu.duke.cabig.c3pr.domain.scheduler.runtime.job.ScheduledJob.java

/**
 * @author Vinay Gangoli/* w w w.j a  va 2  s .c om*/
 * All jobs must extend this class. this handles the hibernate session scope for the jobs.
 */
public void execute(JobExecutionContext context) throws JobExecutionException {

    logger.debug("Executing Scheduled Job");
    WebRequest webRequest = new ServletWebRequest(new MockHttpServletRequest());
    OpenSessionInViewInterceptor interceptor = null;

    try {
        // init the member variables
        scheduler = context.getScheduler();
        jobDetail = context.getJobDetail();
        applicationContext = (ApplicationContext) scheduler.getContext().get("applicationContext");

        plannedNotificationDao = (PlannedNotificationDao) applicationContext.getBean("plannedNotificationDao");
        recipientScheduledNotificationDao = (RecipientScheduledNotificationDao) applicationContext
                .getBean("recipientScheduledNotificationDao");

        interceptor = (OpenSessionInViewInterceptor) applicationContext.getBean("openSessionInViewInterceptor");
        interceptor.preHandle(webRequest);

        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Integer plannedNotificationId = jobDataMap.getInt("plannedNotificationId");
        //PlannedNotification plannedNotification = plannedNotificationDao.getInitializedPlannedNotificationById(plannedNotificationId);
        PlannedNotification plannedNotification = plannedNotificationDao.getById(plannedNotificationId);

        setAuditInfo();
        RecipientScheduledNotification recipientScheduledNotification = null;
        if (jobDataMap.containsKey("recipientScheduledNotificationId")) {
            Integer recipientScheduledNotificationId = jobDataMap.getInt("recipientScheduledNotificationId");
            //recipientScheduledNotification = recipientScheduledNotificationDao.getInitializedRecipientScheduledNotificationById(recipientScheduledNotificationId);
            recipientScheduledNotification = recipientScheduledNotificationDao
                    .getById(recipientScheduledNotificationId);
        }

        if (plannedNotification.getEventName() != null) {
            try {
                processJob(jobDataMap, recipientScheduledNotification, plannedNotification);
            } catch (JobExecutionException jee) {
                logger.error(jee.getMessage());
            }
        }
        //try commenting out the postHandle ...might not be necessary!
        interceptor.postHandle(webRequest, null);

    } catch (RuntimeException exception) {
        logger.error(exception.getMessage());
        // Get cause if present Throwable t = ((HibernateJdbcException) exception).getRootCause(); while (t != null) { 
        //t = t.getCause(); 
    } catch (Exception e) {
        logger.error("execution of job failed", e);
    } finally {
        interceptor.afterCompletion(webRequest, null);
    }
}

From source file:org.openmrs.web.controller.concept.ConceptStopWordListControllerTest.java

/**
 * @see ConceptStopWordListController#showForm(javax.servlet.http.HttpSession)
 *///from   w w  w  .ja  v a  2s  .c om
@SuppressWarnings("unchecked")
@Test
@Verifies(value = "should add all ConceptStopWords in session attribute", method = "showForm(HttpSession)")
public void showForm_shouldAddAllConceptStopWordsInSessionAttribute() throws Exception {
    ConceptStopWordListController controller = (ConceptStopWordListController) applicationContext
            .getBean("conceptStopWordListController");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setMethod("POST");

    controller.showForm(mockRequest.getSession());

    List<ConceptStopWord> conceptStopWordList = (List<ConceptStopWord>) mockRequest.getSession()
            .getAttribute("conceptStopWordList");
    Assert.assertNotNull(conceptStopWordList);
    Assert.assertEquals(4, conceptStopWordList.size());
}

From source file:org.openmrs.module.feedback.web.AddPredefinedSubjectFormControllerTest.java

@Before
public void setUp() throws Exception {

    /* executed before the test is run */
    this.service = Context.getService(FeedbackService.class);
    this.controller = new FeedbackAdminListController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();

    /* this file is in the same folder of test resources where the hibernate mapping file is located */
    initializeInMemoryDatabase();/*  w  ww .j  a va  2s .c  o  m*/
    executeDataSet("PredefinedSubjectDataset.xml");

    /* Sample data is loaded into the system */
    authenticate();
}

From source file:org.openmrs.module.feedback.web.SeverityFormControllerTest.java

@Before
public void setUp() throws Exception {

    /* executed before the test is run */
    this.service = Context.getService(FeedbackService.class);
    this.controller = new SeverityFormController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();

    /* this file is in the same folder of test resources where the hibernate mapping file is located */
    initializeInMemoryDatabase();/*from   w w  w .  j  av a2s.  c o  m*/
    executeDataSet("SeverityDataset.xml");

    /* Sample data is loaded into the system */
    authenticate();
}

From source file:com.surevine.alfresco.audit.MultiReadHttpServletRequestTest.java

MultiReadHttpServletRequest testRequestWithSize(int size) throws Exception {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    byte[] testData = getTestData(size);

    mockRequest.setContent(testData); // A request

    MultiReadHttpServletRequest request = new MultiReadHttpServletRequest(mockRequest);

    byte[] result1 = IOUtils.toByteArray(request.getInputStream());
    assertTrue("result1 should equal the input array. [" + testData.length + ", " + result1.length + "]",
            Arrays.equals(testData, result1));

    result1 = null;/*from   w w  w  .j ava2  s .  c  o  m*/
    System.gc();

    byte[] result2 = IOUtils.toByteArray(request.getInputStream());
    assertTrue("result2 should equal the output array [" + testData.length + ", " + result2.length + "]",
            Arrays.equals(testData, result2));

    testData = null;
    result2 = null;
    System.gc();

    return request;
}