MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi { @Override public ResponseEntity<List<MgmtSoftwareModuleMetadata>> createMetadata( @PathVariable("softwareModuleId") final Long softwareModuleId, @RequestBody final List<MgmtSoftwareModuleMetadata> metadataRest) { final List<SoftwareModuleMetadata> created = softwareModuleManagement.createMetaData( MgmtSoftwareModuleMapper.fromRequestSwMetadata(entityFactory, softwareModuleId, metadataRest)); return ResponseEntity.status(HttpStatus.CREATED).body(MgmtSoftwareModuleMapper.toResponseSwMetadata(created)); } MgmtSoftwareModuleResource(final ArtifactManagement artifactManagement,
            final SoftwareModuleManagement softwareModuleManagement, final EntityFactory entityFactory); @Override ResponseEntity<MgmtArtifact> uploadArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId,
            @RequestPart("file") final MultipartFile file,
            @RequestParam(value = "filename", required = false) final String optionalFileName,
            @RequestParam(value = "md5sum", required = false) final String md5Sum,
            @RequestParam(value = "sha1sum", required = false) final String sha1Sum,
            @RequestParam(value = "sha256sum", required = false) final String sha256Sum); @Override ResponseEntity<List<MgmtArtifact>> getArtifacts(
            @PathVariable("softwareModuleId") final Long softwareModuleId); @Override @ResponseBody // Exception squid:S3655 - Optional access is checked in // findSoftwareModuleWithExceptionIfNotFound // subroutine @SuppressWarnings("squid:S3655") ResponseEntity<MgmtArtifact> getArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId,
            @PathVariable("artifactId") final Long artifactId); @Override @ResponseBody ResponseEntity<Void> deleteArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId,
            @PathVariable("artifactId") final Long artifactId); @Override ResponseEntity<PagedList<MgmtSoftwareModule>> getSoftwareModules(
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_PAGING_OFFSET, defaultValue = MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_OFFSET) final int pagingOffsetParam,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_PAGING_LIMIT, defaultValue = MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_LIMIT) final int pagingLimitParam,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_SORTING, required = false) final String sortParam,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_SEARCH, required = false) final String rsqlParam); @Override ResponseEntity<MgmtSoftwareModule> getSoftwareModule(
            @PathVariable("softwareModuleId") final Long softwareModuleId); @Override ResponseEntity<List<MgmtSoftwareModule>> createSoftwareModules(
            @RequestBody final List<MgmtSoftwareModuleRequestBodyPost> softwareModules); @Override ResponseEntity<MgmtSoftwareModule> updateSoftwareModule(
            @PathVariable("softwareModuleId") final Long softwareModuleId,
            @RequestBody final MgmtSoftwareModuleRequestBodyPut restSoftwareModule); @Override ResponseEntity<Void> deleteSoftwareModule(@PathVariable("softwareModuleId") final Long softwareModuleId); @Override ResponseEntity<PagedList<MgmtSoftwareModuleMetadata>> getMetadata(
            @PathVariable("softwareModuleId") final Long softwareModuleId,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_PAGING_OFFSET, defaultValue = MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_OFFSET) final int pagingOffsetParam,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_PAGING_LIMIT, defaultValue = MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_LIMIT) final int pagingLimitParam,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_SORTING, required = false) final String sortParam,
            @RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_SEARCH, required = false) final String rsqlParam); @Override ResponseEntity<MgmtSoftwareModuleMetadata> getMetadataValue(
            @PathVariable("softwareModuleId") final Long softwareModuleId,
            @PathVariable("metadataKey") final String metadataKey); @Override ResponseEntity<MgmtSoftwareModuleMetadata> updateMetadata(
            @PathVariable("softwareModuleId") final Long softwareModuleId,
            @PathVariable("metadataKey") final String metadataKey,
            @RequestBody final MgmtSoftwareModuleMetadataBodyPut metadata); @Override ResponseEntity<Void> deleteMetadata(@PathVariable("softwareModuleId") final Long softwareModuleId,
            @PathVariable("metadataKey") final String metadataKey); @Override ResponseEntity<List<MgmtSoftwareModuleMetadata>> createMetadata(
            @PathVariable("softwareModuleId") final Long softwareModuleId,
            @RequestBody final List<MgmtSoftwareModuleMetadata> metadataRest);  }
@Test @Description("Verfies the successful creation of metadata and the enforcement of the meta data quota.") public void createMetadata() throws Exception { final String knownKey1 = "knownKey1"; final String knownValue1 = "knownValue1"; final String knownKey2 = "knownKey2"; final String knownValue2 = "knownValue1"; final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); final JSONArray metaData1 = new JSONArray(); metaData1.put(new JSONObject().put("key", knownKey1).put("value", knownValue1)); metaData1.put(new JSONObject().put("key", knownKey2).put("value", knownValue2).put("targetVisible", true)); mvc.perform(post("/rest/v1/softwaremodules/{swId}/metadata", sm.getId()).accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON).content(metaData1.toString())) .andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("[0]key", equalTo(knownKey1))).andExpect(jsonPath("[0]value", equalTo(knownValue1))) .andExpect(jsonPath("[0]targetVisible", equalTo(false))) .andExpect(jsonPath("[1]key", equalTo(knownKey2))).andExpect(jsonPath("[1]value", equalTo(knownValue2))) .andExpect(jsonPath("[1]targetVisible", equalTo(true))); final SoftwareModuleMetadata metaKey1 = softwareModuleManagement .getMetaDataBySoftwareModuleId(sm.getId(), knownKey1).get(); final SoftwareModuleMetadata metaKey2 = softwareModuleManagement .getMetaDataBySoftwareModuleId(sm.getId(), knownKey2).get(); assertThat(metaKey1.getValue()).as("Metadata key is wrong").isEqualTo(knownValue1); assertThat(metaKey2.getValue()).as("Metadata key is wrong").isEqualTo(knownValue2); final int maxMetaData = quotaManagement.getMaxMetaDataEntriesPerSoftwareModule(); final JSONArray metaData2 = new JSONArray(); for (int i = 0; i < maxMetaData - metaData1.length() + 1; ++i) { metaData2.put(new JSONObject().put("key", knownKey1 + i).put("value", knownValue1 + i)); } mvc.perform(post("/rest/v1/softwaremodules/{swId}/metadata", sm.getId()).accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON).content(metaData2.toString())) .andDo(MockMvcResultPrinter.print()).andExpect(status().isForbidden()); assertThat(softwareModuleManagement .findMetaDataBySoftwareModuleId(PageRequest.of(0, Integer.MAX_VALUE), sm.getId()).getTotalElements()) .isEqualTo(metaData1.length()); }
@Test @Description("Ensures that module metadta deletion request to API on an entity that does not exist results in NOT_FOUND.") public void deleteModuleMetadataThatDoesNotExistLeadsToNotFound() throws Exception { final String knownKey = "knownKey"; final String knownValue = "knownValue"; final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); softwareModuleManagement.createMetaData( entityFactory.softwareModuleMetadata().create(sm.getId()).key(knownKey).value(knownValue)); mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/XXX", sm.getId(), knownKey)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isNotFound()); mvc.perform(delete("/rest/v1/softwaremodules/1234/metadata/{key}", knownKey)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isNotFound()); assertThat(softwareModuleManagement.getMetaDataBySoftwareModuleId(sm.getId(), knownKey)).isPresent(); }
@Test @Description("Verfies the successfull search of a metadata entry based on value.") public void searchSoftwareModuleMetadataRsql() throws Exception { final int totalMetadata = 10; final String knownKeyPrefix = "knownKey"; final String knownValuePrefix = "knownValue"; final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); for (int index = 0; index < totalMetadata; index++) { softwareModuleManagement.createMetaData(entityFactory.softwareModuleMetadata().create(sm.getId()) .key(knownKeyPrefix + index).value(knownValuePrefix + index)); } final String rsqlSearchValue1 = "value==knownValue1"; mvc.perform(get("/rest/v1/softwaremodules/{swId}/metadata?q=" + rsqlSearchValue1, sm.getId())) .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()).andExpect(jsonPath("size", equalTo(1))) .andExpect(jsonPath("total", equalTo(1))).andExpect(jsonPath("content[0].key", equalTo("knownKey1"))) .andExpect(jsonPath("content[0].value", equalTo("knownValue1"))); }