055a6e74480349d61600af934606233a4f982d1f
[cps.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory.models
23
24 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse.Status
25 import spock.lang.Specification
26
27 import java.util.stream.Collectors
28
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.ALTERNATE_ID_ALREADY_ASSOCIATED
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST
31 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR
32
33 class CmHandleRegistrationResponseSpec extends Specification {
34
35     def 'Successful cm-handle Registration Response'() {
36         when: 'cm-handle response is created'
37             def cmHandleRegistrationResponse = CmHandleRegistrationResponse.createSuccessResponse('cmHandle')
38         then: 'a success response is returned'
39             with(cmHandleRegistrationResponse) {
40                 assert it.cmHandle == 'cmHandle'
41                 assert it.status == Status.SUCCESS
42             }
43         and: 'error details are null'
44             cmHandleRegistrationResponse.ncmpResponseStatus == null
45             cmHandleRegistrationResponse.errorText == null
46     }
47
48     def 'Failed cm-handle Registration Response: for unexpected exception'() {
49         when: 'cm-handle response is created for an unexpected exception'
50             def cmHandleRegistrationResponse =
51                 CmHandleRegistrationResponse.createFailureResponse('cmHandle', new Exception('unexpected error'))
52         then: 'the response is created with expected value'
53             with(cmHandleRegistrationResponse) {
54                 assert it.ncmpResponseStatus == UNKNOWN_ERROR
55                 assert it.cmHandle == 'cmHandle'
56                 assert errorText == 'unexpected error'
57             }
58     }
59
60     def 'Failed cm-handle Registration Response'() {
61         when: 'cm-handle failure response is created'
62         def cmHandleRegistrationResponse =
63                 CmHandleRegistrationResponse.createFailureResponse('cmHandle', CM_HANDLE_ALREADY_EXIST)
64         then: 'the response is created with expected value'
65         with(cmHandleRegistrationResponse) {
66             assert it.ncmpResponseStatus == CM_HANDLE_ALREADY_EXIST
67             assert it.cmHandle == 'cmHandle'
68             assert it.status == Status.FAILURE
69             assert errorText == CM_HANDLE_ALREADY_EXIST.message
70         }
71     }
72
73     def 'Failed cm-handle Registration with multiple responses.'() {
74         when: 'cm-handle failure response is created for 2 xpaths'
75             def cmHandleRegistrationResponses =
76                 CmHandleRegistrationResponse.createFailureResponsesFromXpaths(["somePathWithId[@id='123']", "somePathWithId[@id='456']"], CM_HANDLE_ALREADY_EXIST)
77         then: 'the response has the correct cm handle ids'
78             assert cmHandleRegistrationResponses.size() == 2
79             assert cmHandleRegistrationResponses.stream().map(it -> it.cmHandle).collect(Collectors.toList())
80                 .containsAll(['123','456'])
81     }
82
83     def 'Failed cm-handle Registration with multiple responses with an unexpected xpath.'() {
84         when: 'cm-handle failure response is created for one valid and one unexpected xpath'
85             def cmHandleRegistrationResponses =
86                 CmHandleRegistrationResponse.createFailureResponsesFromXpaths(["somePathWithId[@id='123']", "valid/xpath/without-id[@key='123']"], CM_HANDLE_ALREADY_EXIST)
87         then: 'the response has only one entry'
88             assert cmHandleRegistrationResponses.size() == 1
89     }
90
91     def 'Failed cm-handle registration based on cm handle id and registration error'() {
92         when: 'the failure response is created with "alternate id already associated" error code for 1 cm handle'
93             def cmHandleRegistrationResponses =
94                     CmHandleRegistrationResponse.createFailureResponses(['ch 1'], ALTERNATE_ID_ALREADY_ASSOCIATED)
95         then: 'the response with expected values'
96             assert cmHandleRegistrationResponses[0].cmHandle == 'ch 1'
97             assert cmHandleRegistrationResponses[0].status == Status.FAILURE
98             assert cmHandleRegistrationResponses[0].ncmpResponseStatus == ALTERNATE_ID_ALREADY_ASSOCIATED
99             assert cmHandleRegistrationResponses[0].errorText == 'alternate id already associated'
100     }
101
102 }