aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/UiComponentDataConverterTest.java
blob: 7ebb3ee2d14fa6e000a7f1f301da82d8ee241fec (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*

 * Copyright (c) 2018 AT&T Intellectual Property.

 *

 * 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.

 */

package org.openecomp.sdc.be.datamodel;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.components.utils.ServiceBuilder;
import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.validator.internal.util.CollectionHelper.asSet;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UiComponentDataConverterTest {

    private PolicyDefinition policy1, policy2;
    private GroupDefinition group1, group2;
    private static GroupTypeBusinessLogic groupTypeBusinessLogic;
    private static PolicyTypeBusinessLogic policyTypeBusinessLogic;
    private static UiComponentDataConverter uiComponentDataConverter;

    @BeforeClass
    public static void initClass() {
        groupTypeBusinessLogic = mock(GroupTypeBusinessLogic.class);
        policyTypeBusinessLogic = mock(PolicyTypeBusinessLogic.class);
        uiComponentDataConverter = new UiComponentDataConverter(groupTypeBusinessLogic, policyTypeBusinessLogic);
    }

    @Before
    public void setUp() throws Exception {
        policy1 = PolicyDefinitionBuilder.create()
                .setName("policy1")
                .setUniqueId("uid1")
                .setType("a")
                .build();

        policy2 = PolicyDefinitionBuilder.create()
                .setName("policy2")
                .setUniqueId("uid2")
                .setType("b")
                .build();
        group1 = GroupDefinitionBuilder.create()
                .setUniqueId("group1")
                .setName("Group 1")
                .setType("a")
                .build();
        group2 = GroupDefinitionBuilder.create()
                .setUniqueId("group2")
                .setName("Group 2")
                .setType("b")
                .build();
    }

    @Test
    public void getUiDataTransferFromResourceByParams_groups_allGroups() {
        Resource resourceWithGroups = buildResourceWithGroups();
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resourceWithGroups, Collections.singletonList("groups"));
        assertThat(componentDTO.getGroups()).isEqualTo(resourceWithGroups.getGroups());
    }

    @Test
    public void getUiDataTransferFromResourceByParams_groups_excludedGroups() {
        Resource resourceWithGroups = buildResourceWithGroups();
        when(groupTypeBusinessLogic.getExcludedGroupTypes("VFC")).thenReturn(buildExcludedTypesList());
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resourceWithGroups, Collections.singletonList("nonExcludedGroups"));
        List<GroupDefinition> groups = componentDTO.getGroups();
        assertThat(groups.size()).isEqualTo(1);
        assertThat(groups.get(0)).isEqualTo(group2);
    }

    @Test
    public void getUiDataTransferFromResourceByParams_policies_noPoliciesForResource() {
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromResourceByParams(new Resource(), Collections.singletonList("policies"));
        assertThat(componentDTO.getPolicies()).isEmpty();
    }

    @Test
    public void getUiDataTransferFromServiceByParams_policies_noPoliciesForResource() {
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromServiceByParams(new Service(), Collections.singletonList("policies"));
        assertThat(componentDTO.getPolicies()).isEmpty();
    }

    @Test
    public void getUiDataTransferFromResourceByParams_policies() {
        Resource resourceWithPolicies = buildResourceWithPolicies();
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resourceWithPolicies, Collections.singletonList("policies"));
        assertThat(componentDTO.getPolicies()).isEqualTo(resourceWithPolicies.resolvePoliciesList());
    }

    @Test
    public void getUiDataTransferFromServiceByParams_policies() {
        Service resourceWithPolicies = buildServiceWithPolicies();
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromServiceByParams(resourceWithPolicies, Collections.singletonList("policies"));
        assertThat(componentDTO.getPolicies()).isEqualTo(resourceWithPolicies.resolvePoliciesList());
    }

    @Test
    public void getUiDataTransferFromResourceByParams_policies_excludedPolicies() {
        Resource resourceWithPolicies = buildResourceWithPolicies();
        when(policyTypeBusinessLogic.getExcludedPolicyTypes("VFC")).thenReturn(buildExcludedTypesList());
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resourceWithPolicies, Collections.singletonList("nonExcludedPolicies"));
        List<PolicyDefinition> policies = componentDTO.getPolicies();
        assertThat(policies.size()).isEqualTo(1);
        assertThat(policies.get(0)).isEqualTo(policy2);
    }

    @Test
    public void getResourceWithoutGroupsAndPolicies_returnsEmptyLists() {
        Resource resource = new ResourceBuilder().build();
        UiComponentDataTransfer componentDTO = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, Arrays.asList("nonExcludedPolicies", "nonExcludedGroups"));
        List<PolicyDefinition> policies = componentDTO.getPolicies();
        assertThat(policies.size()).isZero();
        List<GroupDefinition> groups = componentDTO.getGroups();
        assertThat(groups.size()).isZero();
    }

    private Resource buildResourceWithGroups() {
        return new ResourceBuilder()
                .addGroup(group1)
                .addGroup(group2)
                .build();
    }

    private Resource buildResourceWithPolicies() {
        return new ResourceBuilder()
                .addPolicy(policy1)
                .addPolicy(policy2)
                .build();
    }

    private Service buildServiceWithPolicies() {
        return new ServiceBuilder()
                .addPolicy(policy1)
                .addPolicy(policy2)
                .build();
    }

    private Set<String> buildExcludedTypesList() {
        return asSet("a");
    }

}