aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java
blob: e9f94ca0e8af1c6f958a234e2055591cee3b43cd (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2018 - 2019 Nokia. 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.onap.vid.services;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.core.Response;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.randomizers.misc.BooleanRandomizer;
import org.jeasy.random.randomizers.text.StringRandomizer;
import org.junit.Test;
import org.onap.vid.aai.AaiClientInterface;
import org.onap.vid.aai.AaiGetVnfResponse;
import org.onap.vid.aai.AaiOverTLSClientInterface;
import org.onap.vid.aai.AaiResponse;
import org.onap.vid.aai.AaiResponseTranslator;
import org.onap.vid.aai.model.AaiGetServicesRequestModel.GetServicesAAIRespone;
import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
import org.onap.vid.aai.model.VnfResult;
import org.onap.vid.roles.RoleValidator;
import org.onap.vid.roles.RoleValidatorByRoles;

public class AaiServiceImplTest {

    private static final long STATIC_SEED = 5336L;
    private EasyRandomParameters parameters = new EasyRandomParameters()
        .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED))
        .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED));
    private EasyRandom modelGenerator = new EasyRandom(parameters);

    private AaiClientInterface aaiClient = mock(AaiClientInterface.class);
    private AaiOverTLSClientInterface aaiSslClient = mock(AaiOverTLSClientInterface.class);
    private AaiResponseTranslator aaiResponseTranslator = mock(AaiResponseTranslator.class);
    private AAITreeNodeBuilder aaiTreeNode = mock(AAITreeNodeBuilder.class);
    private AAIServiceTree aaiServiceTree = mock(AAIServiceTree.class);

    private AaiServiceImpl aaiService = new AaiServiceImpl(
        aaiClient, aaiSslClient, aaiResponseTranslator, aaiTreeNode, aaiServiceTree
    );

    @Test
    public void shouldRetrievePnf() {
        // given
        String globalCustomerId = "global_customer";
        String serviceType = "service_type";
        String modelVersionId = "model_version";
        String modelInvariantId = "model_invariant_id";
        String cloudRegion = "cloud_region";
        String equipVendor = "equip_vendor";
        String equipModel = "equip_model";

        AaiResponse response = mock(AaiResponse.class);
        when(aaiClient.getPNFData(
            globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor, equipModel
        )).thenReturn(response);

        // when
        AaiResponse actual = aaiService.getPNFData(
            globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor, equipModel
        );

        // then
        assertThat(response).isEqualTo(actual);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void shouldRetrieveSpecificPnf() {
        // given
        String pnfId = "some_pnf_id";

        AaiResponse response = mock(AaiResponse.class);
        when(aaiClient.getSpecificPnf(pnfId)).thenReturn(response);

        // when
        AaiResponse actual = aaiService.getSpecificPnf(pnfId);

        // then
        assertThat(response).isEqualTo(actual);
    }

    @Test
    public void shouldRetrieveTenantsByInvariantId() {
        // given
        List<String> modelInvariantId = new ArrayList<>();

        Response response = mock(Response.class);
        when(aaiClient.getVersionByInvariantId(modelInvariantId)).thenReturn(response);

        // when
        Response actual = aaiService.getVersionByInvariantId(modelInvariantId);

        // then
        assertThat(response).isEqualTo(actual);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void shouldRetrieveTenants() {
        // given
        String globalCustomerId = "global_customer";
        String serviceType = "service_type";

        GetTenantsResponse permittedTenant = new GetTenantsResponse(
            "cloud_region", "cloud_owner", "permitted_tenant", "tenant_id", false
        );
        GetTenantsResponse unpermittedTenant = new GetTenantsResponse(
            "cloud_region", "cloud_owner", "unpermitted_tenant", "tenant_id", false
        );

        AaiResponse<GetTenantsResponse[]> response = mock(AaiResponse.class);
        when(response.getT()).thenReturn(new GetTenantsResponse[]{ permittedTenant, unpermittedTenant });
        when(aaiClient.getTenants(globalCustomerId, serviceType)).thenReturn(response);

        RoleValidator roleValidator = mock(RoleValidatorByRoles.class);
        when(roleValidator.isTenantPermitted(globalCustomerId, serviceType, "permitted_tenant")).thenReturn(true);
        when(roleValidator.isTenantPermitted(globalCustomerId, serviceType, "unpermitted_tenant")).thenReturn(false);

        // when
        AaiResponse actual = aaiService.getTenants(globalCustomerId, serviceType, roleValidator);

        // then
        assertThat(response).isEqualTo(actual);
        assertThat(permittedTenant.isPermitted).isTrue();
        assertThat(unpermittedTenant.isPermitted).isFalse();
    }

    @Test
    public void shouldRetrieveVNFs() {
        // given
        String globalSubscriber = "global_subscriber";
        String serviceType = "service_type";
        String serviceInstanceId = "service_instance";

        AaiResponse response = mock(AaiResponse.class);
        when(aaiClient.getVNFData(globalSubscriber, serviceType, serviceInstanceId)).thenReturn(response);

        // when
        AaiResponse actual = aaiService.getVNFData(globalSubscriber, serviceType, serviceInstanceId);

        // then
        assertThat(response).isEqualTo(actual);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void shouldRetrieveAndFilterVNFsBySubscriberAndServiceType() {
        // given
        String globalSubscriber = "global_subscriber";
        String serviceType = "service_type";

        VnfResult genericVnf = new VnfResult();
        genericVnf.nodeType = "generic-vnf";

        VnfResult serviceInstance = new VnfResult();
        serviceInstance.nodeType = "service-instance";

        VnfResult someVnf = new VnfResult();
        someVnf.nodeType = "some-vnf";

        AaiResponse<AaiGetVnfResponse> response = mock(AaiResponse.class);
        AaiGetVnfResponse vnfs = new AaiGetVnfResponse();
        vnfs.results = Arrays.asList(genericVnf, serviceInstance, someVnf);
        when(response.getT()).thenReturn(vnfs);

        when(aaiClient.getVNFData(globalSubscriber, serviceType)).thenReturn(response);

        // when
        AaiResponse actual = aaiService.getVNFData(globalSubscriber, serviceType);

        // then
        assertThat(response).isEqualTo(actual);
        assertThat(response.getT().results).containsOnly(genericVnf, serviceInstance);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void getServicesShouldMarkAllServicesAsPermitted() {
        // given
        RoleValidator roleValidator = modelGenerator.nextObject(RoleValidatorByRoles.class);

        GetServicesAAIRespone inputPayload = modelGenerator.nextObject(GetServicesAAIRespone.class);
        assertThat(inputPayload.service.stream().allMatch(service -> service.isPermitted)).isFalse();

        when(aaiClient.getServices()).thenReturn(new AaiResponse<>(inputPayload, "", 200));

        // when
        AaiResponse<GetServicesAAIRespone> result = aaiService.getServices(roleValidator);
        GetServicesAAIRespone outputPayload = result.getT();

        // then
        assertThat(outputPayload.service.stream().allMatch(service -> service.isPermitted)).isTrue();
    }

    @Test
    public void shouldGetNodeTemplateInstances() {
        // given
        String globalCustomerId = "gcid";
        String serviceType = "st";
        String modelVersionId = "mvid";
        String modelInvariantId = "miid";
        String cloudRegion = "cr";

        // when
        aaiService
            .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion);

        // then
        verify(aaiClient)
            .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion);
    }

    @Test
    public void shouldGetNetworkCollectionDetails() {
        // given
        String serviceInstanceId = "siid";

        // when
        aaiService.getNetworkCollectionDetails(serviceInstanceId);

        // then
        verify(aaiClient).getNetworkCollectionDetails(serviceInstanceId);
    }

    @Test
    public void shouldGetInstanceGroupsByCloudRegion() {
        // given
        String cloudOwner = "co";
        String cloudRegionId = "crid";
        String networkFunction = "nf";

        // when
        aaiService.getInstanceGroupsByCloudRegion(cloudOwner, cloudRegionId, networkFunction);

        // then
        verify(aaiClient).getInstanceGroupsByCloudRegion(cloudOwner, cloudRegionId, networkFunction);
    }

    @Test
    public void getAAIServiceTree() {
        // given
        String globalCustomerId = "gcid";
        String serviceType = "st";
        String serviceInstanceId = "siid";

        // when
        aaiService.getAAIServiceTree(globalCustomerId, serviceType, serviceInstanceId);

        // then
        verify(aaiServiceTree).getServiceInstanceTopology(globalCustomerId, serviceType, serviceInstanceId);
    }
}