diff options
author | pramod.jamkhedkar <pramod@research.att.com> | 2019-04-09 14:40:16 -0400 |
---|---|---|
committer | pramod.jamkhedkar <pramod@research.att.com> | 2019-04-10 17:02:16 -0400 |
commit | 38c89e3a57f168603faaf2ad4220105b2d54a9c8 (patch) | |
tree | 1f9568e77e9999663c89b7ee88655d0eb386a755 /models-interactions/model-impl/aai/src/test/java | |
parent | e678e5af567040022f23ed7a1ba1723b82434418 (diff) |
Custom Query Code
Changes to aai, so, vfc and restmanager to support aai custom queries.
updated to latest version of aai schema. Made changes according to
latest updates.
Issue-ID: POLICY-1278
Change-Id: I255cef17fff4fe7d4ea21344c0e5ccaac52cbe9a
Signed-off-by: pramod.jamkhedkar <pramod@research.att.com>
Diffstat (limited to 'models-interactions/model-impl/aai/src/test/java')
2 files changed, 337 insertions, 2 deletions
diff --git a/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiCqResponseTest.java b/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiCqResponseTest.java new file mode 100644 index 000000000..bfa0a5a4c --- /dev/null +++ b/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiCqResponseTest.java @@ -0,0 +1,278 @@ +/*- + * ============LICENSE_START======================================================= + * + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.aai; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.File; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.onap.aai.domain.yang.CloudRegion; +import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.aai.domain.yang.Tenant; +import org.onap.aai.domain.yang.VfModule; +import org.onap.aai.domain.yang.Vserver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AaiCqResponseTest { + private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponseTest.class); + private static final String CQ_RESPONSE_SAMPLE = "src/test/resources/org/onap/policy/aai/AaiCqResponse.json"; + + + @Test + public void testConstructor() throws Exception { + /* + * Read JSON String and add all AaiObjects + */ + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + assertNotNull(aaiCqResponse); + assertNotNull(aaiCqResponse.getInventoryResponseItems()); + } + + @Test + public void testAaiMalformedCqResponse() throws Exception { + /* + * Read JSON String and add all AaiObjects + */ + + String responseString = ""; + responseString = new String(Files + .readAllBytes(new File("src/test/resources/org/onap/policy/aai/AaiMalformedCqResponse.json").toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + for (Object aaiObj : aaiCqResponse.getInventoryResponseItems()) { + assertNull(aaiObj); + } + + + } + + @Test + public void testGetItemByList() throws Exception { + /* + * Read JSON String and add all AaiObjects + */ + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + ArrayList<Vserver> vs = (ArrayList<Vserver>) aaiCqResponse.getItemListByType(Vserver.class); + assertNotNull(vs); + assertEquals("e7f1db09-ff78-44fc-b256-69095c5556fb", vs.get(0).getVserverId()); + LOGGER.info(vs.get(0).getVserverId()); + + } + + @Test + public void testGetServiceInstance() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + ServiceInstance si = aaiCqResponse.getServiceInstance(); + assertNotNull(si); + assertEquals("vLoadBalancerMS-0211-1", si.getServiceInstanceName()); + LOGGER.info(si.getServiceInstanceName()); + } + + @Test + public void testGetDefaultCloudRegion() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + CloudRegion cloudRegion = aaiCqResponse.getDefaultCloudRegion(); + assertNotNull(cloudRegion); + assertEquals("cr-16197-01-as988q", cloudRegion.getCloudRegionId()); + LOGGER.info(cloudRegion.getCloudRegionId()); + } + + @Test + public void testGetDefaultTenant() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + Tenant tenant = aaiCqResponse.getDefaultTenant(); + assertNotNull(tenant); + assertEquals("tenant1-16197-as988q", tenant.getTenantId()); + LOGGER.info(tenant.getTenantId()); + } + + + + @Test + public void testGetGenericVnfs() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + List<GenericVnf> genericVnfList = aaiCqResponse.getGenericVnfs(); + assertNotNull(genericVnfList); + for (GenericVnf genVnf : genericVnfList) { + LOGGER.info(genVnf.getVnfName()); + } + + } + + + + @Test + public void testGetDefaultGenericVnf() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + GenericVnf genVnf = aaiCqResponse.getDefaultGenericVnf(); + assertNotNull(genVnf); + assertEquals("TestVM-Vnf-0201-1", genVnf.getVnfName()); + LOGGER.info(genVnf.getVnfName()); + + } + + @Test + public void testGetGenericVnfByName() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + GenericVnf genVnf = aaiCqResponse.getGenericVnfByVnfName("TestVM-Vnf-0201-1"); + assertNotNull(genVnf); + assertEquals("17044ef4-e7f3-46a1-af03-e2aa562f23ac", genVnf.getVnfId()); + LOGGER.info(genVnf.getVnfId()); + } + + + @Test + public void testGetGenericVnfByModelInvariantId() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + GenericVnf genVnf = aaiCqResponse.getGenericVnfByModelInvariantId("724ab1cf-6120-49e8-b909-849963bed1d6"); + assertNotNull(genVnf); + assertEquals("724ab1cf-6120-49e8-b909-849963bed1d6", genVnf.getModelInvariantId()); + LOGGER.info(genVnf.getModelInvariantId()); + } + + @Test + public void testGetAllVfModules() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + List<VfModule> vfModuleList = aaiCqResponse.getAllVfModules(); + assertNotNull(vfModuleList); + for (VfModule vfMod : vfModuleList) { + LOGGER.info(vfMod.getVfModuleName()); + } + + } + + + @Test + public void testGetVfModuleByVfModuleName() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + VfModule vfModule = aaiCqResponse.getVfModuleByVfModuleName("vLoadBalancerMS-0211-1"); + assertNotNull(vfModule); + assertEquals("vLoadBalancerMS-0211-1", vfModule.getVfModuleName()); + LOGGER.info(vfModule.getVfModuleName()); + + + } + + @Test + public void testGetDefaultVfModule() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + VfModule vfModule = aaiCqResponse.getDefaultVfModule(); + assertNotNull(vfModule); + assertEquals("TestVM-0201-2", vfModule.getVfModuleName()); + LOGGER.info(vfModule.getVfModuleName()); + } + + @Test + public void testGetVserver() throws Exception { + + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + + AaiCqResponse aaiCqResponse; + aaiCqResponse = new AaiCqResponse(responseString); + Vserver vserver = aaiCqResponse.getVserver(); + assertNotNull(vserver); + assertEquals("vfw-vm-0201-2", vserver.getVserverName()); + LOGGER.info(vserver.getVserverName()); + + } + + /** + * Aai Cq sample response. + * + * @return String return response + * @throws Exception file read exception + */ + public String getAaiCqResponse() throws Exception { + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath())); + return responseString; + } + +} diff --git a/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java b/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java index 111042f6d..c453d89a1 100644 --- a/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java +++ b/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java @@ -31,10 +31,12 @@ import static org.mockito.ArgumentMatchers.startsWith; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import java.util.UUID; - import org.junit.Before; import org.junit.Test; import org.onap.policy.aai.util.Serialization; @@ -48,12 +50,18 @@ public class AaiManagerTest { Pair<Integer, String> httpResponseErr0; Pair<Integer, String> httpResponseErr1; Pair<Integer, String> httpResponseWait; + Pair<Integer, String> httpTenantResponseOk; + Pair<Integer, String> httpCqResponseOk; + + private static final String TENANT_RESPONSE_SAMPLE = + "src/test/resources/org/onap/policy/aai/AaiTenantResponse.json"; /** * Set up test cases. + * @throws Exception if error occurs */ @Before - public void beforeTestAaiManager() { + public void beforeTestAaiManager() throws Exception { restManagerMock = mock(RestManager.class); Map<String, String> expectedHeaders = new HashMap<>(); @@ -61,6 +69,10 @@ public class AaiManagerTest { expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString()); expectedHeaders.put("Accept", "application/json"); + String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse(); + String tenantResponse = this.getTenantQueryResponse(); + httpCqResponseOk = restManagerMock.new Pair<>(200, aaiCqResponse); + httpTenantResponseOk = restManagerMock.new Pair<>(200, tenantResponse); AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse(); httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse)); httpResponseErr0 = restManagerMock.new Pair<>(200, null); @@ -69,6 +81,51 @@ public class AaiManagerTest { } @Test + public void testAaiCqResponse() { + AaiManager aaiManager = new AaiManager(restManagerMock); + assertNotNull(aaiManager); + + UUID vserverNameRequestId = UUID.randomUUID(); + + when(restManagerMock.put(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap(), anyString(), + anyString())).thenReturn(httpCqResponseOk); + + when(restManagerMock.get(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap())) + .thenReturn(httpTenantResponseOk); + + AaiCqResponse aaiCqResponse = + aaiManager.getCustomQueryResponse("http://testing.cq.query", "Foo", "Bar", vserverNameRequestId, "Foo"); + assertNotNull(aaiCqResponse); + + when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString())) + .thenReturn(httpResponseErr0); + + when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap())) + .thenReturn(httpResponseErr0); + + AaiCqResponse aaiCqResponseNull = + aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo"); + assertNull(aaiCqResponseNull); + + + when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString())) + .thenReturn(httpResponseErr1); + + when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap())) + .thenReturn(httpResponseErr1); + + AaiCqResponse aaiCqResponseErr = + aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo"); + assertNull(aaiCqResponseErr); + } + + private String getTenantQueryResponse() throws IOException { + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath())); + return responseString; + } + + @Test public void testAaiManagerAaiNqRequest() { AaiManager aaiManager = new AaiManager(restManagerMock); |