X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-impl%2Faai%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Faai%2FAaiManagerTest.java;h=33934d937d5e3ddeab648d12e6cdb2df7833a88b;hb=21607022333b8d3917754d071d5e4c259308edef;hp=111042f6d70d1204c597a832f35d48c46efd951c;hpb=e47ec5069a6304a028c4144e8abaa58d6462b8e2;p=policy%2Fmodels.git 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..33934d937 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 @@ -3,7 +3,7 @@ * aai * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,150 +21,119 @@ package org.onap.policy.aai; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.contains; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.startsWith; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.HashMap; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.Map; import java.util.UUID; - +import org.apache.commons.lang3.tuple.Pair; import org.junit.Before; import org.junit.Test; -import org.onap.policy.aai.util.Serialization; import org.onap.policy.rest.RestManager; -import org.onap.policy.rest.RestManager.Pair; public class AaiManagerTest { - RestManager restManagerMock; - UUID aaiNqRequestUuid = UUID.randomUUID(); - Pair httpResponseOk; - Pair httpResponseErr0; - Pair httpResponseErr1; - Pair httpResponseWait; + private static final String CQ_QUERY_URL = "http://testing.cq.query"; + private static final String DOROTHY = "Dorothy"; + private static final String SOME_URL = "http://somewhere.over.the.rainbow"; + private static final String TENANT_RESPONSE_SAMPLE = + "src/test/resources/org/onap/policy/aai/AaiTenantResponse.json"; + + private RestManager restManagerMock; + private Pair httpResponseErr0; + private Pair httpResponseErr1; + private Pair httpTenantResponseOk; + private Pair httpCqResponseOk; /** * Set up test cases. + * + * @throws Exception if error occurs */ @Before - public void beforeTestAaiManager() { + public void beforeTestAaiManager() throws Exception { restManagerMock = mock(RestManager.class); - Map expectedHeaders = new HashMap<>(); - expectedHeaders.put("X-FromAppId", "POLICY"); - expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString()); - expectedHeaders.put("Accept", "application/json"); + String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse(); + String tenantResponse = this.getTenantQueryResponse(); + httpCqResponseOk = Pair.of(200, aaiCqResponse); + httpTenantResponseOk = Pair.of(200, tenantResponse); + httpResponseErr0 = Pair.of(200, null); + httpResponseErr1 = Pair.of(200, "{"); - AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse(); - httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse)); - httpResponseErr0 = restManagerMock.new Pair<>(200, null); - httpResponseErr1 = restManagerMock.new Pair<>(200, "{"); - httpResponseWait = restManagerMock.new Pair<>(503, null); } - @Test - public void testAaiManagerAaiNqRequest() { + @Test + public void testAaiCqResponse() { AaiManager aaiManager = new AaiManager(restManagerMock); assertNotNull(aaiManager); - UUID aaiNqUuid = UUID.randomUUID(); + UUID vserverNameRequestId = UUID.randomUUID(); - AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters(); - AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery(); - aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid); - aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery); + when(restManagerMock.put(startsWith(CQ_QUERY_URL), eq("Foo"), eq("Bar"), anyMap(), anyString(), + anyString())).thenReturn(httpCqResponseOk); - AaiNqRequest aaiNqRequest = new AaiNqRequest(); - aaiNqRequest.setQueryParameters(aaiNqQueryParameters); + when(restManagerMock.get(startsWith(CQ_QUERY_URL), eq("Foo"), eq("Bar"), anyMap())) + .thenReturn(httpTenantResponseOk); - when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(), - anyString(), anyString())).thenReturn(httpResponseOk); + AaiCqResponse aaiCqResponse = + aaiManager.getCustomQueryResponse(CQ_QUERY_URL, "Foo", "Bar", vserverNameRequestId, "Foo"); + assertNotNull(aaiCqResponse); - AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale", - aaiNqRequest, aaiNqRequestUuid); - assertNotNull(aaiNqOkResponse); + when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString())) + .thenReturn(httpResponseErr0); - when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString())) - .thenReturn(null); + when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap())) + .thenReturn(httpResponseErr0); - AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid); - assertNull(aaiNqNullResponse); + AaiCqResponse aaiCqResponseNull = + aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo"); + assertNull(aaiCqResponseNull); - when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(), - anyString(), anyString())).thenReturn(httpResponseErr0); - AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West", - aaiNqRequest, aaiNqRequestUuid); - assertNull(aaiNqNotOkResponse0); + when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString())) + .thenReturn(httpResponseErr1); - when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(), - anyString(), anyString())).thenReturn(httpResponseErr1); + when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap())) + .thenReturn(httpResponseErr1); - AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West", - aaiNqRequest, aaiNqRequestUuid); - assertNull(aaiNqNotOkResponse1); + AaiCqResponse aaiCqResponseErr = + aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo"); + assertNull(aaiCqResponseErr); } - @Test - public void testAaiManagerQueryByVserverName() { - AaiManager aaiManager = new AaiManager(restManagerMock); - assertNotNull(aaiManager); - - UUID vserverNameRequestId = UUID.randomUUID(); - - when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap())) - .thenReturn(httpResponseOk); - - AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow", - "Dorothy", "Gale", vserverNameRequestId, "vserverName"); - assertNotNull(vserverResponse); - - AaiGetVserverResponse vserverNullResponse = - aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName"); - assertNull(vserverNullResponse); - - when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap())) - .thenReturn(httpResponseErr0); - - AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName( - "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName"); - assertNull(vserverNotOkResponse0); + private String getTenantQueryResponse() throws IOException { + String responseString = ""; + responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath())); + return responseString; } - @Test - public void testAaiManagerQueryByVnfId() { - AaiManager aaiManager = new AaiManager(restManagerMock); - assertNotNull(aaiManager); - - UUID vserverNameRequestId = UUID.randomUUID(); - - when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap())) - .thenReturn(httpResponseOk); - - AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy", - "Gale", vserverNameRequestId, "vnfID"); - assertNotNull(vnfResponse); - } @Test - public void testAaiManagerQueryByVnfName() { + public void testAaiManagerGetPnf() { AaiManager aaiManager = new AaiManager(restManagerMock); assertNotNull(aaiManager); + String pnfName = "test-pnf"; + String pnfResponse = "{\"pnf-name\":" + pnfName + + ",\"pnf-id\":\"123456\",\"in-maint\":false,\"ipaddress-v4-oam\":\"1.1.1.1\"}"; - UUID vserverNameRequestId = UUID.randomUUID(); - - when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap())) - .thenReturn(httpResponseOk); + Pair pnfHttpResponse = Pair.of(200, pnfResponse); + when(restManagerMock.get(contains(pnfName), eq(DOROTHY), eq("Gale"), anyMap())) + .thenReturn(pnfHttpResponse); - AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy", - "Gale", vserverNameRequestId, "vnfName"); - assertNotNull(vnfResponse); + Map pnfParams = aaiManager.getPnf(SOME_URL, DOROTHY, "Gale", UUID.randomUUID(), pnfName); + assertNotNull(pnfParams); + assertEquals(pnfName, pnfParams.get("pnf.pnf-name")); } }