2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
21 package org.onap.aai.modelloader.entity.catalog;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import com.sun.jersey.api.client.ClientResponse;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mockito;
38 import org.onap.aai.modelloader.config.ModelLoaderConfig;
39 import org.onap.aai.modelloader.entity.Artifact;
40 import org.onap.aai.modelloader.restclient.AaiRestClient;
41 import org.onap.aai.restclient.client.OperationResult;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
49 * Because Jacoco (and other coverage tools) can't cope with mocked classes under some circumstances, coverage is/was
50 * falsely reported as < 50%. Hence these duplicated but non-mock tests to address this, for ONAP reasons.
56 @PrepareForTest({VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class})
57 public class VnfCatalogArtifactHandler_noMock_Test {
59 protected static String CONFIG_FILE = "model-loader.properties";
63 public void testWithOutMocks() throws Exception {
65 Properties configProperties = new Properties();
67 configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
68 } catch (IOException e) {
72 ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
73 config.setModelVersion("11");
75 AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class);
76 PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient);
79 OperationResult mockGetResp = PowerMockito.mock(OperationResult.class);
82 PowerMockito.when(mockGetResp.getResultCode())
83 .thenReturn(Response.Status.OK.getStatusCode())
84 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
85 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
86 .thenReturn(Response.Status.OK.getStatusCode());
89 mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
90 .thenReturn(mockGetResp);
93 OperationResult mockPutResp = PowerMockito.mock(OperationResult.class);
95 PowerMockito.when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
96 PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
97 Mockito.any(MediaType.class))).thenReturn(mockPutResp);
99 // Example VNF Catalog with
100 VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config);
101 String examplePath = "src/test/resources/imagedataexample.json";
102 byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
103 List<Artifact> artifacts = new ArrayList<Artifact>();
104 artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
106 assertTrue(vnfCAH.pushArtifacts(artifacts, "test", new ArrayList<Artifact>(), mockRestClient));
108 // Only two of the VNF images should be pushed
109 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
110 AaiRestClient r = Mockito.verify(mockRestClient, Mockito.times(2));
111 r.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class));
112 assertTrue(argument.getAllValues().get(0).contains("3.16.9"));
113 assertTrue(argument.getAllValues().get(0).contains("VM00"));
114 assertTrue(argument.getAllValues().get(1).contains("3.16.1"));
115 assertTrue(argument.getAllValues().get(1).contains("VM01"));