90b3b5fa51b1f1a04bfa35c794df60a727537aaf
[ccsdk/sli/adaptors.git] / aai-service / provider / src / test / java / org / onap / ccsdk / sli / adaptors / aai / GenericVnfTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights
6  *                         reserved.
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
22 package org.onap.ccsdk.sli.adaptors.aai;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.URL;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Properties;
39 import java.util.UUID;
40
41 import org.apache.commons.lang.StringUtils;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.FixMethodOrder;
45 import org.junit.Test;
46 import org.junit.runners.MethodSorters;
47 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
48 import org.onap.ccsdk.sli.adaptors.aai.AAIRequest;
49 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
51 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
52 import org.onap.aai.inventory.v14.GenericVnf;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 public class GenericVnfTest {
60
61     private static final Logger LOG = LoggerFactory.getLogger(GenericVnfTest.class);
62
63     protected static AAIClient client;
64
65     @BeforeClass
66     public static void setUp() throws Exception {
67         Properties properties = new Properties();
68         properties.setProperty("org.onap.ccsdk.sli.adaptors.aai.host.certificate.ignore", "true");
69         properties.setProperty("org.onap.ccsdk.sli.adaptors.aai.client.name", "SDNC");
70         properties.setProperty("org.onap.ccsdk.sli.adaptors.aai.client.psswd", "SDNC");
71         properties.setProperty("org.onap.ccsdk.sli.adaptors.aai.application", "CCSDK");
72         properties.setProperty("org.onap.ccsdk.sli.adaptors.aai.uri", "http://localhost:8181");
73         properties.setProperty("connection.timeout", "60000");
74         properties.setProperty("read.timeout", "60000");
75         client = new AAIService(properties);
76         ((AAIService)client).setExecutor(new TestExecutor());
77         LOG.info("\nTaicAAIResourceTest.setUp\n");
78     }
79
80     @AfterClass
81     public static void tearDown() throws Exception {
82         client = null;
83         LOG.info("----------------------- GenericVnfTest.tearDown -----------------------");
84     }
85
86
87     @Test
88     public void test01SaveGenericVnf() {
89         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
90         try
91         {
92             SvcLogicContext ctx = new SvcLogicContext();
93
94             String uuid = UUID.randomUUID().toString();
95             Map<String, String> data = new HashMap<String, String>();
96             data.put("vnf-id", uuid);
97             data.put("vnf-name"    , "Demo-vmtn5scpx01");
98             data.put("vnf-type", "asc_heat-int");
99             data.put("service-id", "SDN-MOBILITY");
100             data.put("equipment-role", "vSCP");
101             data.put("orchestration-status", "active");
102             data.put("heat-stack-id", "Devmtn5scpx04/" + data.get("vnf-id"));
103             data.put("in-maint", "false");
104             data.put("is-closed-loop-disabled", "false");
105             data.put("encrypted-access-flag","true");
106
107             QueryStatus resp = client.save("generic-vnf", false, false, "generic-vnf.vnf-id = '"+uuid+"'", data, "aaidata", ctx);
108
109         }
110         catch (Throwable e)
111         {
112
113         }
114     }
115
116     @Test
117     public void test02QueryGenericVnf()
118     {
119         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
120
121         try
122         {
123             List<String> keys = new ArrayList<String>();
124             keys.add("vnf-id = 'ec14a84d-7b43-45ad-bb04-c12b74083648'");
125             keys.add("depth = 'all'");
126
127             SvcLogicContext ctx = new SvcLogicContext();
128             QueryStatus response = client.query("generic-vnf", false, null, StringUtils.join(keys, " AND "), "aaiTest", null, ctx);
129
130             assertTrue(response == QueryStatus.SUCCESS);
131             LOG.info("AAIResponse: " + response.toString());
132         }
133         catch (Exception e)
134         {
135             LOG.error("Caught exception", e);
136             fail("Caught exception");
137         }
138     }
139
140     @Test
141     public void test03UpdateGenericVnf() {
142         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
143         try
144         {
145             SvcLogicContext ctx = new SvcLogicContext();
146
147             String uuid = UUID.randomUUID().toString();
148             Map<String, String> data = new HashMap<String, String>();
149             data.put("service-id", "SDN-MOBILITY");
150             data.put("equipment-role", "vSCP");
151             data.put("orchestration-status", "active");
152             data.put("heat-stack-id", "Devmtn5scpx04/" + data.get("vnf-id"));
153             data.put("in-maint", "false");
154             data.put("is-closed-loop-disabled", "false");
155             data.put("encrypted-access-flag","true");
156
157             QueryStatus resp = client.update("generic-vnf", "generic-vnf.vnf-id = '"+uuid+"'", data, "aaidata", ctx);
158
159         }
160         catch (Throwable e)
161         {
162
163         }
164     }
165
166     @Test
167     public void test04DeleteGenericVnf()
168     {
169         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
170
171         try
172         {
173             SvcLogicContext ctx = new SvcLogicContext();
174
175             List<String> keys = new ArrayList<String>();
176             keys.add("generic-vnf.vnf-id = 'VNF-S7'");
177
178             QueryStatus response = client.delete("generic-vnf", StringUtils.join(keys, " AND "), ctx);
179
180             assertTrue(response == QueryStatus.SUCCESS);
181             LOG.info("AAIResponse: " + response.toString());
182         }
183         catch (Exception e)
184         {
185             LOG.error("Caught exception", e);
186             fail("Caught exception");
187         }
188     }
189
190     @Test
191     public void test05GetResource()
192     {
193         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
194
195         try
196         {
197             SvcLogicContext ctx = new SvcLogicContext();
198             GenericVnf response = ((AAIService)client).getResource("/network/generic-vnfs/generic-vnf/ec14a84d-7b43-45ad-bb04-c12b74083648", GenericVnf.class);
199
200             assertNotNull(response);
201         }
202         catch (Exception e)
203         {
204
205         }
206     }
207
208     static class TestExecutor implements AAIExecutorInterface {
209         private String data = "{\"vnf-id\":\"7324200933\",\"vnf-name\":\"vnfinst1m001\",\"vnf-type\":\"TestVnf\",\"service-id\":\"9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb\",\"equipment-role\":\"vTEST\",\"orchestration-status\":\"active\",\"in-maint\":false,\"is-closed-loop-disabled\":false,\"resource-version\":\"1520720941585\"}";
210
211         @Override
212         public String get(AAIRequest request) throws AAIServiceException {
213             return data;
214         }
215
216         @Override
217         public String post(AAIRequest request) throws AAIServiceException {
218             return "success";
219         }
220
221         @Override
222         public Boolean delete(AAIRequest request, String resourceVersion) throws AAIServiceException {
223             return Boolean.TRUE;
224         }
225
226         @Override
227         public Object query(AAIRequest request, Class clas) throws AAIServiceException {
228             ObjectMapper mapper = AAIService.getObjectMapper();
229             try {
230                 return mapper.readValue(data, GenericVnf.class);
231             } catch (IOException e) {
232                 return new GenericVnf();
233             }
234         }
235
236         @Override
237         public Boolean patch(AAIRequest request, String resourceVersion) throws AAIServiceException {
238             return Boolean.TRUE;
239         }
240
241     }
242 }