SpaceController { @RequestMapping( value = "", method = RequestMethod.POST, consumes = {"application/json;charset=UTF-8"}, produces = {"application/json;charset=UTF-8"}) public ResponseEntity<Space> createSpace( @RequestBody Space space, @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false) String tenant) { final StopWatch sw = new StopWatch( "Create space [" + (space == null ? null : space.getSpaceName()) + "] " + "for tenant token [" + tenant + "] ") .start(); try { if (space == null) { log.error("No space submitted"); return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST); } else if (!space.hasNameAndDescription()) { log.error("Work spaces require a name and description"); return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST); } else if (space.isReadOnly()) { log.error("Work spaces cannot be created with read-only set to true"); return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST); } Tenant t = null; try { t = this.tenantRepository.getTenant(tenant); } catch (EntityNotFoundException enfe) { log.error("Tenant [" + tenant + "] not found"); return new ResponseEntity<Space>(HttpStatus.NOT_FOUND); } catch (Exception e) { log.error("Error retrieving tenant [" + tenant + "]: " + e); return new ResponseEntity<Space>(HttpStatus.NOT_FOUND); } if (space.isDefault() && this.spaceRepository.findDefault(t.getTenantToken()) != null) { log.error( "A default space for the given tenant already exists, adjust the configuration" + " accordingly"); return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST); } space.setTenant(t); space.setSpaceToken(TokenUtil.generateToken()); this.spaceRepository.customSave(space); sw.stop(); return new ResponseEntity<Space>(space, HttpStatus.CREATED); } catch (Exception enfe) { sw.stop(enfe); return new ResponseEntity<Space>(HttpStatus.INTERNAL_SERVER_ERROR); } } @Autowired  SpaceController(
      TenantRepository tenantRepository,
      SpaceRepository spaceRepository,
      ApplicationRepository appRepository,
      ApplicationExporter appExporter,
      Filter cacheFilter); @RequestMapping( value = "", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) ResponseEntity<Collection<Space>> getAllSpaces(
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping( value = "default", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) ResponseEntity<Space> getDefaultSpace(
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping( value = "search", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) ResponseEntity<List<Space>> searchSpaces(
      @RequestParam(value = "propertyName", required = true) String propertyName,
      @RequestParam(value = "mode", required = false, defaultValue = "EQUALS") ComparisonMode mode,
      @RequestParam(value = "caseSensitivity", required = false, defaultValue = "CASE_SENSITIVE")
          CaseSensitivity caseSensitivity,
      @RequestParam(value = "value", required = true) String[] value,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping(value = "/{token:.+}", method = RequestMethod.OPTIONS) ResponseEntity<Space> isSpaceExisting(
      @PathVariable String token,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping(value = "/{token:.+}", method = RequestMethod.GET) ResponseEntity<Space> getSpace(
      @PathVariable String token,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping( value = "", method = RequestMethod.POST, consumes = {"application/json;charset=UTF-8"}, produces = {"application/json;charset=UTF-8"}) ResponseEntity<Space> createSpace(
      @RequestBody Space space,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping( value = "/{token:.+}", method = RequestMethod.PUT, consumes = {"application/json;charset=UTF-8"}, produces = {"application/json;charset=UTF-8"}) ResponseEntity<Space> modifySpace(
      @PathVariable String token,
      @RequestBody Space new_space,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping(value = "/{token:.+}", method = RequestMethod.POST) ResponseEntity<Space> cleanSpace(
      @PathVariable String token,
      @RequestParam(value = "clean", required = true) Boolean clean,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping(value = "/{token:.+}", method = RequestMethod.DELETE) ResponseEntity<Space> deleteSpace(
      @PathVariable String token,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant); @RequestMapping(value = "/{token:.+}/apps", method = RequestMethod.GET) void getApplications(
      @PathVariable String token,
      @RequestParam(value = "includeSpaceProperties", required = false, defaultValue = "")
          final String[] includeSpaceProperties,
      @RequestParam(value = "includeGoalConfiguration", required = false, defaultValue = "")
          final String[] includeGoalConfiguration,
      @RequestParam(value = "includeGoalSystemInfo", required = false, defaultValue = "")
          final String[] includeGoalSystemInfo,
      @RequestParam(value = "includeBugs", required = false, defaultValue = "false")
          final String includeBugs,
      @RequestParam(value = "includeExemptions", required = false, defaultValue = "false")
          final String includeExemptions,
      @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false) final String tenant,
      HttpServletRequest request,
      HttpServletResponse response);  }
@Test public void testSpaceCreation() throws Exception { assertEquals(0, this.spaceRepository.count()); final Tenant d_tenant = this.createDefaultTenant(); final Space d_space = this.createSpace(d_tenant, TEST_DEFAULT_SPACE, true); assertEquals(1, this.spaceRepository.count()); final String space_name = "foo"; org.eclipse.steady.shared.json.model.Space new_shared_space = new org.eclipse.steady.shared.json.model.Space(); new_shared_space.setSpaceName(TEST_DEFAULT_SPACE); new_shared_space.setDefault( true); new_shared_space.setExportConfiguration(ExportConfiguration.OFF); new_shared_space.setSpaceDescription("bar"); new_shared_space.setOwnerEmails( new HashSet<String>(Arrays.asList(new String[] {"foo@bar.com"}))); MockHttpServletRequestBuilder post_builder = post("/spaces") .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken()) .content(JacksonUtil.asJsonString(new_shared_space)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON); mockMvc.perform(post_builder).andExpect(status().isBadRequest()); assertEquals(1, this.spaceRepository.count()); new_shared_space.setDefault(false); post_builder = post("/spaces") .content(JacksonUtil.asJsonString(new_shared_space)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON); mockMvc.perform(post_builder).andExpect(status().isCreated()); assertEquals(2, this.spaceRepository.count()); new_shared_space.setReadOnly(true); post_builder = post("/spaces") .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken()) .content(JacksonUtil.asJsonString(new_shared_space)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON); mockMvc.perform(post_builder).andExpect(status().isBadRequest()); assertEquals(2, this.spaceRepository.count()); new_shared_space.setReadOnly(false); new_shared_space.setSpaceName(space_name); new_shared_space.setDefault(false); post_builder = post("/spaces") .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken()) .content(JacksonUtil.asJsonString(new_shared_space)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON); mockMvc .perform(post_builder) .andExpect(status().isCreated()) .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$.spaceName", is(space_name))); assertEquals(3, this.spaceRepository.count()); MockHttpServletRequestBuilder post_builder2 = post("/spaces") .content(JacksonUtil.asJsonString(new_shared_space)) .contentType(MediaType.APPLICATION_JSON) .header(Constants.HTTP_TENANT_HEADER, "does-not-exist") .accept(MediaType.APPLICATION_JSON); mockMvc.perform(post_builder2).andExpect(status().isNotFound()); }
@Test public void testSpaceCreationAndDeletion() throws Exception { assertEquals(0, this.spaceRepository.count()); final Tenant d_tenant = this.createDefaultTenant(); final Space d_space = this.createSpace(d_tenant, TEST_DEFAULT_SPACE, true); assertEquals(1, this.spaceRepository.count()); final String space_name = "foo"; org.eclipse.steady.shared.json.model.Space new_shared_space = new org.eclipse.steady.shared.json.model.Space(); new_shared_space.setSpaceName("foo"); new_shared_space.setExportConfiguration(ExportConfiguration.OFF); new_shared_space.setSpaceDescription("bar"); new_shared_space.setOwnerEmails( new HashSet<String>(Arrays.asList(new String[] {"foo@bar.com"}))); MockHttpServletRequestBuilder post_builder = post("/spaces") .content(JacksonUtil.asJsonString(new_shared_space)) .contentType(MediaType.APPLICATION_JSON) .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken()) .accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc .perform(post_builder) .andExpect(status().isCreated()) .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$.default", is(false))) .andExpect(jsonPath("$.spaceName", is(space_name))) .andReturn(); assertEquals(2, this.spaceRepository.count()); org.eclipse.steady.shared.json.model.Space created_shared_space = (org.eclipse.steady.shared.json.model.Space) JacksonUtil.asObject( result.getResponse().getContentAsString(), org.eclipse.steady.shared.json.model.Space.class); MockHttpServletRequestBuilder del_builder = delete("/spaces/" + created_shared_space.getSpaceToken()) .header(Constants.HTTP_TENANT_HEADER, TEST_DEFAULT_TENANT) .accept(MediaType.APPLICATION_JSON); mockMvc.perform(del_builder).andExpect(status().isOk()); assertEquals(1, this.spaceRepository.count()); }
@Test public void testReadOnlySpace() throws Exception { assertEquals(0, this.spaceRepository.count()); final Tenant d_tenant = this.createDefaultTenant(); final Space d_space = this.createSpace(d_tenant, TEST_DEFAULT_SPACE, true); assertEquals(1, this.spaceRepository.count()); d_space.setReadOnly(true); MockHttpServletRequestBuilder post_builder = put("/spaces/" + d_space.getSpaceToken()) .content(JacksonUtil.asJsonString(d_space)) .contentType(MediaType.APPLICATION_JSON) .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken()) .accept(MediaType.APPLICATION_JSON); mockMvc .perform(post_builder) .andExpect(status().isOk()) .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$.default", is(true))) .andExpect(jsonPath("$.spaceName", is(TEST_DEFAULT_SPACE))); d_space.setReadOnly(false); post_builder = put("/spaces/" + d_space.getSpaceToken()) .content(JacksonUtil.asJsonString(d_space)) .contentType(MediaType.APPLICATION_JSON) .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken()) .accept(MediaType.APPLICATION_JSON); mockMvc.perform(post_builder).andExpect(status().isBadRequest()); }