diff options
author | Frank Kimmlingen <frank.kimmlingen@telekom.de> | 2022-08-29 13:15:30 +0200 |
---|---|---|
committer | KAPIL SINGAL <ks220y@att.com> | 2022-09-08 17:43:03 +0000 |
commit | 964df01c19c529deca28d118b67e3465fc3998aa (patch) | |
tree | 35bf04d6aec0759d6d43b9e95420f3035b57c30a | |
parent | 6c23c2bc522f6ddc9f61785489a8eab4969bb819 (diff) |
Http 204 response results with exception in rest resolution
Issue-ID: CCSDK-3746
Signed-off-by: Frank Kimmlingen <frank.kimmlingen@telekom.de>
Change-Id: I74873bc54a7007bac1eed9223460146fb9724af2
2 files changed, 92 insertions, 3 deletions
diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt index 945d29850..d37612fe6 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BlueprintWebClientService.kt @@ -22,6 +22,9 @@ import com.fasterxml.jackson.databind.JsonNode import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.apache.commons.io.IOUtils +import org.apache.http.HttpEntity +import org.apache.http.HttpResponse +import org.apache.http.HttpStatus import org.apache.http.client.ClientProtocolException import org.apache.http.client.methods.HttpDelete import org.apache.http.client.methods.HttpGet @@ -165,11 +168,25 @@ interface BlueprintWebClientService { WebClientResponse<T> { val httpResponse = httpClient().execute(httpUriRequest) val statusCode = httpResponse.statusLine.statusCode - httpResponse.entity.content.use { - val body = getResponse(it, responseType) + val entity: HttpEntity? = httpResponse.entity + if (canResponseHaveBody(httpResponse)) { + entity!!.content.use { + val body = getResponse(it, responseType) + return WebClientResponse(statusCode, body) + } + } else { + val constructor = responseType.getConstructor() + val body = constructor.newInstance() return WebClientResponse(statusCode, body) } } + fun canResponseHaveBody(response: HttpResponse): Boolean { + val status = response.statusLine.statusCode + return response.entity !== null && + status != HttpStatus.SC_NO_CONTENT && + status != HttpStatus.SC_NOT_MODIFIED && + status != HttpStatus.SC_RESET_CONTENT + } suspend fun getNB(path: String): WebClientResponse<String> { return getNB(path, null, String::class.java) diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt index 030996685..e865a0047 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt @@ -39,6 +39,8 @@ import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory import org.springframework.boot.web.server.WebServer import org.springframework.context.annotation.Bean import org.springframework.http.HttpMethod +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity import org.springframework.http.server.reactive.HttpHandler import org.springframework.security.config.web.server.ServerHttpSecurity import org.springframework.security.core.userdetails.MapReactiveUserDetailsService @@ -184,7 +186,7 @@ class RestClientServiceTest { assertEquals(res, "Basic request arrived successfully") } - // @Test + @Test fun testSampleAaiReq() { val restClientService = bluePrintRestLibPropertyService .blueprintWebClientService("test") @@ -282,6 +284,51 @@ class RestClientServiceTest { assertEquals(res5, "The patch request is success") assertEquals(res6, "The message is successfully deleted") } + @Test + fun testDeleteWith204StatusAndResponseAsString() { + val restClientService = bluePrintRestLibPropertyService + .blueprintWebClientService("sample") + val headers = mutableMapOf<String, String>() + headers["X-Transaction-Id"] = "1234" + val response = restClientService.exchangeResource( + HttpMethod.DELETE.name, + "/sample/name", "" + ) + assertEquals(response.status, 204) + assertEquals(response.body, "") + } + + @Test + fun testDeleteWith204StatusAndResponseAsCustomerWithDefaultConstructor() { + val restClientService = bluePrintRestLibPropertyService + .blueprintWebClientService("sample") + val headers = mutableMapOf<String, String>() + headers["X-Transaction-Id"] = "1234" + runBlocking { + val response = restClientService.exchangeNB( + HttpMethod.DELETE.name, + "/sample/customersWithDefaultConstructor", "", headers, CustomerWithDefaultConstructor::class.java + ) + assertEquals(response.status, 204) + assertEquals(response.body, CustomerWithDefaultConstructor()) + } + } + + // @Test + fun testDeleteWith204StatusAndResponseAsCustomerWithoutDefaultConstructor() { + val restClientService = bluePrintRestLibPropertyService + .blueprintWebClientService("sample") + val headers = mutableMapOf<String, String>() + headers["X-Transaction-Id"] = "1234" + runBlocking { + val response = restClientService.exchangeNB( + HttpMethod.DELETE.name, + "/sample/customersWithoutDefaultConstructor", "", headers, CustomerWithoutDefaultConstructor::class.java + ) + assertEquals(response.status, 204) + assertEquals(response.body, CustomerWithoutDefaultConstructor("")) + } + } } /** @@ -293,6 +340,10 @@ open class SampleController { @GetMapping("/name") fun getName(): String = "Sample Controller" + @DeleteMapping("/name") + fun deleteNameWith204StatusAndEmptyBody(): ResponseEntity<Unit> { + return ResponseEntity(HttpStatus.NO_CONTENT) + } @GetMapping("/query") fun getQuery(@RequestParam("id") id: String): String = @@ -377,6 +428,17 @@ open class SampleController { } return "The message is successfully deleted" } + @DeleteMapping("/customersWithDefaultConstructor") + fun deleteCustomersWith204StatusAndResponseAsCustomerWithDefaultConstructor(): + ResponseEntity<CustomerWithDefaultConstructor> { + return ResponseEntity(CustomerWithDefaultConstructor(), HttpStatus.NO_CONTENT) + } + + @DeleteMapping("/customersWithoutDefaultConstructor") + fun deleteCustomersWith204StatusAndResponseAsCustomerWithoutDefaultConstructor(): + ResponseEntity<CustomerWithoutDefaultConstructor> { + return ResponseEntity(CustomerWithoutDefaultConstructor(""), HttpStatus.NO_CONTENT) + } } /** @@ -414,3 +476,13 @@ data class Customer( val type: String, val resource: String ) + +data class CustomerWithoutDefaultConstructor( + val name: String +) + +data class CustomerWithDefaultConstructor( + val name: String +) { + constructor() : this("") +} |