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

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

Introduction

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

Prototype

public void setParameter(String name, String... values) 

Source Link

Document

Set an array of values for the specified HTTP parameter.

Usage

From source file:org.jasig.cas.support.oauth.web.OAuth20TokenRefreshTokenControllerTests.java

@Test
public void verifyNoClientSecret() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.REFRESH_TOKEN);
    mockRequest.setParameter(OAuthConstants.REFRESH_TOKEN, RT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//from  w  w w. ja  v a  2  s. c o m
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("application/json", mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + new InvalidParameterException(OAuthConstants.CLIENT_SECRET).getMessage() + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.OrderController1_8Test.java

@Test
public void shouldGetOrderAsRef() throws Exception {

    MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
    req.setParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, Representation.REF.getRepresentation());
    SimpleObject result = deserialize(handle(req));

    Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "display"));
    Assert.assertNull(PropertyUtils.getProperty(result, "concept"));
    Util.log("order as ref", result);
}

From source file:no.dusken.common.plugin.control.admin.PluginStoreControllerTest.java

@Test
public void testSubmit() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/pluginstore");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setParameter("no.dusken.common.plugin.p1-setting1", "newValue1p1");
    request.setParameter("no.dusken.common.plugin.p1-setting2", "newValue2p1");
    request.setParameter("no.dusken.common.plugin.p2-setting1", "newValue1p2");
    request.setParameter("no.dusken.common.plugin.p2-setting2", "newValue2p2");
    ModelAndView mav = pluginStoreController.handleRequestInternal(request, response);

    Map<DuskenPlugin, Map<String, String>> plugins = (Map<DuskenPlugin, Map<String, String>>) mav.getModel()
            .get("plugins");
    assertNotNull("Pluginsmap was null", plugins);
    assertEquals("Pluginsmap contained wrong number of plugins", 2, plugins.size());
    for (DuskenPlugin p : plugins.keySet()) {
        PluginStore store = pluginStoreProvider.getStore(p);
        String setting1 = store.getString("setting1", "default");
        String setting2 = store.getString("setting2", "default");

        assertEquals("Setting1 has wrong value", "newValue1".concat(p.getPluginName(null)), setting1);
        assertEquals("Setting2 has wrong value", "newValue2".concat(p.getPluginName(null)), setting2);
    }/*from  ww w. j a  v  a2s .  c o  m*/

    Map updatemap = (Map) mav.getModel().get("updatemap");
    assertNotNull("Map did not contain updatemap", updatemap);
    assertEquals("Wrong number of updatestrings", 4, updatemap.size());
}

From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeClientPrincipalTokensControllerTests.java

@Test
public void verifyInvalidAccessToken() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenThrow(new InvalidTokenException("error"));
    when(centralOAuthService.getPersonalAccessToken(AT_ID)).thenReturn(null);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//from  w  w w.  j a va  2  s.c o  m
    assertEquals(HttpStatus.SC_UNAUTHORIZED, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.UNAUTHORIZED_REQUEST
            + "\",\"error_description\":\"" + OAuthConstants.INVALID_ACCESS_TOKEN_DESCRIPTION + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.jasig.cas.support.oauth.web.OAuth20TokenRefreshTokenControllerTests.java

@Test
public void verifyNoRefreshToken() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(RT_ID, RefreshToken.class)).thenThrow(new InvalidTokenException("error"));

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.REFRESH_TOKEN);
    mockRequest.setParameter(OAuthConstants.REFRESH_TOKEN, RT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/*w  w w.  jav a  2 s.  c  o  m*/
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("application/json", mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.INVALID_REFRESH_TOKEN_DESCRIPTION + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.OrderController1_8Test.java

@Test
public void shouldGetDrugOrderAsRef() throws Exception {
    MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + DRUG_ORDER_UUID);
    req.setParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, Representation.REF.getRepresentation());
    SimpleObject result = deserialize(handle(req));

    Assert.assertEquals(DRUG_ORDER_UUID, PropertyUtils.getProperty(result, "uuid"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "display"));
    Assert.assertNull(PropertyUtils.getProperty(result, "concept"));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeClientPrincipalTokensControllerTests.java

@Test
public void verifyFailedToRevokeTokens() throws Exception {
    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken);
    when(centralOAuthService.revokeClientPrincipalTokens(accessToken, CLIENT_ID)).thenReturn(false);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.REVOKE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);// w w  w.  jav  a 2s.  c  o  m
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.INVALID_ACCESS_TOKEN_DESCRIPTION + "\"}";

    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.soybeanMilk.test.unit.web.TestDefaultWebExecutor.java

@Test
public void execute_resolverClass_typeVariable() throws Exception {
    WebObjectSource os = createWebObjectSource();
    MockHttpServletRequest request = (MockHttpServletRequest) os.getRequest();

    String id = "my id";
    String name = "my name";

    request.setParameter("typeVariable.id", id);
    request.setParameter("typeVariable.name", name);

    webExecutor.execute("typeVariableTest", os);

    TestBeanSub result = os.get("request.testResult");

    Assert.assertEquals(result.getId(), id);
    Assert.assertEquals(result.getName(), name);
}

From source file:org.jasig.cas.support.oauth.web.OAuth20TokenRefreshTokenControllerTests.java

@Test
public void verifyOK() throws Exception {
    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.getCreationTime()).thenReturn(new Date().getTime());

    final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, CLIENT_SECRET);

    final RefreshToken refreshToken = mock(RefreshToken.class);
    when(refreshToken.getId()).thenReturn(RT_ID);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getTicket()).thenReturn(ticketGrantingTicket);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(RT_ID, RefreshToken.class)).thenReturn(refreshToken);
    when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(service);
    when(centralOAuthService.grantOfflineAccessToken(refreshToken)).thenReturn(accessToken);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.REFRESH_TOKEN);
    mockRequest.setParameter(OAuthConstants.REFRESH_TOKEN, RT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.setTimeout(TIMEOUT);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//from ww  w  .  j av  a 2  s .c o m
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals("application/json", mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();
    final String expected = "{\"token_type\":\"" + OAuthConstants.BEARER_TOKEN + "\",\"expires_in\":\""
            + TIMEOUT + "\",\"refresh_token\":\"" + RT_ID + "\",\"access_token\":\"" + AT_ID + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("token_type").asText(), receivedObj.get("token_type").asText());
    assertTrue("received expires_at greater or equal to expected",
            expectedObj.get("expires_in").asInt() >= receivedObj.get("expires_in").asInt());
    assertEquals(expectedObj.get("access_token").asText(), receivedObj.get("access_token").asText());
}

From source file:org.openmrs.module.coreapps.htmlformentry.EncounterDiagnosesTagHandlerComponentTest.java

@Test
public void testHandleSubmissionHandlesValidSubmissionEnteringForm() throws Exception {
    final int codedConceptId = 11;

    ObjectMapper jackson = new ObjectMapper();
    ArrayNode json = jackson.createArrayNode();
    ObjectNode diagnosisNode = json.addObject();
    diagnosisNode.put("certainty", "PRESUMED");
    diagnosisNode.put("order", "PRIMARY");
    diagnosisNode.put("diagnosis", CodedOrFreeTextAnswer.CONCEPT_PREFIX + codedConceptId);
    final String jsonToSubmit = jackson.writeValueAsString(json);

    final DiagnosisMetadata dmd = emrApiProperties.getDiagnosisMetadata();

    final Date date = new Date();

    new RegressionTestHelper() {
        @Override/*from ww w .  j ava2  s. c  om*/
        public String getXmlDatasetPath() {
            return "";
        }

        @Override
        public String getFormName() {
            return "encounterDiagnosesSimpleForm";
        }

        @Override
        public Map<String, Object> getFormEntrySessionAttributes() {
            Map<String, Object> attributes = new HashMap<String, Object>();
            attributes.put("uiUtils", new TestUiUtils() {
                @Override
                public String includeFragment(String providerName, String fragmentId,
                        Map<String, Object> config) throws PageAction {
                    return "[[ included fragment " + providerName + "." + fragmentId + " here ]]";
                }
            });
            return attributes;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:" };
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.setParameter(widgets.get("Date:"), dateAsString(date));
            request.setParameter(widgets.get("Location:"), "2");
            request.setParameter(widgets.get("Provider:"), "1");
            request.setParameter("encounterDiagnoses", jsonToSubmit);
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertProvider(1);
            results.assertLocation(2);
            results.assertObsCreatedCount(1);
            results.assertObsGroupCreated(dmd.getDiagnosisSetConcept().getConceptId(),
                    dmd.getDiagnosisCertaintyConcept().getId(), dmd.getConceptFor(Diagnosis.Certainty.PRESUMED),
                    dmd.getDiagnosisOrderConcept().getId(), dmd.getConceptFor(Diagnosis.Order.PRIMARY),
                    dmd.getCodedDiagnosisConcept().getId(),
                    Context.getConceptService().getConcept(codedConceptId));
        }
    }.run();
}