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.ncmp
 
  23 import com.fasterxml.jackson.databind.ObjectMapper
 
  24 import org.onap.cps.integration.base.CpsIntegrationSpecBase
 
  25 import org.springframework.http.HttpHeaders
 
  26 import org.springframework.http.MediaType
 
  28 import static org.springframework.http.HttpMethod.POST
 
  29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request
 
  31 class PolicyExecutorIntegrationSpec extends CpsIntegrationSpecBase {
 
  33     def objectMapper = new ObjectMapper()
 
  36         // Enable mocked policy executor logic
 
  37         policyDispatcher.allowAll = false;
 
  38         //minimum setup for cm handles with alternate ids
 
  39         dmiDispatcher1.moduleNamesPerCmHandleId = ['ch-1': [], 'ch-2': [], 'ch-3':[]]
 
  40         registerCmHandle(DMI1_URL, 'ch-1', NO_MODULE_SET_TAG, 'fdn1')
 
  41         registerCmHandle(DMI1_URL, 'ch-2', NO_MODULE_SET_TAG, 'fdn2')
 
  42         registerCmHandle(DMI1_URL, 'ch-3', NO_MODULE_SET_TAG, 'mock slow response')
 
  46         deregisterCmHandle(DMI1_URL, 'ch-1')
 
  47         deregisterCmHandle(DMI1_URL, 'ch-2')
 
  48         deregisterCmHandle(DMI1_URL, 'ch-3')
 
  51     def 'Policy Executor create request with #scenario.'() {
 
  52         when: 'a pass-through write request is sent to NCMP'
 
  53             def response = mvc.perform(request(POST, "/ncmp/v1/ch/$cmHandle/data/ds/ncmp-datastore:passthrough-running")
 
  54                     .queryParam('resourceIdentifier', 'my-resource-id')
 
  55                     .contentType(MediaType.APPLICATION_JSON)
 
  56                     .content('{ "some-json": "data" }')
 
  57                     .header(HttpHeaders.AUTHORIZATION, authorization))
 
  59         then: 'the expected status code is returned'
 
  60             response.getStatus() == execpectedStatusCode
 
  61         and: 'when not allowed the response body contains the expected message'
 
  62             if (expectedMessage!='allow') {
 
  63                 def bodyAsMap = objectMapper.readValue(response.getContentAsByteArray(), Map.class)
 
  64                 assert bodyAsMap.get('message').endsWith(expectedMessage)
 
  66         where: 'following parameters are used'
 
  67             scenario                | cmHandle | authorization         || execpectedStatusCode || expectedMessage
 
  68             'accepted cm handle'    | 'ch-1'   | 'mock expects "ABC"'  || 201                  || 'allow'
 
  69             'un-accepted cm handle' | 'ch-2'   | 'mock expects "ABC"'  || 409                  || 'deny from mock server (dispatcher)'
 
  70             'timeout'               | 'ch-3'   | 'mock expects "ABC"'  || 409                  || 'test default decision'
 
  71             'invalid authorization' | 'ch-1'   | 'something else'      || 500                  || '401 Unauthorized from POST http://localhost:8790/policy-executor/api/v1/execute'