Convert project from AJSC to Spring Boot
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / catalog / VnfCatalogArtifactHandlerTest.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.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Properties;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mockito;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.entity.Artifact;
41 import org.onap.aai.modelloader.restclient.AaiRestClient;
42 import org.onap.aai.restclient.client.OperationResult;
43
44 public class VnfCatalogArtifactHandlerTest {
45
46     protected static String CONFIG_FILE = "model-loader.properties";
47
48     @Test
49     public void testWithMocks() throws Exception {
50
51         Properties configProperties = new Properties();
52         try {
53             configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
54         } catch (IOException e) {
55             fail();
56         }
57         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
58         config.setModelVersion("11");
59
60         AaiRestClient mockRestClient = mock(AaiRestClient.class);
61
62         // GET operation
63         OperationResult mockGetResp = mock(OperationResult.class);
64
65         // @formatter:off
66         when(mockGetResp.getResultCode())
67                 .thenReturn(Response.Status.OK.getStatusCode())
68                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
69                 .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
70                 .thenReturn(Response.Status.OK.getStatusCode());
71         // @formatter:on
72         when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
73                 .thenReturn(mockGetResp);
74
75         // PUT operation
76         OperationResult mockPutResp = mock(OperationResult.class);
77
78         when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
79         when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
80                 Mockito.any(MediaType.class))).thenReturn(mockPutResp);
81
82         // Example VNF Catalog with
83         VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config);
84         String examplePath = "src/test/resources/imagedataexample.json";
85         byte[] encoded = Files.readAllBytes(Paths.get(examplePath));
86         List<Artifact> artifacts = new ArrayList<Artifact>();
87         artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
88
89         assertTrue(vnfCAH.pushArtifacts(artifacts, "test", new ArrayList<Artifact>(), mockRestClient));
90
91         // Only two of the VNF images should be pushed
92         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
93         AaiRestClient r = Mockito.verify(mockRestClient, Mockito.times(2));
94         r.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class));
95         assertTrue(argument.getAllValues().get(0).contains("3.16.9"));
96         assertTrue(argument.getAllValues().get(0).contains("VM00"));
97         assertTrue(argument.getAllValues().get(1).contains("3.16.1"));
98         assertTrue(argument.getAllValues().get(1).contains("VM01"));
99     }
100 }