Updating AAI service adaptor to use version 21 (v21) of the schema model.
[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.v21.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             assertNotNull(ctx);
109
110         }
111         catch (Throwable e)
112         {
113
114         }
115         ;
116     }
117
118     @Test
119     public void test02QueryGenericVnf()
120     {
121         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
122
123         try
124         {
125             List<String> keys = new ArrayList<String>();
126             keys.add("vnf-id = 'ec14a84d-7b43-45ad-bb04-c12b74083648'");
127             keys.add("depth = 'all'");
128
129             SvcLogicContext ctx = new SvcLogicContext();
130             QueryStatus response = client.query("generic-vnf", false, null, StringUtils.join(keys, " AND "), "aaiTest", null, ctx);
131
132             assertTrue(response == QueryStatus.SUCCESS);
133             LOG.info("AAIResponse: " + response.toString());
134         }
135         catch (Exception e) {
136             LOG.error("Caught exception", e);
137             fail("Caught exception");
138         }
139     }
140
141     @Test
142     public void test03UpdateGenericVnf() {
143         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
144         try
145         {
146             SvcLogicContext ctx = new SvcLogicContext();
147
148             String uuid = UUID.randomUUID().toString();
149             Map<String, String> data = new HashMap<String, String>();
150             data.put("service-id", "SDN-MOBILITY");
151             data.put("equipment-role", "vSCP");
152             data.put("orchestration-status", "active");
153             data.put("heat-stack-id", "Devmtn5scpx04/" + data.get("vnf-id"));
154             data.put("in-maint", "false");
155             data.put("is-closed-loop-disabled", "false");
156             data.put("encrypted-access-flag","true");
157
158             QueryStatus resp = client.update("generic-vnf", "generic-vnf.vnf-id = '"+uuid+"'", data, "aaidata", ctx);
159             assertNotNull(uuid);
160         }
161         catch (Throwable e)
162         {
163
164         }
165     }
166
167     @Test
168     public void test04DeleteGenericVnf()
169     {
170         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
171
172         try
173         {
174             SvcLogicContext ctx = new SvcLogicContext();
175
176             List<String> keys = new ArrayList<String>();
177             keys.add("generic-vnf.vnf-id = 'VNF-S7'");
178
179             QueryStatus response = client.delete("generic-vnf", StringUtils.join(keys, " AND "), ctx);
180
181             assertTrue(response == QueryStatus.SUCCESS);
182             LOG.info("AAIResponse: " + response.toString());
183         }
184         catch (Exception e)
185         {
186             LOG.error("Caught exception", e);
187             fail("Caught exception");
188         }
189     }
190
191     @Test
192     public void test05GetResource()
193     {
194         LOG.info("----------------------- Test: " + new Object(){}.getClass().getEnclosingMethod().getName() + " -----------------------");
195
196         try
197         {
198             SvcLogicContext ctx = new SvcLogicContext();
199             GenericVnf response = ((AAIService)client).getResource("/network/generic-vnfs/generic-vnf/ec14a84d-7b43-45ad-bb04-c12b74083648", GenericVnf.class);
200
201             assertNotNull(response);
202         }
203         catch (Exception e)
204         {
205
206         }
207     }
208
209     static class TestExecutor implements AAIExecutorInterface {
210         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\"}";
211
212         @Override
213         public String get(AAIRequest request) throws AAIServiceException {
214             return data;
215         }
216
217         @Override
218         public String post(AAIRequest request) throws AAIServiceException {
219             return "success";
220         }
221
222         @Override
223         public Boolean delete(AAIRequest request, String resourceVersion) throws AAIServiceException {
224             return Boolean.TRUE;
225         }
226
227         @Override
228         public Object query(AAIRequest request, Class clas) throws AAIServiceException {
229             ObjectMapper mapper = AAIService.getObjectMapper();
230             try {
231                 return mapper.readValue(data, GenericVnf.class);
232             } catch (IOException e) {
233                 return new GenericVnf();
234             }
235         }
236
237         @Override
238         public Boolean patch(AAIRequest request, String resourceVersion) throws AAIServiceException {
239             return Boolean.TRUE;
240         }
241
242     }
243 }