Merge "Changing resource identifier to a query param"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / operation / DmiOperationsSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
21 package org.onap.cps.ncmp.api.impl.operation
22
23 import org.onap.cps.ncmp.api.impl.client.DmiRestClient
24 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
25 import org.spockframework.spring.SpringBean
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.boot.test.context.SpringBootTest
28 import org.springframework.http.HttpHeaders
29 import org.springframework.test.context.ContextConfiguration
30 import spock.lang.Specification
31
32 @SpringBootTest
33 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiOperations])
34 class DmiOperationsSpec extends Specification {
35
36     @SpringBean
37     DmiRestClient mockDmiRestClient = Mock()
38
39     @Autowired
40     DmiOperations objectUnderTest = new DmiOperations(mockDmiRestClient)
41
42     def 'call get resource data for pass-through:operational datastore from DMI.'() {
43         given: 'expected url'
44         def expectedUrl = 'testDmiBasePath/dmi/v1/ch/testCmhandle/data/ds' +
45                 '/ncmp-datastore:passthrough-operational?resourceIdentifier=parent/child&fields=testFieldsQuery&depth=10'
46         when: 'get resource data is called to DMI'
47         objectUnderTest.getResourceDataOperationalFromDmi('testDmiBasePath',
48                 'testCmhandle',
49                 'parent/child',
50                 'testFieldsQuery',
51                 10,
52                 'testAcceptJson',
53                 'testJsonbody')
54         then: 'the put operation is executed with the correct URL'
55         1 * mockDmiRestClient.putOperationWithJsonData(expectedUrl, 'testJsonbody', _ as HttpHeaders)
56     }
57     def 'call get resource data for pass-through:running datastore from DMI.'() {
58         given: 'expected url'
59         def expectedUrl = 'testDmiBasePath/dmi/v1/ch/testCmhandle/data/ds' +
60                 '/ncmp-datastore:passthrough-running?resourceIdentifier=parent/child&fields=testFieldsQuery&depth=10'
61         when: 'get resource data is called to DMI'
62         objectUnderTest.getResourceDataPassThroughRunningFromDmi('testDmiBasePath',
63                 'testCmhandle',
64                 'parent/child',
65                 'testFieldsQuery',
66                 10,
67                 'testAcceptJson',
68                 'testJsonbody')
69         then: 'the put operation is executed with the correct URL'
70         1 * mockDmiRestClient.putOperationWithJsonData(expectedUrl, 'testJsonbody', _ as HttpHeaders)
71     }
72     def 'call create resource data for pass-through:running datastore from DMI.'() {
73         given: 'expected url'
74         def expectedUrl = 'testDmiBasePath/dmi/v1/ch/testCmhandle/data/ds' +
75                 '/ncmp-datastore:passthrough-running?resourceIdentifier=parent/child'
76         when: 'get resource data is called to DMI'
77         objectUnderTest.createResourceDataPassThroughRunningFromDmi('testDmiBasePath',
78                 'testCmhandle',
79                 'parent/child',
80                 'testJsonbody')
81         then: 'the put operation is executed with the correct URL'
82         1 * mockDmiRestClient.postOperationWithJsonData(expectedUrl, 'testJsonbody', _ as HttpHeaders)
83     }
84
85     def 'Call get resource from dmi.'() {
86         given: 'expected url'
87             def expectedUrl = 'testDmiBasePath/dmi/v1/ch/testCmhandle/modules'
88         when: 'get resource data is called to dmi'
89             objectUnderTest.getResourceFromDmi('testDmiBasePath',
90                     'testCmhandle',
91                     'modules')
92         then: 'the post operation is executed with the correct URL'
93             1 * mockDmiRestClient.postOperation(expectedUrl, _ as HttpHeaders)
94     }
95
96     def 'Call get resource from dmi with json data.'() {
97         given: 'expected url & json data'
98             def requestBody = 'some json'
99             def expectedUrl = 'testDmiBasePath/dmi/v1/ch/testCmHandle/modules'
100             def expectedHttpHeaders = new HttpHeaders()
101         when: 'get resource data is called to dmi'
102             objectUnderTest.getResourceFromDmiWithJsonData('testDmiBasePath',
103                     requestBody,
104                     'testCmHandle',
105                     'modules')
106         then: 'the post operation is executed with the correct URL and json data'
107             1 * mockDmiRestClient.postOperationWithJsonData(expectedUrl, requestBody, expectedHttpHeaders)
108     }
109 }