aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/configs-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/configs/api/ResourceConfigSnapshotControllerTest.kt
blob: 634c3368b0b5c615298d90c3094f1a90990cc5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright © 2019 Bell Canada.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.configs.api

import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith
import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.http.MediaType
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.web.reactive.function.BodyInserters

@RunWith(SpringRunner::class)
@WebFluxTest
@ContextConfiguration(
    classes = [BluePrintCoreConfiguration::class, BluePrintCatalogService::class, ErrorCatalogTestConfiguration::class]
)
@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
@TestPropertySource(locations = ["classpath:application-test.properties"])
class ResourceConfigSnapshotControllerTest {

    private val log = LoggerFactory.getLogger(ResourceConfigSnapshotControllerTest::class.toString())

    @Autowired
    lateinit var webTestClient: WebTestClient

    val resourceId = "fcaa6ac3ff08"
    val resourceType = "PNF"
    val snapshotData = "PAYLOAD DATA"

    var requestArguments = "resourceId=$resourceId&resourceType=$resourceType"

    @Test
    fun `ping return Success`() {
        runBlocking {
            webTestClient.get().uri("/api/v1/configs/health-check")
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .equals("Success")
        }
    }

    @Test
    fun `update configuration is allowed and updates timestamp`() {
        runBlocking {

            webTestClient
                .post()
                .uri("/api/v1/configs/$resourceType/$resourceId/running")
                .body(BodyInserters.fromObject(snapshotData))
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectBody()
                .jsonPath("$.createdDate")
                .value<String> { println(it) }

            webTestClient
                .post()
                .uri("/api/v1/configs/$resourceType/$resourceId/running")
                .body(BodyInserters.fromObject(snapshotData))
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectBody()
                .jsonPath("$.createdDate")
                .value<String> { println(it) }
        }
    }

    @Test
    fun `get returns requested JSON content-type`() {
        runBlocking {
            post(resourceType, "22", "RUNNING")
            get("json", resourceType, "22", "RUNNING")
        }
    }

    @Test
    fun `get returns requested XML content-type`() {
        runBlocking {
            post(resourceType, "3", "CANDIDATE")
            get("xml", resourceType, "3", "CANDIDATE")
        }
    }

    @Test
    fun `get returns 400 error if missing arg`() {
        runBlocking {
            val arguments = "artifactName=WRONGARG1&resolutionKey=WRONGARG1"

            webTestClient.get().uri("/api/v1/configs?$arguments")
                .exchange()
                .expectStatus().is4xxClientError
        }
    }

    @Test
    fun `get returns 400 error if wrong Status arg`() {
        runBlocking {
            val arguments = "resourceId=MISSING&resourceType=PNF&status=TOTALLY_WRONG"

            webTestClient.get().uri("/api/v1/configs?$arguments")
                .exchange()
                .expectStatus().isBadRequest
        }
    }

    @Test
    fun `get returns 200 if entry not found`() {
        runBlocking {

            webTestClient
                .get()
                .uri("/api/v1/configs?resourceId=MISSING&resourceType=PNF")
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectBody()
        }
    }

    @Test
    fun `getAllByID returns 200 if entries found`() {
        runBlocking {
            post(resourceType, "3", "RUNNING")
            post(resourceType, "2", "RUNNING")
            post(resourceType, resourceId, "RUNNING")

            webTestClient
                .get()
                .uri("/api/v1/configs/allByID?resourceId=$resourceId")
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectBody()
                .jsonPath("$.length()")
                .isEqualTo(1)
        }
    }

    @Test
    fun `getAllByID with CANDIDATE status returns 200 if entries found`() {
        runBlocking {
            post(resourceType, "3", "RUNNING")
            post(resourceType, "2", "RUNNING")
            post(resourceType, resourceId, "CANDIDATE")

            webTestClient
                .get()
                .uri("/api/v1/configs/allByID?resourceId=$resourceId&status=CANDIDATE")
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectBody()
                .jsonPath("$.length()")
                .isEqualTo(1)
        }
    }

    @Test
    fun `getAllByID returns 400 error if missing parameter`() {
        runBlocking {

            webTestClient
                .get()
                .uri("/api/v1/configs/allByID")
                .exchange()
                .expectStatus().is4xxClientError
                .expectBody()
        }
    }

    @Test
    fun `getAllByID returns 400 error if wrong status parameter`() {
        runBlocking {

            webTestClient
                .get()
                .uri("/api/v1/configs/allByID?resourceId=$resourceId&status=NOTGOOD")
                .exchange()
                .expectStatus().is4xxClientError
                .expectBody()
        }
    }

    @Test
    fun `getAllByType returns 200 if entries found`() {
        runBlocking {
            post(resourceType, "3", "RUNNING")
            post(resourceType + "DIFF", "2", "RUNNING")
            post(resourceType, "1", "RUNNING")

            webTestClient
                .get()
                .uri("/api/v1/configs/allByType?resourceType=$resourceType")
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectBody()
                .jsonPath("$.length()")
                .isEqualTo(3)
        }
    }

    @Test
    fun `getAllByType returns 400 error if missing parameter`() {
        runBlocking {

            webTestClient
                .get()
                .uri("/api/v1/configs/allByType")
                .exchange()
                .expectStatus().is4xxClientError
                .expectBody()
        }
    }

    @Test
    fun `getAllByType returns 400 error if wrong status parameter`() {
        runBlocking {

            webTestClient
                .get()
                .uri("/api/v1/configs/allByType?resourceType=$resourceType&status=NOTGOOD")
                .exchange()
                .expectStatus().is4xxClientError
                .expectBody()
        }
    }

    private fun post(resourceType: String, resourceId: String, status: String) {
        webTestClient
            .post()
            .uri("/api/v1/configs/$resourceType/$resourceId/$status")
            .body(BodyInserters.fromObject(snapshotData))
            .exchange()
            .expectStatus().is2xxSuccessful
            .expectBody()
    }

    private fun get(expectedType: String, resourceType: String, resourceId: String, status: String) {
        var requestArguments = "resourceId=$resourceId&resourceType=$resourceType&status=$status"

        if (expectedType.isNotEmpty()) {
            requestArguments = "$requestArguments&format=$expectedType"
            webTestClient
                .get()
                .uri("/api/v1/configs?$requestArguments")
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectHeader().contentType(MediaType.valueOf("application/$expectedType"))
                .expectBody().equals(snapshotData)
        } else {
            webTestClient
                .get()
                .uri("/api/v1/configs?$requestArguments")
                .exchange()
                .expectStatus().is2xxSuccessful
                .expectHeader().contentType(MediaType.TEXT_PLAIN)
                .expectBody().equals(snapshotData)
        }
    }
}