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.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<Integer, String> httpResponseOk;
- Pair<Integer, String> httpResponseErr0;
- Pair<Integer, String> httpResponseErr1;
- Pair<Integer, String> 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<Integer, String> httpResponseErr0;
+ private Pair<Integer, String> httpResponseErr1;
+ private Pair<Integer, String> httpTenantResponseOk;
+ private Pair<Integer, String> httpCqResponseOk;
/**
* 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<>();
- expectedHeaders.put("X-FromAppId", "POLICY");
- expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
- expectedHeaders.put("Accept", "application/json");
-
- AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse();
- httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse));
+ String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse();
+ String tenantResponse = this.getTenantQueryResponse();
+ httpCqResponseOk = restManagerMock.new Pair<>(200, aaiCqResponse);
+ httpTenantResponseOk = restManagerMock.new Pair<>(200, tenantResponse);
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<Integer, String> pnfHttpResponse = restManagerMock.new Pair<>(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<String, String> pnfParams = aaiManager.getPnf(SOME_URL, DOROTHY, "Gale", UUID.randomUUID(), pnfName);
+ assertNotNull(pnfParams);
+ assertEquals(pnfName, pnfParams.get("pnf.pnf-name"));
}
}