Merge "Make nginx load balance using least-connections"
[cps.git] / cps-ncmp-rest-stub / cps-ncmp-rest-stub-service / src / test / groovy / org / onap / cps / ncmp / rest / stub / SampleCpsNcmpClientSpec.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.cps.ncmp.rest.stub
21
22 import com.fasterxml.jackson.core.JsonProcessingException
23 import com.fasterxml.jackson.core.type.TypeReference
24 import com.fasterxml.jackson.databind.JsonMappingException
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import org.onap.cps.ncmp.api.data.models.DatastoreType
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.beans.factory.annotation.Value
29 import org.springframework.boot.test.context.SpringBootTest
30 import org.springframework.boot.test.web.client.TestRestTemplate
31 import org.springframework.boot.test.web.server.LocalServerPort
32 import org.springframework.http.HttpStatus
33 import org.springframework.test.context.ActiveProfiles
34 import spock.lang.Specification
35
36 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
37 @ActiveProfiles("test")
38 class SampleCpsNcmpClientSpec extends Specification {
39
40     static final String CM_HANDLE = "anything"
41
42     static final String DATA_STORE_NAME = DatastoreType.PASSTHROUGH_OPERATIONAL.getDatastoreName()
43
44     @LocalServerPort
45     def port
46
47     final def testRestTemplate = new TestRestTemplate()
48
49     @Value('${rest.api.ncmp-stub-base-path}')
50     def stubBasePath
51
52     @Autowired
53     ObjectMapper objectMapper
54
55     def 'Test the invocation of the stub API' () throws JsonMappingException, JsonProcessingException {
56
57         when: 'Get resource data for cm handle URL is invoked'
58             def url = "${getBaseUrl()}/v1/ch/${CM_HANDLE}/data/ds/${DATA_STORE_NAME}?resourceIdentifier=parent&options=(a=1,b=2)"
59             def response = testRestTemplate.getForEntity(url, String.class)
60         then: 'Response is OK'
61             response.getStatusCode() == HttpStatus.OK
62
63         and: 'Response body contains customized stub response that contains correct bookstore category code'
64             def typeRef = new TypeReference<HashMap<String, Object>>() {}
65             def map = objectMapper.readValue(response.getBody(), typeRef)
66             def obj1 = (Map<String, Object>) map.get("stores:bookstore")
67             def obj2 = (List<Object>) obj1.get("categories")
68             def obj3 = (Map<String, Object>) obj2.iterator().next()
69
70             assert obj3.get("code") == "02"
71     }
72
73     def String getBaseUrl() {
74         return "http://localhost:" + port + stubBasePath
75     }
76 }