2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * Modifications Copyright (C) 2023 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.models
24 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST
25 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR
27 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
28 import spock.lang.Specification
29 import java.util.stream.Collectors
31 class CmHandleRegistrationResponseSpec extends Specification {
33 def 'Successful cm-handle Registration Response'() {
34 when: 'cm-handle response is created'
35 def cmHandleRegistrationResponse = CmHandleRegistrationResponse.createSuccessResponse('cmHandle')
36 then: 'a success response is returned'
37 with(cmHandleRegistrationResponse) {
38 assert it.cmHandle == 'cmHandle'
39 assert it.status == Status.SUCCESS
41 and: 'error details are null'
42 cmHandleRegistrationResponse.ncmpResponseStatus == null
43 cmHandleRegistrationResponse.errorText == null
46 def 'Failed cm-handle Registration Response: for unexpected exception'() {
47 when: 'cm-handle response is created for an unexpected exception'
48 def cmHandleRegistrationResponse =
49 CmHandleRegistrationResponse.createFailureResponse('cmHandle', new Exception('unexpected error'))
50 then: 'the response is created with expected value'
51 with(cmHandleRegistrationResponse) {
52 assert it.ncmpResponseStatus == UNKNOWN_ERROR
53 assert it.cmHandle == 'cmHandle'
54 assert errorText == 'unexpected error'
58 def 'Failed cm-handle Registration Response'() {
59 when: 'cm-handle failure response is created'
60 def cmHandleRegistrationResponse =
61 CmHandleRegistrationResponse.createFailureResponse('cmHandle', CM_HANDLE_ALREADY_EXIST)
62 then: 'the response is created with expected value'
63 with(cmHandleRegistrationResponse) {
64 assert it.ncmpResponseStatus == CM_HANDLE_ALREADY_EXIST
65 assert it.cmHandle == 'cmHandle'
66 assert it.status == Status.FAILURE
67 assert errorText == CM_HANDLE_ALREADY_EXIST.message
71 def 'Failed cm-handle Registration with multiple responses.'() {
72 when: 'cm-handle failure response is created for 2 xpaths'
73 def cmHandleRegistrationResponses =
74 CmHandleRegistrationResponse.createFailureResponses(["somePathWithId[@id='123']","somePathWithId[@id='456']"], CM_HANDLE_ALREADY_EXIST)
75 then: 'the response has the correct cm handle ids'
76 assert cmHandleRegistrationResponses.size() == 2
77 assert cmHandleRegistrationResponses.stream().map(it -> it.cmHandle).collect(Collectors.toList())
78 .containsAll(['123','456'])
81 def 'Failed cm-handle Registration with multiple responses with an unexpected xpath.'() {
82 when: 'cm-handle failure response is created for one valid and one unexpected xpath'
83 def cmHandleRegistrationResponses =
84 CmHandleRegistrationResponse.createFailureResponses(["somePathWithId[@id='123']","valid/xpath/without-id[@key='123']"], CM_HANDLE_ALREADY_EXIST)
85 then: 'the response has only one entry'
86 assert cmHandleRegistrationResponses.size() == 1