Merge "Clean up Dependencies"
[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 org.onap.cps.ncmp.api.impl.operations.DatastoreType
23 import org.springframework.beans.factory.annotation.Autowired
24 import org.springframework.beans.factory.annotation.Value
25 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
26 import org.springframework.boot.test.context.SpringBootContextLoader
27 import org.springframework.boot.test.context.SpringBootTest
28 import org.springframework.boot.test.web.client.TestRestTemplate
29 import org.springframework.boot.test.web.server.LocalServerPort
30 import org.springframework.http.HttpStatus
31 import org.springframework.test.context.ActiveProfiles
32 import org.springframework.test.context.ContextConfiguration
33
34 import com.fasterxml.jackson.core.JsonProcessingException
35 import com.fasterxml.jackson.core.type.TypeReference
36 import com.fasterxml.jackson.databind.JsonMappingException
37 import com.fasterxml.jackson.databind.ObjectMapper
38
39 import spock.lang.Shared
40 import spock.lang.Specification
41
42 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
43 @ActiveProfiles("test")
44 class SampleCpsNcmpClientSpec extends Specification {
45
46     static final String CM_HANDLE = "anything"
47
48     static final String DATA_STORE_NAME = DatastoreType.PASSTHROUGH_OPERATIONAL.getDatastoreName()
49
50     @LocalServerPort
51     def port
52
53     final def testRestTemplate = new TestRestTemplate()
54
55     @Value('${rest.api.ncmp-stub-base-path}')
56     def stubBasePath
57
58     @Autowired
59     ObjectMapper objectMapper
60
61     def 'Test the invocation of the stub API' () throws JsonMappingException, JsonProcessingException {
62
63         when: 'Get resource data for cm handle URL is invoked'
64             def url = "${getBaseUrl()}/v1/ch/${CM_HANDLE}/data/ds/${DATA_STORE_NAME}?resourceIdentifier=parent&options=(a=1,b=2)"
65             def response = testRestTemplate.getForEntity(url, String.class)
66         then: 'Response is OK'
67             response.getStatusCode() == HttpStatus.OK
68
69         and: 'Response body contains customized stub response that contains correct bookstore category code'
70             def typeRef = new TypeReference<HashMap<String, Object>>() {}
71             def map = objectMapper.readValue(response.getBody(), typeRef)
72             def obj1 = (Map<String, Object>) map.get("stores:bookstore")
73             def obj2 = (List<Object>) obj1.get("categories")
74             def obj3 = (Map<String, Object>) obj2.iterator().next()
75
76             assert obj3.get("code") == "02"
77     }
78
79     def String getBaseUrl() {
80         return "http://localhost:" + port + stubBasePath
81     }
82 }