aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/capability/SimpleCapabilityResolverTest.java
blob: a94a2cf4c8523b5613fa624b018f934818fbebe0 (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
129
130
131
132
133
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.merge.capability;

import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
import org.openecomp.sdc.be.components.utils.CapabilityDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.model.CapabilityDefinition;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.Resource;

import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class SimpleCapabilityResolverTest {

    public static final String NEW_OWNER_ID1 = "owner1";
    public static final String NEW_OWNER_ID2 = "owner2";
    public static final String OLD_OWNER_ID1 = "oldOwner1";
    private static final String OLD_OWNER_ID2 = "oldOwner2";
    public static final String OLD_OWNER_ID3 = "oldOwner3";
    @InjectMocks
    private SimpleCapabilityResolver testInstance;
    @Mock
    private MergeInstanceUtils MergeInstanceUtils;

    private CapabilityDefinition capName1Owner1Type1;
    private CapabilityDefinition capName1Owner1Type2;
    private CapabilityDefinition capName1Owner2Type1;
    private CapabilityDefinition capName2Owner1Type2;

    private CapabilityDefinition prevCapName1Owner1Type1;
    private CapabilityDefinition prevCapName1Owner1Type2;
    private CapabilityDefinition prevCapName1Owner2Type1;
    private CapabilityDefinition prevCapName2Owner1Type2;
    private CapabilityDefinition prevCapName1Owner3Type1;
    private CapabilityDefinition prevCapName3Owner1Type1;
    private CapabilityDefinition prevCapName1Owner1Type3;
    CapabilityDefinition[] prevCapabilities;
    private ComponentInstance oldInstance;
    private ComponentInstance currInstance;


    @Before
    public void setUp() {
        capName1Owner1Type1 = new CapabilityDefinitionBuilder().setName("name1").setOwnerId(NEW_OWNER_ID1).setType("type1").build();
        prevCapName1Owner1Type1 = new CapabilityDefinition(capName1Owner1Type1);
        prevCapName1Owner1Type1.setOwnerId(OLD_OWNER_ID1);

        capName1Owner1Type2 = new CapabilityDefinitionBuilder().setName("name1").setOwnerId(NEW_OWNER_ID1).setType("type2").build();
        prevCapName1Owner1Type2 = new CapabilityDefinition(capName1Owner1Type2);
        prevCapName1Owner1Type2.setOwnerId(OLD_OWNER_ID1);

        capName1Owner2Type1 = new CapabilityDefinitionBuilder().setName("name1").setOwnerId(NEW_OWNER_ID2).setType("type1").build();
        prevCapName1Owner2Type1 = new CapabilityDefinition(capName1Owner2Type1);
        prevCapName1Owner2Type1.setOwnerId(OLD_OWNER_ID2);

        capName2Owner1Type2 = new CapabilityDefinitionBuilder().setName("name2").setOwnerId(NEW_OWNER_ID1).setType("type2").build();
        prevCapName2Owner1Type2 = new CapabilityDefinition(capName2Owner1Type2);
        prevCapName2Owner1Type2.setOwnerId(OLD_OWNER_ID1);

        //prev capabilities that are not mapped to any new capability
        prevCapName1Owner3Type1 = new CapabilityDefinitionBuilder().setName("name1").setOwnerId(OLD_OWNER_ID3).setType("type1").build();
        prevCapName3Owner1Type1 = new CapabilityDefinitionBuilder().setName("name3").setOwnerId(OLD_OWNER_ID1).setType("type1").build();
        prevCapName1Owner1Type3 = new CapabilityDefinitionBuilder().setName("name1").setOwnerId(OLD_OWNER_ID1).setType("type3").build();

        currInstance = new ComponentInstanceBuilder().setId("inst1").addCapabilities(capName1Owner1Type1, capName1Owner1Type2, capName1Owner2Type1, capName2Owner1Type2).build();
        prevCapabilities = new CapabilityDefinition[]{prevCapName1Owner1Type1, prevCapName1Owner1Type2, prevCapName1Owner2Type1, prevCapName2Owner1Type2, prevCapName1Owner3Type1, prevCapName3Owner1Type1, prevCapName1Owner1Type3};
        oldInstance = new ComponentInstanceBuilder().setId("inst1").addCapabilities(prevCapabilities).build();
    }

    @Test
    public void resolvePrevCapIdToNewCapability_resolveByTypeNameAndNewOwnerId() {
        when(MergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(oldInstance, currInstance)).thenReturn(ImmutableMap.of(OLD_OWNER_ID1, NEW_OWNER_ID1, OLD_OWNER_ID2, NEW_OWNER_ID2));
        Map<CapabilityDefinition, CapabilityDefinition> oldToNewMap = testInstance.resolvePrevCapIdToNewCapability(oldInstance, currInstance);
        assertThat(oldToNewMap).hasSize(4);
        assertThat(oldToNewMap).containsEntry(prevCapName1Owner1Type1, capName1Owner1Type1);
        assertThat(oldToNewMap).containsEntry(prevCapName1Owner1Type2, capName1Owner1Type2);
        assertThat(oldToNewMap).containsEntry(prevCapName1Owner2Type1, capName1Owner2Type1);
        assertThat(oldToNewMap).containsEntry(prevCapName2Owner1Type2, capName2Owner1Type2);
    }

    @Test
    public void resolvePrevCapIdToNewCapability_noMatchingNewOwnerIds() {
        when(MergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(oldInstance, currInstance)).thenReturn(emptyMap());
        Map<CapabilityDefinition, CapabilityDefinition> oldToNewMap = testInstance.resolvePrevCapIdToNewCapability(oldInstance, currInstance);
        assertThat(oldToNewMap).isEmpty();
    }

    @Test
    public void mapOldToNewInstanceCapabilitiesOwnerIds() {
        Resource container = new ResourceBuilder().addComponentInstance(currInstance).build();
        Resource prevInstanceOrigNode = new Resource();
        when(MergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, prevInstanceOrigNode, "inst1", asList(OLD_OWNER_ID1, OLD_OWNER_ID2, OLD_OWNER_ID3))).thenReturn(ImmutableMap.of(OLD_OWNER_ID1, NEW_OWNER_ID1, OLD_OWNER_ID2, NEW_OWNER_ID2));
        Map<CapabilityDefinition, CapabilityDefinition> oldToNewMap = testInstance.resolvePrevCapToNewCapability(container, prevInstanceOrigNode, "inst1", asList(prevCapabilities));
        assertThat(oldToNewMap).hasSize(4);
        assertThat(oldToNewMap).containsEntry(prevCapName1Owner1Type1, capName1Owner1Type1);
        assertThat(oldToNewMap).containsEntry(prevCapName1Owner1Type2, capName1Owner1Type2);
        assertThat(oldToNewMap).containsEntry(prevCapName1Owner2Type1, capName1Owner2Type1);
        assertThat(oldToNewMap).containsEntry(prevCapName2Owner1Type2, capName2Owner1Type2);
    }
}