Refactor for Sonar smells and code coverage
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / catalog / TestVnfCatalogArtifactHandler.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 European Software Marketing Ltd.
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.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Properties;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mockito;
42 import org.onap.aai.modelloader.config.ModelLoaderConfig;
43 import org.onap.aai.modelloader.entity.Artifact;
44 import org.onap.aai.modelloader.restclient.AaiRestClient;
45 import org.onap.aai.restclient.client.OperationResult;
46
47 public class TestVnfCatalogArtifactHandler {
48
49     protected static String CONFIG_FILE = "model-loader.properties";
50
51     private AaiRestClient mockRestClient = mock(AaiRestClient.class);
52
53     /**
54      * Update A&AI with 4 images, 2 of which already exist.
55      * 
56      * @throws Exception
57      */
58     @Test
59     public void testUpdateVnfImages() throws Exception {
60         // GET operation
61         OperationResult mockGetResp = mock(OperationResult.class);
62
63         // @formatter:off
64         when(mockGetResp.getResultCode())
65                 .thenReturn(Response.Status.OK.getStatusCode())
66                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
67                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
68                 .thenReturn(Response.Status.OK.getStatusCode());
69         // @formatter:on
70
71         when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
72                 .thenReturn(mockGetResp);
73         mockPutOperations();
74
75         // Example VNF Catalog XML
76         VnfCatalogArtifactHandler handler = new VnfCatalogArtifactHandler(createConfig());
77         assertThat(handler.pushArtifacts(createVnfCatalogArtifact(), "test", new ArrayList<Artifact>(), mockRestClient),
78                 is(true));
79
80         assertPutOperationsSucceeded();
81     }
82
83     private ModelLoaderConfig createConfig() {
84         Properties configProperties = new Properties();
85         try {
86             configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
87         } catch (IOException e) {
88             fail();
89         }
90         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
91         return config;
92     }
93
94     /**
95      * Example VNF Catalog based on JSON data (returned by Babel)
96      * 
97      * @return test Artifacts
98      * @throws IOException
99      * @throws UnsupportedEncodingException
100      */
101     private List<Artifact> createVnfCatalogArtifact() throws IOException, UnsupportedEncodingException {
102         String examplePath = "src/test/resources/imagedataexample.json";
103         byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
104         List<Artifact> artifacts = new ArrayList<Artifact>();
105         artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
106         return artifacts;
107     }
108
109     /**
110      * Always return CREATED (success) for a PUT operation.
111      */
112     private void mockPutOperations() {
113         OperationResult mockPutResp = mock(OperationResult.class);
114         when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
115         when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
116                 Mockito.any(MediaType.class))).thenReturn(mockPutResp);
117     }
118
119     private void assertPutOperationsSucceeded() {
120         // Only two of the VNF images should be pushed
121         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
122         AaiRestClient mockedClient = Mockito.verify(mockRestClient, Mockito.times(2));
123         mockedClient.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(),
124                 Mockito.any(MediaType.class));
125         assertThat(argument.getAllValues().get(0), containsString("3.16.9"));
126         assertThat(argument.getAllValues().get(0), containsString("VM00"));
127         assertThat(argument.getAllValues().get(1), containsString("3.16.1"));
128         assertThat(argument.getAllValues().get(1), containsString("VM01"));
129     }
130 }