aboutsummaryrefslogtreecommitdiffstats
path: root/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NcmpRestInputMapperSpec.groovy
blob: dfe7bf3853c5b19ee904b969f1b0a26c6c39deec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.ncmp.rest.controller

import org.mapstruct.factory.Mappers
import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters
import org.onap.cps.ncmp.rest.model.ConditionProperties
import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
import org.onap.cps.ncmp.rest.model.RestInputCmHandle
import org.onap.cps.ncmp.rest.model.RestModuleDefinition
import org.onap.cps.ncmp.rest.model.RestModuleReference
import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
import org.onap.cps.spi.model.ModuleDefinition
import org.onap.cps.spi.model.ModuleReference
import spock.lang.Specification

class NcmpRestInputMapperSpec extends Specification {

    def objectUnderTest = Mappers.getMapper(NcmpRestInputMapper.class)

    def 'Convert a created REST CM Handle Input to an NCMP Service CM Handle with #scenario'() {
        given: 'a rest cm handle input'
            def inputRestCmHandle = new RestInputCmHandle(cmHandle : 'example-id', cmHandleProperties: dmiProperties,
                publicCmHandleProperties: publicProperties)
            def restDmiPluginRegistration = new RestDmiPluginRegistration(
                createdCmHandles: [inputRestCmHandle])
        when: 'to plugin dmi registration is called'
            def result  = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
        then: 'the result returns the correct number of cm handles'
            result.createdCmHandles.size() == 1
        and: 'the converted cm handle has the same id'
            result.createdCmHandles[0].cmHandleId == 'example-id'
        and: '(empty) properties are converted correctly'
            result.createdCmHandles[0].dmiProperties == expectedDmiProperties
            result.createdCmHandles[0].publicProperties == expectedPublicProperties
        where: 'the following parameters are used'
            scenario                    | dmiProperties                            | publicProperties                                         || expectedDmiProperties                     | expectedPublicProperties
            'dmi and public properties' | ['Property-Example': 'example property'] | ['Public-Property-Example': 'public example property']   || ['Property-Example': 'example property']  | ['Public-Property-Example': 'public example property']
            'no properties'             | null                                     | null                                                     || [:]                                       | [:]
    }

    def 'Handling empty dmi registration'() {
        given: 'a rest cm handle input without any cm handles'
            def restDmiPluginRegistration = new RestDmiPluginRegistration()
        when: 'to plugin dmi registration is called'
            def result  = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
        then: 'unspecified lists remain as empty lists'
            assert result.createdCmHandles == []
            assert result.updatedCmHandles == []
            assert result.removedCmHandles == []
    }

    def 'Handling non-empty dmi registration'() {
        given: 'a rest cm handle input with cm handles'
            def restDmiPluginRegistration = new RestDmiPluginRegistration(
                createdCmHandles: [new RestInputCmHandle()],
                updatedCmHandles: [new RestInputCmHandle()],
                removedCmHandles: ["some-cmHandle"]
            )
        when: 'to dmi plugin registration is called'
            def result  = objectUnderTest.toDmiPluginRegistration(restDmiPluginRegistration)
        then: 'Lists contain values'
            assert result.createdCmHandles[0].class == NcmpServiceCmHandle.class
            assert result.updatedCmHandles[0].class == NcmpServiceCmHandle.class
            assert result.removedCmHandles == ["some-cmHandle"]
    }

    def 'Convert a ModuleReference to a RestModuleReference'() {
        given: 'a ModuleReference'
            def moduleReference = new ModuleReference()
        when: 'toRestModuleReference is called'
            def result = objectUnderTest.toRestModuleReference(moduleReference)
        then: 'the result is of the correct class RestModuleReference'
            result.class == RestModuleReference.class
    }

    def 'Convert a ModuleDefinition to a RestModuleDefinition'() {
        given: 'a ModuleDefinition'
            def moduleDefinition = new ModuleDefinition('moduleName','revision', 'content')
        when: 'toRestModuleDefinition is called'
            def result = objectUnderTest.toRestModuleDefinition(moduleDefinition)
        then: 'the result is of the correct class RestModuleDefinition'
            result.class == RestModuleDefinition.class
        and: 'all contents are mapped correctly'
            result.toString()=='class RestModuleDefinition {\n' +
                    '    moduleName: moduleName\n' +
                    '    revision: revision\n' +
                    '    content: content\n' +
                    '}'
    }

    def 'Convert a CmHandle REST query to CmHandle query service parameters.'() {
        given: 'a CmHandle REST query with two conditions'
            def conditionParameter1 = new ConditionProperties(conditionName: 'some condition', conditionParameters: [[p1:1]] )
            def conditionParameter2 = new ConditionProperties(conditionName: 'other condition', conditionParameters: [[p2:2]] )
            def cmHandleQuery = new CmHandleQueryParameters()
            cmHandleQuery.cmHandleQueryParameters = [conditionParameter1, conditionParameter2]
        when: 'it is converted into CmHandle query service parameters'
            def result = objectUnderTest.toCmHandleQueryServiceParameters(cmHandleQuery)
        then: 'the result is of the correct class'
            assert result instanceof CmHandleQueryServiceParameters
        and: 'the result has the same conditions'
            assert result.cmHandleQueryParameters.size() == 2
            assert result.cmHandleQueryParameters[0].conditionName == 'some condition'
            assert result.cmHandleQueryParameters[0].conditionParameters == [[p1:1]]
            assert result.cmHandleQueryParameters[1].conditionName == 'other condition'
            assert result.cmHandleQueryParameters[1].conditionParameters == [[p2:2]]
    }
}