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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.cps.ncmp.rest.stub
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
36 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
37 @ActiveProfiles("test")
38 class SampleCpsNcmpClientSpec extends Specification {
40 static final String CM_HANDLE = "anything"
42 static final String DATA_STORE_NAME = DatastoreType.PASSTHROUGH_OPERATIONAL.getDatastoreName()
47 final def testRestTemplate = new TestRestTemplate()
49 @Value('${rest.api.ncmp-stub-base-path}')
53 ObjectMapper objectMapper
55 def 'Test the invocation of the stub API' () throws JsonMappingException, JsonProcessingException {
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
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()
70 assert obj3.get("code") == "02"
73 def String getBaseUrl() {
74 return "http://localhost:" + port + stubBasePath