/** * Tests getting the project's collections * * @throws InvalidXmlException * @throws BizInternalException * @throws BizPolicyException * @throws IOException */ @Test public void testGetProjectsCollections() throws InvalidXmlException, BizInternalException, BizPolicyException, IOException { final String mimeType = "application/xml"; final MockHttpServletRequest mockReq = newMockRequest("GET", projectOne.getId() + "/collections", "test.org", 80); MockHttpServletResponse resp = new MockHttpServletResponse(); when(requestUtil.buildRequestUrl(any(HttpServletRequest.class))) .thenReturn(projectOne.getId() + "/collections"); controller.setAuthenticatedUser(admin); controller.handleProjectCollectionsGetRequest( projectOne.getId(), mimeType, null, mockReq, resp); assertNotNull(resp); assertEquals(resp.getErrorMessage(), 200, resp.getStatus()); Bop returnedBop = businessObjectBuilder.buildBusinessObjectPackage( new ByteArrayInputStream(resp.getContentAsByteArray())); assertNotNull(returnedBop); Set<Collection> collections = returnedBop.getCollections(); assertNotNull(collections); assertEquals(1, collections.size()); }
@Test public void testCommenceWithHtmlAndJsonAccept() throws Exception { request.addHeader( "Accept", String.format("%s,%s", MediaType.TEXT_HTML_VALUE, MediaType.APPLICATION_JSON)); entryPoint.commence(request, response, new BadCredentialsException("Bad")); assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus()); assertEquals(null, response.getErrorMessage()); }
@Test public void testCommenceWithHtmlAccept() throws Exception { request.addHeader("Accept", MediaType.TEXT_HTML_VALUE); entryPoint.commence(request, response, new BadCredentialsException("Bad")); // TODO: maybe use forward / redirect for HTML content? assertEquals(HttpServletResponse.SC_NOT_ACCEPTABLE, response.getStatus()); assertEquals("", response.getContentAsString()); assertEquals(null, response.getErrorMessage()); }
@Test public void givenNoAPIKey_WhenCallingSecureAPI_ThenShouldNotBeAllowed() throws Exception { setSecurity(sally_admin); // Sally is Authorised and has not API Key request.setRequestURI("/api/v1/fortress/"); // exception.expect(SecurityException.class); // ToDo: Move to MVC tests TestCase.assertFalse(apiKeyInterceptor.preHandle(request, response, null)); TestCase.assertNotNull(response.getErrorMessage()); TestCase.assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatus()); }
@Test public void testCommenceWithEmptyAccept() throws Exception { entryPoint.commence(request, response, new BadCredentialsException("Bad")); assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus()); assertEquals( "{\"error\":\"unauthorized\",\"error_description\":\"Bad\"}", response.getContentAsString()); assertTrue( MediaType.APPLICATION_JSON.isCompatibleWith(MediaType.valueOf(response.getContentType()))); assertEquals(null, response.getErrorMessage()); }
@Test public void testCommenceWithXml() throws Exception { request.addHeader("Accept", MediaType.APPLICATION_XML_VALUE); entryPoint.commence(request, response, new BadCredentialsException("Bad")); assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus()); assertEquals( "<oauth><error_description>Bad</error_description><error>unauthorized</error></oauth>", response.getContentAsString()); assertEquals(MediaType.APPLICATION_XML_VALUE, response.getContentType()); assertEquals(null, response.getErrorMessage()); }
@Test public void testCommenceWithJson() throws Exception { request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE); entryPoint.commence(request, response, new BadCredentialsException("Bad")); assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus()); assertEquals( "{\"error\":\"unauthorized\",\"error_description\":\"Bad\"}", response.getContentAsString()); assertEquals(MediaType.APPLICATION_JSON_VALUE, response.getContentType()); assertEquals(null, response.getErrorMessage()); }
/** * Tests that the project metadata is successfully updated. * * @throws InvalidXmlException * @throws ProjectServiceException * @throws BizPolicyException * @throws IOException */ @Test public void testUpdateProject() throws InvalidXmlException, BizInternalException, BizPolicyException, IOException { Project newProject = new Project(projectOne); newProject.setName("Update"); newProject.setDescription("foo-update"); newProject.setEndDate(new DateTime(2014, 2, 10, 0, 0)); final String mimeType = "application/xml"; final MockHttpServletRequest mockReq = newMockRequest("PUT", projectOne.getId(), "test.org", 80); MockHttpServletResponse resp = new MockHttpServletResponse(); ByteArrayOutputStream sink = new ByteArrayOutputStream(); businessObjectBuilder.buildProject(newProject, sink); controller.setAuthenticatedUser(admin); controller.handleUpdateProjectRequest( projectOne.getId(), mimeType, sink.toByteArray(), mockReq, resp); assertNotNull(resp); assertEquals(resp.getErrorMessage(), 200, resp.getStatus()); Bop bop = businessObjectBuilder.buildBusinessObjectPackage( new ByteArrayInputStream(resp.getContentAsByteArray())); assertNotNull(bop); Set<Project> projects = bop.getProjects(); assertNotNull(projects); assertEquals(1, projects.size()); Project returnedProject = projects.iterator().next(); assertNotNull(returnedProject); assertTrue(returnedProject.getName().equalsIgnoreCase(newProject.getName())); assertTrue(returnedProject.getDescription().equalsIgnoreCase(newProject.getDescription())); assertTrue(returnedProject.getEndDate().equals(newProject.getEndDate())); assertTrue(returnedProject.getFundingEntity().equalsIgnoreCase(newProject.getFundingEntity())); }
/** * Test getting a project * * @throws InvalidXmlException * @throws BizInternalException * @throws BizPolicyException * @throws IOException */ @Test public void testGetProject() throws InvalidXmlException, BizInternalException, BizPolicyException, IOException { final String mimeType = "application/xml"; final MockHttpServletRequest mockReq = newMockRequest("GET", projectOne.getId(), "test.org", 80); MockHttpServletResponse resp = new MockHttpServletResponse(); controller.setAuthenticatedUser(admin); controller.handleProjectGetRequest(projectOne.getId(), mimeType, null, mockReq, resp); assertNotNull(resp); assertEquals(resp.getErrorMessage(), 200, resp.getStatus()); Bop bop = businessObjectBuilder.buildBusinessObjectPackage( new ByteArrayInputStream(resp.getContentAsByteArray())); assertNotNull(bop); assertEquals(1, bop.getProjects().size()); assertEquals(projectOne, bop.getProjects().iterator().next()); }
public void testNew() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request = new MockHttpServletRequest("GET", "/workCodeForm.html"); MockHttpServletResponse response = new MockHttpServletResponse(); simpleDispatcherServlet.service(request, response); ModelAndView mv = child.handleRequest(request, response); Map m = mv.getModel(); log.debug("m = " + m); WorkCodeFormBacking backing = (WorkCodeFormBacking) m.get("workCodeFormBacking"); log.debug(Constants.toReflexString(backing)); MockPageContext ctx = new MockPageContext(); Iterator i = m.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); ctx.setAttribute(key, m.get(key)); } RequestContext r = new RequestContext(request, m); BindStatus b = r.getBindStatus("workCodeFormBacking.workCode.description"); log.debug("expression = " + b.getExpression()); log.debug("value = " + b.getValue()); String description = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.description}", String.class, ctx); // request.addParameter(name, description); String parttime = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.parttime}", String.class, ctx); String code = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.code}", String.class, ctx); String id = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.workCode.id}", String.class, ctx); String save = (String) ExpressionEvaluationUtils.evaluate( "test", "${workCodeFormBacking.save}", String.class, ctx); log.debug("description = " + description); log.debug("parttime = " + parttime); log.debug("code = " + code); log.debug("id = " + id); log.debug("save = " + save); HashMap newMap = new HashMap(); newMap.put( r.getBindStatus("workCodeFormBacking.workCode.description").getExpression(), description); newMap.put(r.getBindStatus("workCodeFormBacking.workCode.parttime").getExpression(), parttime); newMap.put(r.getBindStatus("workCodeFormBacking.workCode.code").getExpression(), code); newMap.put(r.getBindStatus("workCodeFormBacking.workCode.id").getExpression(), id); newMap.put(r.getBindStatus("workCodeFormBacking.save").getExpression(), "save"); request = new MockHttpServletRequest("POST", "/workCodeForm.html"); i = newMap.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); log.debug("add parameter " + key + " " + newMap.get(key)); request.addParameter(key, (String) newMap.get(key)); } mv = child.handleRequest(request, response); m = mv.getModel(); log.debug("m = " + m); BeanPropertyBindingResult br = (BeanPropertyBindingResult) m.get("org.springframework.validation.BindingResult.workCodeFormBacking"); log.debug("field errors " + br.getFieldErrorCount()); log.debug("errors " + br.getErrorCount()); assertTrue(br.getFieldError("workCode.code") != null); assertTrue(br.getFieldError("workCode.description") != null); backing = (WorkCodeFormBacking) m.get("workCodeFormBacking"); log.debug(response.getErrorMessage()); log.debug(Constants.toReflexString(backing)); log.debug("view = " + mv.getViewName()); assertTrue("workCodeForm".equals(mv.getViewName())); }
/** * Tests that admin can be added and removed from a project * * @throws IOException * @throws BizPolicyException * @throws ProjectServiceException * @throws InvalidXmlException * @throws BizInternalException */ @Test public void testUpdateProjectAdmin() throws InvalidXmlException, ProjectServiceException, BizPolicyException, IOException, BizInternalException { // Test adding an admin to the project. Project newProject = new Project(projectOne); newProject.addPi(user.getId()); final String mimeType = "application/xml"; final MockHttpServletRequest mockReq = newMockRequest("PUT", projectOne.getId(), "test.org", 80); MockHttpServletResponse resp = new MockHttpServletResponse(); ByteArrayOutputStream sink = new ByteArrayOutputStream(); businessObjectBuilder.buildProject(newProject, sink); controller.setAuthenticatedUser(admin); controller.handleUpdateProjectRequest( newProject.getId(), mimeType, sink.toByteArray(), mockReq, resp); assertNotNull(resp); assertEquals(resp.getErrorMessage(), 200, resp.getStatus()); Bop bop = businessObjectBuilder.buildBusinessObjectPackage( new ByteArrayInputStream(resp.getContentAsByteArray())); assertNotNull(bop); Set<Project> projects = bop.getProjects(); assertNotNull(projects); assertEquals(1, projects.size()); Project returnedProject = projects.iterator().next(); assertNotNull(returnedProject); assertEquals(newProject.getPis().size(), returnedProject.getPis().size()); // Test removing an admin from the project. newProject.removePi(user.getId()); resp = new MockHttpServletResponse(); sink = new ByteArrayOutputStream(); businessObjectBuilder.buildProject(newProject, sink); controller.setAuthenticatedUser(admin); controller.handleUpdateProjectRequest( newProject.getId(), mimeType, sink.toByteArray(), mockReq, resp); assertNotNull(resp); bop = businessObjectBuilder.buildBusinessObjectPackage( new ByteArrayInputStream(resp.getContentAsByteArray())); assertNotNull(bop); projects = bop.getProjects(); assertNotNull(projects); assertEquals(1, projects.size()); returnedProject = projects.iterator().next(); assertNotNull(returnedProject); assertEquals(newProject.getPis().size(), returnedProject.getPis().size()); }