Revisions made to the Model Loader to use Babel
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / catalog / VnfCatalogArtifactHandler_noMock_Test.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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============================================
20  */
21 package org.onap.aai.modelloader.entity.catalog;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
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;
44
45
46 /**
47  * No-Mock tests
48  * 
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.
51  * 
52  * @author andrewdo
53  *
54  */
55
56 @PrepareForTest({VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class})
57 public class VnfCatalogArtifactHandler_noMock_Test {
58
59     protected static String CONFIG_FILE = "model-loader.properties";
60
61
62     @Test
63     public void testWithOutMocks() throws Exception {
64
65         Properties configProperties = new Properties();
66         try {
67             configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
68         } catch (IOException e) {
69             fail();
70         }
71
72         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
73         config.setModelVersion("11");
74
75         AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class);
76         PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient);
77
78         // GET operation
79         OperationResult mockGetResp = PowerMockito.mock(OperationResult.class);
80
81         // @formatter:off
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());
87         // @formatter:on
88         PowerMockito.when(
89                 mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
90                 .thenReturn(mockGetResp);
91
92         // PUT operation
93         OperationResult mockPutResp = PowerMockito.mock(OperationResult.class);
94
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);
98
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")));
105
106         assertTrue(vnfCAH.pushArtifacts(artifacts, "test", new ArrayList<Artifact>(), mockRestClient));
107
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"));
116     }
117
118 }