+ @DataProvider
+ public static Object[][] aaiClientGetCloudOwnerByCloudRegionId() {
+
+ final String cloudRegion = "{" +
+ " \"cloud-owner\": \"mure-royo-ru22\"," +
+ " \"cloud-region-id\": \"ravitu\"," +
+ " \"cloud-type\": \"openstack\"," +
+ " \"resource-version\": \"1523631256125\"," +
+ " \"relationship-list\": {" +
+ " \"relationship\": [{" +
+ " \"related-to\": \"pserver\"" +
+ " }" +
+ " ]" +
+ " }" +
+ " }";
+
+ String bodyWith0 = "{ \"cloud-region\": [" + " ]}";
+ String bodyWith1 = "{ \"cloud-region\": [" + cloudRegion + " ]}";
+ String bodyWith2 = "{ \"cloud-region\": [" + cloudRegion + ", " + cloudRegion + " ]}";
+ String bodyWithDifferent2 = "{ \"cloud-region\": [" + cloudRegion + ", " +
+ cloudRegion.replace("mure-royo-ru22", "nolay-umaxo") +
+ "]}";
+
+ return new Object[][] {
+ { "regular single result", bodyWith1, false },
+ { "exceptional empty result", bodyWith0, true },
+ { "two same results", bodyWith2, false },
+ { "two incoherent results", bodyWithDifferent2, true },
+ };
+ }
+
+ @Test(dataProvider = "aaiClientGetCloudOwnerByCloudRegionId")
+ public void getCloudOwnerByCloudRegionIdNonCached(String desc, String body, boolean expectingException) {
+ final String cloudRegion = "ravitu";
+ AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
+ final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK, body);
+ when(aaiRestInterface.doRest(anyString(), anyString(), eq(Unchecked.toURI("cloud-infrastructure/cloud-regions?cloud-region-id=" + cloudRegion)),
+ isNull(), eq(HttpMethod.GET), anyBoolean(), anyBoolean()))
+ .thenReturn(responseWithRequestInfo);
+
+ final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
+
+ try {
+ final String result = aaiClient.getCloudOwnerByCloudRegionIdNonCached(cloudRegion);
+ if (expectingException) fail("expected failure on " + desc + ", got " + result);
+ else {
+ assertThat(result, is("mure-royo-ru22"));
+ }
+ } catch (Exception e) {
+ if (!expectingException) throw e;
+ else {
+ assertThat(e.toString(), either(
+ containsString("No cloud-owner found for " + cloudRegion))
+ .or(containsString("Conflicting cloud-owner found for " + cloudRegion)));
+ }
+ }
+ }
+
+ @DataProvider
+ public static Object[][] cloudRegionAndTenantDataProvider() {
+ return new Object[][] {
+ { "APPC-24595-T-IST-02C", "mtn23b" },
+ { "APPC-24595-T-IST-02C", null },
+ { null, "mtn23b" },
+ { null, null },
+ };
+ }
+
+ @DataProvider
+ public static Object[][] versionsDataProvider() {
+ return new Object[][] {
+ { Stream.of("10","20","30"), "30" },
+ { Stream.of("10","20","20"), "20" },
+ { Stream.of("c","b","a"), "c" },
+ { Stream.of("1.0","2.0","1.8"), "2.0" },
+ { Stream.of("1.0.7","2.0.2","2.0.9"), "2.0.9" },
+ { Stream.of("0","0","0"), "0" },
+ { Stream.of("","10"), "10" },
+
+ };
+ }
+
+ @Test(dataProvider = "versionsDataProvider")
+ public void maxModelVer(Stream<String> input, String expected) {
+ Stream<ModelVer> modelVerStream = input.map(version -> {
+ ModelVer mv = new ModelVer();
+ mv.setModelVersion(version);
+ return mv;
+ });
+
+ final AaiClient aaiClient = new AaiClient(null, null, null);
+
+ assertThat(aaiClient.maxModelVer(modelVerStream), hasProperty("modelVersion", is(expected)));
+ }
+
+ @Test(expectedExceptions = GenericUncheckedException.class)
+ public void maxModelVerException() {
+ final AaiClient aaiClient = new AaiClient(null, null, null);
+ aaiClient.maxModelVer(Stream.of(new ModelVer()));
+ }
+ @Test(dataProvider = "cloudRegionAndTenantDataProvider")
+ public void getCloudRegionAndTenantByVnfId(String tenantName, String cloudRegionId) throws JsonProcessingException {
+ SimpleResult tenant = new SimpleResult();
+ if (tenantName != null) {
+ tenant.setJsonNodeType("tenant");
+ Properties tenantProps = new Properties();
+ tenantProps.setTenantName(tenantName);
+ tenant.setJsonProperties(tenantProps);
+ }
+
+ SimpleResult cloudRegion = new SimpleResult();
+ if (cloudRegionId != null) {
+ cloudRegion.setJsonNodeType("cloud-region");
+ Properties cloudRegionProps = new Properties();
+ cloudRegionProps.setCloudRegionId(cloudRegionId);
+ cloudRegion.setJsonProperties(cloudRegionProps);
+ }
+
+ CustomQuerySimpleResult customQuerySimpleResult = new CustomQuerySimpleResult(ImmutableList.of(tenant, cloudRegion));
+ String mockedBody = new ObjectMapper().writeValueAsString(customQuerySimpleResult);
+
+ AAIRestInterface aaiRestInterface = mock(AAIRestInterface.class);
+ final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK, mockedBody, "query?format=simple", HttpMethod.PUT);
+ when(aaiRestInterface.doRest(anyString(), anyString(), eq(Unchecked.toURI("query?format=simple")),
+ any(), eq(HttpMethod.PUT), anyBoolean(), anyBoolean()))
+ .thenReturn(responseWithRequestInfo);
+
+ final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
+ Map<String, Properties> result = aaiClient.getCloudRegionAndTenantByVnfId("anyVnfId");
+ if (tenantName != null) {
+ assertEquals(result.get("tenant").getTenantName(), tenantName);
+ } else {
+ assertNull(result.get("tenant"));
+ }
+
+ if (cloudRegionId != null) {
+ assertEquals(result.get("cloud-region").getCloudRegionId(), cloudRegionId);
+ } else {
+ assertNull(result.get("cloud-region"));
+ }
+ }
+
+ protected void mockForGetRequest(AAIRestInterface aaiRestInterface, ResponseWithRequestInfo responseWithRequestInfo) {
+ when(aaiRestInterface.doRest(anyString(), anyString(), any(URI.class), isNull(), eq(HttpMethod.GET) ,anyBoolean(), anyBoolean()))
+ .thenReturn(responseWithRequestInfo);
+ }
+
+ @Test
+ public void shouldProperlyReadResponseOnceWhenSubscribersAreNotPresent() {
+ AAIRestInterface restInterface = mock(AAIRestInterface.class);
+ PortDetailsTranslator portDetailsTranslator = mock(PortDetailsTranslator.class);
+ Response response = mock(Response.class);
+ when(response.getStatus()).thenReturn(404);
+ when(response.readEntity(String.class)).thenReturn("sampleEntity");
+ when(response.getStatusInfo()).thenReturn(Response.Status.NOT_FOUND);
+ ResponseWithRequestInfo responseWithRequestInfo = new ResponseWithRequestInfo(response, "test", HttpMethod.GET);
+ when(restInterface.RestGet(eq("VidAaiController"), any(String.class),
+ eq(Unchecked.toURI("business/customers?subscriber-type=INFRA&depth=0")), eq(false), eq(true))).thenReturn(responseWithRequestInfo);
+ AaiClient aaiClient = new AaiClient(restInterface, portDetailsTranslator, null);
+
+
+ aaiClient.getAllSubscribers(true);
+
+ verify(response).readEntity(String.class);
+ }
+