2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * Modifications Copyright (C) 2023-2024 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.ALTERNATE_ID_ALREADY_ASSOCIATED
25 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST
26 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR
28 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
29 import spock.lang.Specification
30 import java.util.stream.Collectors
32 class CmHandleRegistrationResponseSpec extends Specification {
34 def 'Successful cm-handle Registration Response'() {
35 when: 'cm-handle response is created'
36 def cmHandleRegistrationResponse = CmHandleRegistrationResponse.createSuccessResponse('cmHandle')
37 then: 'a success response is returned'
38 with(cmHandleRegistrationResponse) {
39 assert it.cmHandle == 'cmHandle'
40 assert it.status == Status.SUCCESS
42 and: 'error details are null'
43 cmHandleRegistrationResponse.ncmpResponseStatus == null
44 cmHandleRegistrationResponse.errorText == null
47 def 'Failed cm-handle Registration Response: for unexpected exception'() {
48 when: 'cm-handle response is created for an unexpected exception'
49 def cmHandleRegistrationResponse =
50 CmHandleRegistrationResponse.createFailureResponse('cmHandle', new Exception('unexpected error'))
51 then: 'the response is created with expected value'
52 with(cmHandleRegistrationResponse) {
53 assert it.ncmpResponseStatus == UNKNOWN_ERROR
54 assert it.cmHandle == 'cmHandle'
55 assert errorText == 'unexpected error'
59 def 'Failed cm-handle Registration Response'() {
60 when: 'cm-handle failure response is created'
61 def cmHandleRegistrationResponse =
62 CmHandleRegistrationResponse.createFailureResponse('cmHandle', CM_HANDLE_ALREADY_EXIST)
63 then: 'the response is created with expected value'
64 with(cmHandleRegistrationResponse) {
65 assert it.ncmpResponseStatus == CM_HANDLE_ALREADY_EXIST
66 assert it.cmHandle == 'cmHandle'
67 assert it.status == Status.FAILURE
68 assert errorText == CM_HANDLE_ALREADY_EXIST.message
72 def 'Failed cm-handle Registration with multiple responses.'() {
73 when: 'cm-handle failure response is created for 2 xpaths'
74 def cmHandleRegistrationResponses =
75 CmHandleRegistrationResponse.createFailureResponsesFromXpaths(["somePathWithId[@id='123']", "somePathWithId[@id='456']"], CM_HANDLE_ALREADY_EXIST)
76 then: 'the response has the correct cm handle ids'
77 assert cmHandleRegistrationResponses.size() == 2
78 assert cmHandleRegistrationResponses.stream().map(it -> it.cmHandle).collect(Collectors.toList())
79 .containsAll(['123','456'])
82 def 'Failed cm-handle Registration with multiple responses with an unexpected xpath.'() {
83 when: 'cm-handle failure response is created for one valid and one unexpected xpath'
84 def cmHandleRegistrationResponses =
85 CmHandleRegistrationResponse.createFailureResponsesFromXpaths(["somePathWithId[@id='123']", "valid/xpath/without-id[@key='123']"], CM_HANDLE_ALREADY_EXIST)
86 then: 'the response has only one entry'
87 assert cmHandleRegistrationResponses.size() == 1
90 def 'Failed cm-handle registration based on cm handle id and registration error'() {
91 when: 'the failure response is created with "alternate id already associated" error code for 1 cm handle'
92 def cmHandleRegistrationResponses =
93 CmHandleRegistrationResponse.createFailureResponses(['ch 1'], ALTERNATE_ID_ALREADY_ASSOCIATED)
94 then: 'the response with expected values'
95 assert cmHandleRegistrationResponses[0].cmHandle == 'ch 1'
96 assert cmHandleRegistrationResponses[0].status == Status.FAILURE
97 assert cmHandleRegistrationResponses[0].ncmpResponseStatus == ALTERNATE_ID_ALREADY_ASSOCIATED
98 assert cmHandleRegistrationResponses[0].errorText == 'alternate id already associated'