Replace RestTemplate with WebClient in synchronous DMI calls
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / NcmpBearerTokenPassthroughSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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.integration.functional
22
23 import spock.lang.Ignore
24
25 import java.time.Duration
26 import org.onap.cps.integration.base.CpsIntegrationSpecBase
27 import org.springframework.http.HttpHeaders
28 import org.springframework.http.HttpStatus
29 import org.springframework.http.MediaType
30 import org.springframework.test.web.client.match.MockRestRequestMatchers
31
32 import static org.springframework.http.HttpMethod.GET
33 import static org.springframework.http.HttpMethod.DELETE
34 import static org.springframework.http.HttpMethod.PATCH
35 import static org.springframework.http.HttpMethod.POST
36 import static org.springframework.http.HttpMethod.PUT
37 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method
38 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo
39 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request
41 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
42
43 @Ignore
44 class NcmpBearerTokenPassthroughSpec extends CpsIntegrationSpecBase {
45
46     static final MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_Response.json')
47     static final MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json')
48
49     def setup() {
50         mockDmiWillRespondToHealthChecks(DMI_URL)
51         mockDmiResponsesForModuleSync(DMI_URL, 'ch-1', MODULE_REFERENCES_RESPONSE, MODULE_RESOURCES_RESPONSE)
52         registerCmHandle(DMI_URL, 'ch-1', NO_MODULE_SET_TAG)
53         mockDmiServer.reset()
54         mockDmiWillRespondToHealthChecks(DMI_URL)
55     }
56
57     def cleanup() {
58         deregisterCmHandle(DMI_URL, 'ch-1')
59     }
60
61     def 'Bearer token is passed from NCMP to DMI in pass-through data operations.'() {
62         given: 'DMI will expect to receive a request with a bearer token'
63             def targetDmiUrl = "$DMI_URL/dmi/v1/ch/ch-1/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=my-resource-id"
64             mockDmiServer.expect(requestTo(targetDmiUrl))
65                     .andExpect(MockRestRequestMatchers.header(HttpHeaders.AUTHORIZATION, 'Bearer some-bearer-token'))
66                     .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON))
67
68         when: 'a pass-through data request is sent to NCMP with a bearer token'
69             mvc.perform(request(httpMethod, '/ncmp/v1/ch/ch-1/data/ds/ncmp-datastore:passthrough-running')
70                     .queryParam('resourceIdentifier', 'my-resource-id')
71                     .contentType(MediaType.APPLICATION_JSON)
72                     .content('{ "some-json": "data" }')
73                     .header(HttpHeaders.AUTHORIZATION, 'Bearer some-bearer-token'))
74                     .andExpect(status().is2xxSuccessful())
75
76         then: 'DMI has received request with bearer token'
77             mockDmiServer.verify()
78
79         where: 'all HTTP operations are applied'
80             httpMethod << [GET, POST, PUT, PATCH, DELETE]
81     }
82
83     def 'Basic auth header is NOT passed from NCMP to DMI in pass-through data operations.'() {
84         given: 'DMI will expect to receive a request with no authorization header'
85             def targetDmiUrl = "$DMI_URL/dmi/v1/ch/ch-1/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=my-resource-id"
86             mockDmiServer.expect(requestTo(targetDmiUrl))
87                     .andExpect(MockRestRequestMatchers.headerDoesNotExist(HttpHeaders.AUTHORIZATION))
88                     .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON))
89
90         when: 'a pass-through data request is sent to NCMP with basic authentication'
91             mvc.perform(request(httpMethod, '/ncmp/v1/ch/ch-1/data/ds/ncmp-datastore:passthrough-running')
92                     .queryParam('resourceIdentifier', 'my-resource-id')
93                     .contentType(MediaType.APPLICATION_JSON)
94                     .content('{ "some-json": "data" }')
95                     .header(HttpHeaders.AUTHORIZATION, 'Basic Y3BzdXNlcjpjcHNyMGNrcyE='))
96                     .andExpect(status().is2xxSuccessful())
97
98         then: 'DMI has received request with no authorization header'
99             mockDmiServer.verify()
100
101         where: 'all HTTP operations are applied'
102             httpMethod << [GET, POST, PUT, PATCH, DELETE]
103     }
104
105     def 'Bearer token is passed from NCMP to DMI in async batch pass-through data operation.'() {
106         given: 'DMI will expect to receive a request with a bearer token'
107             mockDmiServer.expect(method(POST))
108                     .andExpect(MockRestRequestMatchers.header(HttpHeaders.AUTHORIZATION, 'Bearer some-bearer-token'))
109                     .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON))
110
111         when: 'a pass-through async data request is sent to NCMP with a bearer token'
112             def requestBody = """{"operations": [{
113                 "operation": "read",
114                 "operationId": "operational-1",
115                 "datastore": "ncmp-datastore:passthrough-running",
116                 "resourceIdentifier": "my-resource-id",
117                 "targetIds": ["ch-1"]
118             }]}"""
119         mvc.perform(request(POST, '/ncmp/v1/data')
120                     .queryParam('topic', 'my-topic')
121                     .contentType(MediaType.APPLICATION_JSON)
122                     .content(requestBody)
123                     .header(HttpHeaders.AUTHORIZATION, 'Bearer some-bearer-token'))
124                     .andExpect(status().is2xxSuccessful())
125
126         then: 'DMI will receive the async request with bearer token'
127             mockDmiServer.verify(Duration.ofSeconds(1))
128     }
129
130 }