098423540fccd30027fe4039a5f88779180ca3e2
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / resource-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / resource / api / TemplateControllerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.resource.api
18
19 import kotlinx.coroutines.runBlocking
20 import org.junit.Test
21 import org.junit.runner.RunWith
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
23 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
25 import org.slf4j.LoggerFactory
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
28 import org.springframework.context.annotation.ComponentScan
29 import org.springframework.http.MediaType
30 import org.springframework.test.context.ContextConfiguration
31 import org.springframework.test.context.TestPropertySource
32 import org.springframework.test.context.junit4.SpringRunner
33 import org.springframework.test.web.reactive.server.WebTestClient
34 import org.springframework.web.reactive.function.BodyInserters
35 import kotlin.test.AfterTest
36
37 @RunWith(SpringRunner::class)
38 @WebFluxTest
39 @ContextConfiguration(
40     classes = [TestDatabaseConfiguration::class, BluePrintCoreConfiguration::class,
41         BluePrintCatalogService::class, ErrorCatalogTestConfiguration::class]
42 )
43 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
44 @TestPropertySource(locations = ["classpath:application-test.properties"])
45 class TemplateControllerTest {
46
47     private val log = LoggerFactory.getLogger(TemplateControllerTest::class.toString())
48
49     @Autowired
50     lateinit var webTestClient: WebTestClient
51
52     var resolutionKey = "7cafa9f3-bbc8-49ec-8f25-fcaa6ac3ff08"
53     val blueprintName = "baseconfiguration"
54     val blueprintVersion = "1.0.0"
55     val templatePrefix = "activate"
56     val payloadDummyTemplateData = "PAYLOAD DATA"
57
58     var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
59         "&artifactName=$templatePrefix&resolutionKey=$resolutionKey"
60
61     @AfterTest
62     fun cleanDir() {
63         deleteDir("target", "blueprints")
64     }
65
66     @Test
67     fun `ping return Success`() {
68         runBlocking {
69             webTestClient.get().uri("/api/v1/template/health-check")
70                 .exchange()
71                 .expectStatus().isOk
72                 .expectBody()
73                 .equals("Success")
74         }
75     }
76
77     @Test
78     fun `store same value and tries to retrieve - duplicate entry execption`() {
79         runBlocking {
80
81             resolutionKey = "1"
82
83             post(resolutionKey)
84             post(resolutionKey)
85
86             webTestClient
87                 .get()
88                 .uri("/api/v1/template?$requestArguments")
89                 .exchange()
90                 .expectStatus().is4xxClientError
91                 .expectBody().equals(payloadDummyTemplateData)
92         }
93     }
94
95     @Test
96     fun `get returns requested JSON content-type`() {
97         runBlocking {
98             resolutionKey = "2"
99             post(resolutionKey)
100             get("json", resolutionKey)
101         }
102     }
103
104     @Test
105     fun `get returns requested XML content-type`() {
106         runBlocking {
107             resolutionKey = "3"
108             post(resolutionKey)
109             get("xml", resolutionKey)
110         }
111     }
112
113     @Test
114     fun `get returns 400 error if missing arg`() {
115         runBlocking {
116             val arguments = "bpBADName=$blueprintName" +
117                 "&bpBADVersion=$blueprintVersion" +
118                 "&artifactName=$templatePrefix" +
119                 "&resolutionKey=$resolutionKey"
120
121             webTestClient.get().uri("/api/v1/template?$arguments")
122                 .exchange()
123                 .expectStatus().isNotFound
124         }
125     }
126
127     @Test
128     fun `get returns 404 if entry not found`() {
129         runBlocking {
130
131             webTestClient
132                 .get()
133                 .uri(
134                     "/api/v1/template?bpName=$blueprintName&bpVersion=$blueprintVersion" +
135                         "&artifactName=$templatePrefix&resolutionKey=notFound"
136                 )
137                 .exchange()
138                 .expectStatus().isNotFound
139         }
140     }
141
142     private fun post(resKey: String) {
143         webTestClient
144             .post()
145             .uri("/api/v1/template/$blueprintName/$blueprintVersion/$templatePrefix/$resKey")
146             .body(BodyInserters.fromObject(payloadDummyTemplateData))
147             .exchange()
148             .expectStatus().is2xxSuccessful
149             .expectBody()
150             .consumeWith {
151                 log.info("Stored result under UUID ${it.responseBody}")
152             }
153     }
154
155     private fun get(expectedType: String, resKey: String) {
156         var requestArguments = "bpName=$blueprintName&bpVersion=$blueprintVersion" +
157             "&artifactName=$templatePrefix&resolutionKey=$resKey"
158
159         if (expectedType.isNotEmpty()) {
160             requestArguments = "$requestArguments&format=$expectedType"
161             webTestClient
162                 .get()
163                 .uri("/api/v1/template?$requestArguments")
164                 .exchange()
165                 .expectStatus().is2xxSuccessful
166                 .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
167                 .expectBody().equals(payloadDummyTemplateData)
168         } else {
169             webTestClient
170                 .get()
171                 .uri("/api/v1/template?$requestArguments")
172                 .exchange()
173                 .expectStatus().is2xxSuccessful
174                 .expectHeader().contentType(MediaType.TEXT_PLAIN)
175                 .expectBody().equals(payloadDummyTemplateData)
176         }
177     }
178 }