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 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
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
39 import spock.lang.Shared
40 import spock.lang.Specification
42 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
43 @ActiveProfiles("test")
44 class SampleCpsNcmpClientSpec extends Specification {
46 static final String CM_HANDLE = "anything"
48 static final String DATA_STORE_NAME = DatastoreType.PASSTHROUGH_OPERATIONAL.getDatastoreName()
53 final def testRestTemplate = new TestRestTemplate()
55 @Value('${rest.api.ncmp-stub-base-path}')
59 ObjectMapper objectMapper
61 def 'Test the invocation of the stub API' () throws JsonMappingException, JsonProcessingException {
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
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()
76 assert obj3.get("code") == "02"
79 def String getBaseUrl() {
80 return "http://localhost:" + port + stubBasePath