diff options
Diffstat (limited to 'ms')
7 files changed, 91 insertions, 22 deletions
diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceController.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceController.kt index 7cbc89583..eff977348 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceController.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceController.kt @@ -22,9 +22,12 @@ import kotlinx.coroutines.runBlocking import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput +import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.determineHttpStatusCode import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException import org.springframework.beans.factory.annotation.Autowired +import org.springframework.http.HttpStatus import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity import org.springframework.http.codec.multipart.FilePart import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.* @@ -63,10 +66,11 @@ open class ExecutionServiceController { notes = "Takes the blueprint information and process as per the payload") @ResponseBody @PreAuthorize("hasRole('USER')") - fun process(@RequestBody executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput = runBlocking { + fun process(@RequestBody executionServiceInput: ExecutionServiceInput): ResponseEntity<ExecutionServiceOutput> = runBlocking { if (executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC) { throw IllegalStateException("Can't process async request through the REST endpoint. Use gRPC for async processing.") } - executionServiceHandler.doProcess(executionServiceInput) + val processResult = executionServiceHandler.doProcess(executionServiceInput) + ResponseEntity(processResult, determineHttpStatusCode(processResult.status.code)) } } diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt index 32be3e0a0..2cf1c1dd0 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt @@ -16,6 +16,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException +import org.springframework.http.HttpStatus import org.springframework.http.codec.multipart.FilePart import org.springframework.util.StringUtils import java.io.File @@ -26,6 +27,7 @@ import java.time.ZoneId import java.time.format.DateTimeFormatter import java.util.* +const val INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE = 500 fun currentTimestamp(): String { val now = LocalDateTime.now(ZoneId.systemDefault()) @@ -56,4 +58,15 @@ fun saveCBAFile(filePart: FilePart, targetDirectory: Path): Path { filePart.transferTo(file) return targetLocation +} + +fun determineHttpStatusCode(statusCode: Int): HttpStatus { + + try { + return HttpStatus.valueOf(statusCode) + } catch (exception: Exception) { + //if statusCode cannot be converted to a proper HttpStatus, the resource still needs to assign a HTTP status + // code to the response. In this case, a 500 Internal Server Error will be returned as default. + return HttpStatus.valueOf(INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE) + } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt index d14761cc9..9cbd898dc 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt @@ -38,6 +38,7 @@ import org.springframework.test.context.junit4.SpringRunner import org.springframework.test.web.reactive.server.WebTestClient import org.springframework.test.web.reactive.server.returnResult import org.springframework.web.reactive.function.BodyInserters +import java.io.File import java.nio.file.Files import java.nio.file.Paths import java.util.* @@ -71,11 +72,8 @@ class ExecutionServiceHandlerTest { @Test fun `test rest upload blueprint`() { runBlocking { - val file = Paths.get("./src/test/resources/test-cba.zip").toFile() - assertTrue(file.exists(), "couldn't get file ${file.absolutePath}") - val body = MultipartBodyBuilder().apply { - part("file", object : ByteArrayResource(Files.readAllBytes(Paths.get("./src/test/resources/test-cba.zip"))) { + part("file", object : ByteArrayResource(Files.readAllBytes(loadTestCbaFile().toPath())) { override fun getFilename(): String { return "test-cba.zip" } @@ -98,9 +96,7 @@ class ExecutionServiceHandlerTest { @Test fun `test rest process`() { runBlocking { - val file = Paths.get("./src/test/resources/test-cba.zip").toFile() - assertTrue(file.exists(), "couldnt get file ${file.absolutePath}") - blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), file) + blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile()) val executionServiceInput = JacksonUtils .readValueFromClassPathFile("execution-input/default-input.json", @@ -114,4 +110,28 @@ class ExecutionServiceHandlerTest { .expectStatus().isOk } } + + @Test + fun `rest resource process should return status code 500 in case of server-side exception`() { + runBlocking { + blueprintCatalog.saveToDatabase(UUID.randomUUID().toString(), loadTestCbaFile()) + + val executionServiceInput = JacksonUtils + .readValueFromClassPathFile("execution-input/faulty-input.json", + ExecutionServiceInput::class.java)!! + + webTestClient + .post() + .uri("/api/v1/execution-service/process") + .body(BodyInserters.fromObject(executionServiceInput)) + .exchange() + .expectStatus().is5xxServerError + } + } + + private fun loadTestCbaFile(): File { + val testCbaFile = Paths.get("./src/test/resources/test-cba.zip").toFile() + assertTrue(testCbaFile.exists(), "couldn't get file ${testCbaFile.absolutePath}") + return testCbaFile + } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt new file mode 100644 index 000000000..07d8ca4e1 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/utils/Utils.kt @@ -0,0 +1,27 @@ +package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.http.HttpStatus +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +class UtilsTest { + + @Test + fun `valid Http status codes should be produced for valid parameters`() { + val httpStatusCode200 = determineHttpStatusCode(200) + assertEquals(HttpStatus.OK, httpStatusCode200) + + val httpStatusCode500 = determineHttpStatusCode(500) + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, httpStatusCode500) + } + + @Test + fun `Http status code 500 should be produced for any invalid parameter`() { + val nonExistentHttpStatusCode = determineHttpStatusCode(999999) + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, nonExistentHttpStatusCode) + } + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/execution-input/faulty-input.json b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/execution-input/faulty-input.json new file mode 100644 index 000000000..999039e11 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/execution-input/faulty-input.json @@ -0,0 +1,15 @@ +{ + "commonHeader": { + "originatorId": "System", + "requestId": "1234", + "subRequestId": "1234-12234" + }, + "actionIdentifiers": { + "blueprintName": "baseconfiguration", + "blueprintVersion": "1.0.0", + "actionName": "activate", + "mode": "sync" + }, + "payload": { + } +}
\ No newline at end of file diff --git a/ms/sdclistener/application/src/main/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImpl.java b/ms/sdclistener/application/src/main/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImpl.java index c78505377..148d0c028 100644 --- a/ms/sdclistener/application/src/main/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImpl.java +++ b/ms/sdclistener/application/src/main/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImpl.java @@ -31,7 +31,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Enumeration; import java.util.List; -import java.util.Optional; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -138,10 +137,7 @@ public class ListenerServiceImpl implements ListenerService { public void saveBluePrintToCdsDatabase(Path cbaArchivePath, ManagedChannel channel) { List<File> zipFiles = FileUtil.getFilesFromDisk(cbaArchivePath); if (!zipFiles.isEmpty()) { - zipFiles.forEach(file -> FileUtil.deleteFile(file, cbaArchivePath.toString())); prepareRequestForCdsBackend(zipFiles, channel, cbaArchivePath.toString()); - } else { - LOGGER.error("Could not able to read CBA archives from this location {}", cbaArchivePath); } } @@ -187,7 +183,7 @@ public class ListenerServiceImpl implements ListenerService { files.forEach(zipFile -> { try { - final BluePrintUploadInput request = generateBluePrintUploadInputBuilder(zipFile); + final BluePrintUploadInput request = generateBluePrintUploadInputBuilder(zipFile, path); // Send request to CDS Backend. final Status responseStatus = bluePrintProcesssorHandler.sendRequest(request, managedChannel); @@ -209,16 +205,14 @@ public class ListenerServiceImpl implements ListenerService { listenerStatus.sendResponseBackToSdc(distributionId, COMPONENT_DONE_ERROR, errorMessage, artifactUrl, SDC_LISTENER_COMPONENT); LOGGER.error(errorMessage); - } finally { - FileUtil.deleteFile(zipFile, path); } }); } - private BluePrintUploadInput generateBluePrintUploadInputBuilder(File file) throws IOException { + private BluePrintUploadInput generateBluePrintUploadInputBuilder(File file, String path) throws IOException { byte[] bytes = FileUtils.readFileToByteArray(file); FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build(); - + FileUtil.deleteFile(file, path); return BluePrintUploadInput.newBuilder() .setFileChunk(fileChunk) .build(); diff --git a/ms/sdclistener/application/src/test/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImplTest.java b/ms/sdclistener/application/src/test/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImplTest.java index ecb753892..aa8f8e4e8 100644 --- a/ms/sdclistener/application/src/test/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImplTest.java +++ b/ms/sdclistener/application/src/test/java/org/onap/ccsdk/cds/sdclistener/service/ListenerServiceImplTest.java @@ -17,7 +17,6 @@ package org.onap.ccsdk.cds.sdclistener.service; import static junit.framework.TestCase.assertTrue; import static org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType.SDC_LISTENER_COMPONENT; -import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_ERROR; import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_OK; import java.io.File; import java.io.IOException; @@ -39,7 +38,6 @@ import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor; import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto; import org.onap.ccsdk.cds.sdclistener.handler.BluePrintProcesssorHandler; import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus; -import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.impl.mock.DistributionClientResultStubImpl; import org.springframework.beans.factory.annotation.Autowired; @@ -102,8 +100,6 @@ public class ListenerServiceImplTest { @Test public void extractBluePrintFailure() { // Arrange - final String errorMessage = String - .format("The CBA Archive doesn't exist as per this given regex %s", CBA_ZIP_PATH); Mockito.when(listenerDto.getDistributionId()).thenReturn(DISTRIBUTION_ID); Mockito.when(listenerDto.getArtifactUrl()).thenReturn(URL); Mockito.doCallRealMethod().when(status) |