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