List of usage examples for org.springframework.mock.web MockHttpServletRequest setMethod
public void setMethod(@Nullable String method)
From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void testServiceOnlyEndpointWithScopeMustFail() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.addParameter("client_id", serviceApplication.getExternalId()); req.addParameter("client_secret", serviceApplication.getSecret()); req.addParameter("grant_type", "client_credentials"); req.setMethod("POST"); req.setPathInfo("/access_token"); try {//from w w w.ja v a2s.c o m oauthServlet.service(req, res); Assert.assertEquals("must return status OK", 200, res.getStatus()); String tokenJson = res.getContentAsString(); final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject(); final String accessToken = token.get(ACCESS_TOKEN).getAsString(); Assert.assertTrue("response must be a valid json and have access_token field", token.has(ACCESS_TOKEN) && accessToken.length() > 0); Response result = target("bennu-oauth").path("test").path("service-only-with-scope") .queryParam(ACCESS_TOKEN, accessToken).request().get(Response.class); Assert.assertNotEquals("request must fail", 200, result.getStatus()); } catch (ServletException | IOException e) { Assert.fail(e.getMessage()); } }
From source file:org.openmrs.web.controller.ConceptFormControllerTest.java
/** * Test adding a concept with a preferred name, short name, description and synonyms. * /*ww w .jav a 2s .c o m*/ * @throws Exception */ @Test public void shouldReplacePreviousDescription() throws Exception { final String EXPECTED_PREFERRED_NAME = "no such concept"; final String EXPECTED_SHORT_NAME = "nonesuch"; final String ORIGINAL_DESCRIPTION = "this is indescribable"; final String EXPECTED_DESCRIPTION = "this is not really a concept"; final String EXPECTED_SYNONYM_A = "phantom"; final String EXPECTED_SYNONYM_B = EXPECTED_SHORT_NAME; final String EXPECTED_SYNONYM_C = "mock"; ConceptService cs = Context.getConceptService(); // first, add the concept with an original description Concept conceptToUpdate = new Concept(); conceptToUpdate.addName(new ConceptName("demo name", Context.getLocale())); ConceptDescription originalConceptDescription = new ConceptDescription(); originalConceptDescription.setLocale(britishEn); originalConceptDescription.setDescription(ORIGINAL_DESCRIPTION); conceptToUpdate.addDescription(originalConceptDescription); cs.saveConcept(conceptToUpdate); // then submit changes through the controller ConceptFormController conceptFormController = (ConceptFormController) applicationContext .getBean("conceptForm"); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); mockRequest.setMethod("POST"); mockRequest.setParameter("action", ""); mockRequest.setParameter("synonymsByLocale[en_GB][0].name", EXPECTED_SYNONYM_A); mockRequest.setParameter("synonymsByLocale[en_GB][1].name", EXPECTED_SYNONYM_B); mockRequest.setParameter("synonymsByLocale[en_GB][2].name", EXPECTED_SYNONYM_C); mockRequest.setParameter("shortNamesByLocale[en_GB].name", EXPECTED_SHORT_NAME); mockRequest.setParameter("descriptionsByLocale[en_GB].description", EXPECTED_DESCRIPTION); mockRequest.setParameter("namesByLocale[en_GB].name", EXPECTED_PREFERRED_NAME); mockRequest.setParameter("concept.datatype", "1"); ModelAndView mav = conceptFormController.handleRequest(mockRequest, response); assertNotNull(mav); assertTrue(mav.getModel().isEmpty()); Concept actualConcept = cs.getConceptByName(EXPECTED_PREFERRED_NAME); assertNotNull(actualConcept); assertNotNull(actualConcept.getDescription(britishEn)); assertEquals(EXPECTED_DESCRIPTION, actualConcept.getDescription(britishEn).getDescription()); }
From source file:org.openmrs.web.controller.ConceptFormControllerTest.java
/** * Test adding a concept with a preferred name, short name, description and synonyms. * /*from www .jav a2 s . c om*/ * @throws Exception */ @Test public void shouldAddConceptWithAllNamingSpecified() throws Exception { final String EXPECTED_PREFERRED_NAME = "no such concept"; final String EXPECTED_SHORT_NAME = "nonesuch"; final String EXPECTED_DESCRIPTION = "this is not really a concept"; final String EXPECTED_SYNONYM_A = "phantom"; final String EXPECTED_SYNONYM_B = "imaginary"; final String EXPECTED_SYNONYM_C = "mock"; AdministrationService as = Context.getAdministrationService(); GlobalProperty gp = as.getGlobalPropertyObject(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST); gp.setPropertyValue("en_GB, en_US"); as.saveGlobalProperty(gp); ConceptService cs = Context.getConceptService(); // make sure the concept doesn't already exist Concept conceptToAdd = cs.getConceptByName(EXPECTED_PREFERRED_NAME); assertNull(conceptToAdd); ConceptFormController conceptFormController = (ConceptFormController) applicationContext .getBean("conceptForm"); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); mockRequest.setMethod("POST"); mockRequest.setParameter("action", ""); mockRequest.setParameter("synonymsByLocale[en_GB][0].name", EXPECTED_SYNONYM_A); mockRequest.setParameter("synonymsByLocale[en_GB][1].name", EXPECTED_SYNONYM_B); mockRequest.setParameter("synonymsByLocale[en_GB][2].name", EXPECTED_SYNONYM_C); mockRequest.setParameter("shortNamesByLocale[en_GB].name", EXPECTED_SHORT_NAME); mockRequest.setParameter("descriptionsByLocale[en_GB].description", EXPECTED_DESCRIPTION); mockRequest.setParameter("namesByLocale[en_GB].name", EXPECTED_PREFERRED_NAME); mockRequest.setParameter("concept.datatype", "1"); ModelAndView mav = conceptFormController.handleRequest(mockRequest, response); assertNotNull(mav); assertTrue(mav.getModel().isEmpty()); Concept actualConcept = cs.getConceptByName(EXPECTED_PREFERRED_NAME); assertNotNull(actualConcept); Collection<ConceptName> actualNames = actualConcept.getNames(); assertEquals(5, actualNames.size()); assertEquals(EXPECTED_PREFERRED_NAME, actualConcept.getFullySpecifiedName(britishEn).getName()); assertNotNull(actualConcept.getShortNameInLocale(britishEn)); assertEquals(EXPECTED_SHORT_NAME, actualConcept.getShortNameInLocale(britishEn).getName()); assertNotNull(actualConcept.getDescription(britishEn)); assertEquals(EXPECTED_DESCRIPTION, actualConcept.getDescription(britishEn).getDescription()); }
From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void testServiceOnlyWithScopeEndpoint() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.addParameter("client_id", serviceApplicationWithScope.getExternalId()); req.addParameter("client_secret", serviceApplicationWithScope.getSecret()); req.addParameter("grant_type", "client_credentials"); req.setMethod("POST"); req.setPathInfo("/access_token"); try {/*w w w. j a va2 s . c o m*/ oauthServlet.service(req, res); Assert.assertEquals("must return status OK", 200, res.getStatus()); String tokenJson = res.getContentAsString(); final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject(); final String accessToken = token.get(ACCESS_TOKEN).getAsString(); Assert.assertTrue("response must be a valid json and have access_token field", token.has(ACCESS_TOKEN) && accessToken.length() > 0); String result = target("bennu-oauth").path("test").path("service-only-with-scope") .queryParam(ACCESS_TOKEN, accessToken).request().get(String.class); Assert.assertEquals("this is an endpoint with SERVICE scope, serviceOnly", result); } catch (ServletException | IOException e) { Assert.fail(e.getMessage()); } }
From source file:org.openmrs.web.controller.ConceptFormControllerTest.java
/** * @see ConceptFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException) * @verifies edit short name when there are multiple allowed locales *///from ww w . j a va2 s. c om @Test public void onSubmit_shouldEditShortNameWhenThereAreMultipleAllowedLocales() throws Exception { AdministrationService as = Context.getAdministrationService(); GlobalProperty gp = as.getGlobalPropertyObject(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST); gp.setPropertyValue(britishEn + ", en_US"); as.saveGlobalProperty(gp); final Integer conceptId = 5089; Concept concept = conceptService.getConcept(conceptId); assertEquals("WT", concept.getShortNameInLocale(britishEn).getName()); ConceptFormController controller = applicationContext.getBean("conceptForm", ConceptFormController.class); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); final String newShortName = "WGT"; request.setMethod("POST"); request.setParameter("action", "Save Concept"); request.setParameter("conceptId", conceptId.toString()); request.setParameter("shortNamesByLocale[" + britishEn + "].name", newShortName); request.setParameter("shortNamesByLocale[en_US].name", ""); ModelAndView mav = controller.handleRequest(request, response); assertNotNull(mav); assertTrue(mav.getModel().isEmpty()); concept = conceptService.getConcept(conceptId); ConceptName shortConceptName = concept.getShortNameInLocale(britishEn); assertNotNull(shortConceptName); assertEquals(newShortName, shortConceptName.getName()); }
From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void getServiceAccessTokenHeaderEmptyTest() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); Authenticate.unmock();/*from w ww . ja va2s. c o m*/ String clientSecret = ""; req.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(clientSecret.getBytes(StandardCharsets.UTF_8))); req.addParameter(GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS); req.setMethod("POST"); req.setPathInfo("/access_token"); try { oauthServlet.service(req, res); Assert.assertEquals("must return BAD_REQUEST", 400, res.getStatus()); } catch (ServletException | IOException e) { Assert.fail(e.getMessage()); } }
From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void getServiceAccessTokenWithWrongClientSecretTest() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.addParameter("client_id", serviceApplication.getExternalId()); req.addParameter("client_secret", BaseEncoding.base64().encode((serviceApplication.getExternalId() + ":lasdlkasldksladkalskdsal") .getBytes(StandardCharsets.UTF_8))); req.addParameter("grant_type", "client_credentials"); req.setMethod("POST"); req.setPathInfo("/access_token"); try {//from w ww. j a v a2 s.c o m oauthServlet.service(req, res); Assert.assertEquals("must return status BAD_REQUEST", 400, res.getStatus()); } catch (ServletException | IOException e) { Assert.fail(e.getMessage()); } }
From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void getServiceAccessTokenHeaderTest() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); Authenticate.unmock();//w w w .j av a 2 s . co m String clientSecret = serviceApplication.getExternalId() + ":" + serviceApplication.getSecret(); req.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(clientSecret.getBytes(StandardCharsets.UTF_8))); req.addParameter(GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS); req.setMethod("POST"); req.setPathInfo("/access_token"); try { oauthServlet.service(req, res); Assert.assertEquals("must return status OK", 200, res.getStatus()); String tokenJson = res.getContentAsString(); final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject(); Assert.assertTrue("response must be a valid json and have access_token field", token.has(ACCESS_TOKEN) && token.get(ACCESS_TOKEN).getAsString().length() > 0); } catch (ServletException | IOException e) { Assert.fail(e.getMessage()); } }
From source file:org.openmrs.web.controller.ConceptFormControllerTest.java
/** * Test to make sure a new patient form can save a person relationship * // w w w . ja v a 2 s . c o m * @throws Exception */ @Test public void shouldNotDeleteConceptsWhenConceptsAreLocked() throws Exception { // this dataset should lock the concepts executeDataSet("org/openmrs/web/include/ConceptFormControllerTest.xml"); ConceptService cs = Context.getConceptService(); // set up the controller ConceptFormController controller = (ConceptFormController) applicationContext.getBean("conceptForm"); controller.setApplicationContext(applicationContext); controller.setSuccessView("index.htm"); controller.setFormView("concept.form"); // set up the request and do an initial "get" as if the user loaded the // page for the first time MockHttpServletRequest request = new MockHttpServletRequest("GET", "/dictionary/concept.form?conceptId=3"); request.setSession(new MockHttpSession(null)); HttpServletResponse response = new MockHttpServletResponse(); controller.handleRequest(request, response); // set this to be a page submission request.setMethod("POST"); request.addParameter("action", "Delete Concept"); // so that the form is processed // send the parameters to the controller ModelAndView mav = controller.handleRequest(request, response); Assert.assertNotSame("The purge attempt should have failed!", "index.htm", mav.getViewName()); Assert.assertNotNull(cs.getConcept(3)); }
From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void testServiceApplicationWithUnexistingScope() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); Authenticate.unmock();//from w w w . ja v a 2s . c o m User user = createUser("testServiceApplicationWithUnexistingScope", "John", "Doe", "John Doe", "john.doe@fenixedu.org"); ServiceApplication serviceApplication = new ServiceApplication(); serviceApplication.setAuthor(user); req.addParameter("client_id", serviceApplication.getExternalId()); req.addParameter("client_secret", serviceApplication.getSecret()); req.addParameter("grant_type", "client_credentials"); req.setMethod("POST"); req.setPathInfo("/access_token"); try { oauthServlet.service(req, res); Assert.assertEquals("must return status OK", 200, res.getStatus()); String tokenJson = res.getContentAsString(); final String serviceAccessToken = new JsonParser().parse(tokenJson).getAsJsonObject() .get("access_token").getAsString(); Response response = target("bennu-oauth").path("test").path("service-only-with-unexisting-scope") .queryParam("access_token", serviceAccessToken).request().get(); Assert.assertNotEquals("request must fail since scope does not exist", 200, response.getStatus()); } catch (ServletException | IOException e) { Assert.fail(e.getMessage()); } }