aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateServiceTest.java
blob: 01fb4bd33759378deb059da9687864463c9a4041 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
 * Copyright 2016-2017 ZTE Corporation.
 *
 * 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.onap.usecaseui.server.service.lcm.impl;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.onap.usecaseui.server.bean.lcm.ServiceTemplateInput;
import org.onap.usecaseui.server.bean.lcm.TemplateInput;
import org.onap.usecaseui.server.service.lcm.ServiceTemplateService;
import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.SDNCController;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.SDNCControllerRsp;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfoRsp;
import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
import org.openecomp.sdc.toscaparser.api.NodeTemplate;
import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
import org.openecomp.sdc.toscaparser.api.common.JToscaException;
import org.openecomp.sdc.toscaparser.api.elements.Metadata;
import org.openecomp.sdc.toscaparser.api.parameters.Input;
import retrofit2.Call;

import java.io.IOException;
import java.util.*;

import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
import static org.onap.usecaseui.server.util.CallStub.failedCall;
import static org.onap.usecaseui.server.util.CallStub.successfulCall;

public class DefaultServiceTemplateServiceTest {
	
    @Test
    public void itCanListDistributedServiceTemplate() {
        List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "V1","url", "category"));
        SDCCatalogService sdcService = mock(SDCCatalogService.class);
        when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates));

        ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);

        Assert.assertSame(templates, service.listDistributedServiceTemplate());
    }

    @Test(expected = SDCCatalogException.class)
    public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() {
        SDCCatalogService sdcService = mock(SDCCatalogService.class);
        when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!"));

        ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
        service.listDistributedServiceTemplate();
    }

    @Test
    public void itWillRetrieveEmptyWhenNoServiceTemplateCanGet() {
        SDCCatalogService sdcService = mock(SDCCatalogService.class);
        when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(emptyBodyCall());

        ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
        List<SDCServiceTemplate> sdcServiceTemplates = service.listDistributedServiceTemplate();

        Assert.assertTrue("service templates should be empty.", sdcServiceTemplates.isEmpty());
    }

    @Test
    public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
        final String uuid = "1";
        String modelPath = "model_path";
        String nodeUUID = "2";

        SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);

        List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
        AAIService aaiService = newAAIService(vim);

        ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);

        Assert.assertNotNull(service.fetchServiceTemplateInput(uuid, modelPath));    }

    private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
        return new DefaultServiceTemplateService(sdcService, aaiService) {

            @Override
            protected void downloadFile(String templateUrl, String toPath) throws IOException {
                // download successfully...
            }

            @Override
            protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
                if (toPath.contains(uuid)) {
                    return e2eToscaTemplate(nodeUUID);
                }
                return nodeToscaTemplate(nodeUUID);
            }
        };
    }

    private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
        SDCCatalogService sdcService = mock(SDCCatalogService.class);
        when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "V1", "nodeModelUrl", "service")));
        return sdcService;
    }

    private ServiceTemplateInput expectedServiceInputs(String uuid, String nodeUUID) {
        ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
                uuid, uuid, "VoLTE", "service","", "VoLTE", "service", "","", Collections.EMPTY_LIST);
        TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
        ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
                nodeUUID, nodeUUID, "", "", "","", "service", "","", Collections.singletonList(templateInput));
//        e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
        return e2eServiceTemplateInput;
    }

    private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
        ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
        Map<String, Object> e2eAttributes = new HashMap<>();
        e2eAttributes.put("invariantUUID", "1");
        e2eAttributes.put("UUID", "1");
        e2eAttributes.put("name", "VoLTE");
        e2eAttributes.put("type", "service");
        e2eAttributes.put("description", "VoLTE");
        e2eAttributes.put("category", "service");
        e2eAttributes.put("subcategory", "");
        e2eAttributes.put("version", "");
        when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
        when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
        NodeTemplate nodeTemplate = mock(NodeTemplate.class);

        Map<String, Object> nodeUUIDAttr = new HashMap<>();

        nodeUUIDAttr.put("UUID", nodeUUID);
        when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));

        ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
//        nodeTemplates.add(nodeTemplate);
        when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);

        return toscaTemplate;
    }

    private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
        ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
        Map<String, Object> Attributes = new HashMap<>();
        Attributes.put("invariantUUID", nodeUUID);
        Attributes.put("UUID", nodeUUID);
        Attributes.put("name", "");
        Attributes.put("type", "");
        Attributes.put("description", "");
        Attributes.put("category", "service");
        Attributes.put("subcategory", "");
        when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));

        Input input = mock(Input.class);
        when(input.getName()).thenReturn("field_name");
        when(input.getDescription()).thenReturn("field_description");
        when(input.getType()).thenReturn("field_type");
        when(input.getDefault()).thenReturn("field_default");
        when(input.isRequired()).thenReturn(true);

        ArrayList<Input> inputs = new ArrayList<>();
        inputs.add(input);
        when(toscaTemplate.getInputs()).thenReturn(inputs);
        when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());

        return toscaTemplate;
    }

    private AAIService newAAIService(List<VimInfo> vim) {
        AAIService aaiService = mock(AAIService.class);
        VimInfoRsp rsp = new VimInfoRsp();
        rsp.setCloudRegion(vim);
        Call<VimInfoRsp> vimCall = successfulCall(rsp);
        when(aaiService.listVimInfo()).thenReturn(vimCall);
        return aaiService;
    }

    @Test(expected = SDCCatalogException.class)
    public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
        ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
            @Override
            protected void downloadFile(String templateUrl, String toPath) throws IOException {
                throw new IOException("download failed!");
            }
        };
        service.fetchServiceTemplateInput("1", "url");
    }

    @Test(expected = SDCCatalogException.class)
    public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
        ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
            @Override
            protected void downloadFile(String templateUrl, String toPath) throws IOException {
                // download successfully...
            }

            @Override
            protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
                throw new JToscaException("parse tosca template failed!", "123");
            }
        };
        service.fetchServiceTemplateInput("1", "url");
    }

    @Test
    public void itCanListVim() {
        List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "region"));
        VimInfoRsp rsp = new VimInfoRsp();
        rsp.setCloudRegion(vim);
        AAIService aaiService = mock(AAIService.class);
        when(aaiService.listVimInfo()).thenReturn(successfulCall(rsp));

        ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);

        Assert.assertSame(vim, service.listVim());
    }

    @Test
    public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
        AAIService aaiService = mock(AAIService.class);
        when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());

        ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
        List<VimInfo> vimInfos = service.listVim();

        Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
    }

    @Test(expected = AAIException.class)
    public void itCanThrowExceptionWhenAAIServiceIsNotAvailable() {
        AAIService aaiService = mock(AAIService.class);
        when(aaiService.listVimInfo()).thenReturn(failedCall("AAI is not available!"));

        ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
        service.listVim();
    }

    @Test
    public void itCanListSDNController() {
        List<SDNCController> controllers = Collections.singletonList(new SDNCController());
        SDNCControllerRsp rsp = new SDNCControllerRsp();
        rsp.setEsrThirdpartySdncList(controllers);
        AAIService aaiService = mock(AAIService.class);
        when(aaiService.listSdncControllers()).thenReturn(successfulCall(rsp));

        ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);

        Assert.assertSame(controllers, service.listSDNCControllers());
    }

    @Test
    public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
        AAIService aaiService = mock(AAIService.class);
        when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());

        ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
        List<SDNCController> controllers = service.listSDNCControllers();

        Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
    }

    @Test(expected = AAIException.class)
    public void itListSDNControllerThrowExceptionWhenAAIServiceIsNotAvailable() {
        AAIService aaiService = mock(AAIService.class);
        when(aaiService.listSdncControllers()).thenReturn(failedCall("AAI is not available!"));

        ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
        service.listSDNCControllers();
    }
    @Test
    public void testDownloadFile() throws IOException {
        SDCCatalogService sdcService = mock(SDCCatalogService.class);
        ResponseBody result= new ResponseBody() {
            @Nullable
            @Override
            public MediaType contentType() {
                return MediaType.parse("application/json; charset=utf-8");
            }

            @Override
            public long contentLength() {
                return 0;
            }

            @NotNull
            @Override
            public BufferedSource source() {

                return new Buffer();
            }
        };
        DefaultServiceTemplateService dsts  = new DefaultServiceTemplateService(sdcService,null);
        when(sdcService.downloadCsar(anyString())).thenReturn(successfulCall(result));
        dsts.downloadFile("toscaModelPath", "toPath");
    }
}