diff options
author | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2019-03-05 16:42:10 -0500 |
---|---|---|
committer | Marcus Williams <marcus.williams@intel.com> | 2019-03-06 15:58:57 +0000 |
commit | 416c7a449548a2fc8709cd60b27b3bad40872b69 (patch) | |
tree | af650bbf61d94dd1375f0d05f2e719ab58536a04 /common/src/test/java/org/onap | |
parent | 91426ec9f221e288ea3f54fa62030e6f17420b90 (diff) |
query clients now have more useable result methods
ResourceAndUrl now outputs the correct wrapper type
query clients now have more useable result methods
Change-Id: I000f8a7e7d4e204d1da7ad1c6bb5ce3e2d0135b2
Issue-ID: SO-1596
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
Diffstat (limited to 'common/src/test/java/org/onap')
6 files changed, 90 insertions, 14 deletions
diff --git a/common/src/test/java/org/onap/so/client/aai/AAIQueryClientTest.java b/common/src/test/java/org/onap/so/client/aai/AAIQueryClientTest.java index 84c3cad0f9..e648ea38da 100644 --- a/common/src/test/java/org/onap/so/client/aai/AAIQueryClientTest.java +++ b/common/src/test/java/org/onap/so/client/aai/AAIQueryClientTest.java @@ -20,47 +20,65 @@ package org.onap.so.client.aai; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.Map; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.GenericType; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aai.domain.yang.Complex; import org.onap.so.client.RestClient; +import org.onap.so.client.aai.entities.AAIResultWrapper; import org.onap.so.client.aai.entities.CustomQuery; +import org.onap.so.client.aai.entities.Results; import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.graphinventory.Format; import org.onap.so.client.graphinventory.GraphInventoryClient; import org.onap.so.client.graphinventory.GraphInventorySubgraphType; +import org.onap.so.client.graphinventory.entities.Pathed; +import org.onap.so.client.graphinventory.entities.ResourceAndUrl; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(MockitoJUnitRunner.class) public class AAIQueryClientTest { @Mock - Response response; - - @Mock - RestClient restClient; + private RestClient restClient; @Mock - GraphInventoryClient client; + private GraphInventoryClient client; @InjectMocks - AAIQueryClient aaiQueryClient = new AAIQueryClient(); + @Spy + private AAIQueryClient aaiQueryClient = new AAIQueryClient(); + private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/"; + + private ObjectMapper mapper = new ObjectMapper(); @Test public void testQuery() { List<AAIResourceUri> uris = Arrays.asList(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY)); @@ -91,4 +109,37 @@ public class AAIQueryClientTest { verify(aaiUri, times(1)).queryParam("nodesOnly", ""); verify(aaiUri, times(1)).queryParam("subgraph", subgraph.toString()); } + + @Test + public void querySingleResourceTest() throws IOException { + doReturn(getJson("single-query-result.json")).when(aaiQueryClient).query(eq(Format.RESOURCE_AND_URL), any(CustomQuery.class)); + List<Complex> result = aaiQueryClient.querySingleResource(new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(AAIObjectType.COMPLEX, "test"))), Complex.class); + assertEquals(2, result.size()); + assertEquals("complex-id-15100-jc689q2", result.get(1).getPhysicalLocationId()); + } + + @Test + public void getResourceAndUrlTest() throws IOException { + doReturn(getJson("single-query-result.json")).when(aaiQueryClient).query(eq(Format.RESOURCE_AND_URL), any(CustomQuery.class)); + List<ResourceAndUrl<AAIResultWrapper>> result = aaiQueryClient.getResourceAndUrl(new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(AAIObjectType.COMPLEX, "test")))); + assertEquals(2, result.size()); + + assertEquals(1, result.get(1).getWrapper().getRelationships().get().getRelatedUris(AAIObjectType.PSERVER).size()); + } + + @Test + public void querySingleTypeTest() throws IOException { + when(client.createClient(isA(AAIUri.class))).thenReturn(restClient); + when(restClient.put(any(Object.class), any(GenericType.class))).thenReturn(mapper.readValue(getJson("pathed-result.json"), new TypeReference<Results<Map<String, Object>>>(){})); + + + List<Pathed> results = aaiQueryClient.queryPathed(new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(AAIObjectType.COMPLEX, "test")))); + + assertEquals(2, results.size()); + assertEquals("service-instance", results.get(1).getResourceType()); + } + + private String getJson(String filename) throws IOException { + return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename))); + } } diff --git a/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java b/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java index 73fbff6e4f..174c76b1ef 100644 --- a/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java +++ b/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java @@ -33,9 +33,9 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; import javax.ws.rs.BadRequestException; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -50,7 +50,6 @@ import org.onap.so.client.aai.entities.AAIResultWrapper; import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl; -import org.onap.so.client.graphinventory.GraphInventoryClient; import com.github.tomakehurst.wiremock.admin.NotFoundException; import com.github.tomakehurst.wiremock.junit.WireMockRule; @@ -60,7 +59,7 @@ public class AAIResourcesClientTest { @Rule - public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443)); + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); @Rule public ExpectedException thrown = ExpectedException.none(); @@ -72,9 +71,11 @@ public class AAIResourcesClientTest { @InjectMocks public AAIResourcesClient aaiClient = new AAIResourcesClient(); + private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/"; + @Before public void beforeTest() { - doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties(); + doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties(); } @Test diff --git a/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java b/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java index 69d46de96a..a156e3c158 100644 --- a/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java +++ b/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java @@ -80,4 +80,13 @@ public class DSLQueryBuilderTest { assertEquals("cloud-region* !('cloud-owner', ' ', ' null ')", builder.build()); } + + @Test + public void shortCutToTest() { + DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER, + __.key("hostname", "my-hostname")).output()); + + builder.to(AAIObjectType.P_INTERFACE).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id")); + assertEquals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id')", builder.build()); + } } diff --git a/common/src/test/java/org/onap/so/client/aai/entities/RelationshipsTest.java b/common/src/test/java/org/onap/so/client/aai/entities/RelationshipsTest.java index 1ce6e4377c..8d11f69308 100644 --- a/common/src/test/java/org/onap/so/client/aai/entities/RelationshipsTest.java +++ b/common/src/test/java/org/onap/so/client/aai/entities/RelationshipsTest.java @@ -44,7 +44,7 @@ public class RelationshipsTest { AAIResultWrapper wrapper = new AAIResultWrapper(content); Relationships relationships = wrapper.getRelationships().get(); - List<AAIResourceUri> test = relationships.getRelatedAAIUris(AAIObjectType.VCE); + List<AAIResourceUri> test = relationships.getRelatedUris(AAIObjectType.VCE); System.out.println(test.get(0).build()); List<AAIResourceUri> uris = Arrays.asList( diff --git a/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java b/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java index 15c1c24ae2..979ca39489 100644 --- a/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java +++ b/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java @@ -69,7 +69,7 @@ public class ServiceInstanceUriTest { private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/"; @Rule - public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443)); + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); @Rule public final ExpectedException exception = ExpectedException.none(); @@ -82,7 +82,7 @@ public class ServiceInstanceUriTest { @Before public void beforeTest() { - doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties(); + doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties(); } @Test public void found() throws IOException { diff --git a/common/src/test/java/org/onap/so/client/defaultproperties/DefaultAAIPropertiesImpl.java b/common/src/test/java/org/onap/so/client/defaultproperties/DefaultAAIPropertiesImpl.java index 77e1dfee7d..65c76561dc 100644 --- a/common/src/test/java/org/onap/so/client/defaultproperties/DefaultAAIPropertiesImpl.java +++ b/common/src/test/java/org/onap/so/client/defaultproperties/DefaultAAIPropertiesImpl.java @@ -50,6 +50,21 @@ public class DefaultAAIPropertiesImpl implements AAIProperties { this.props = temp; } + + public DefaultAAIPropertiesImpl(int port) { + File initialFile = new File("src/test/resources/aai.properties"); + Map<Object, Object> temp; + try (InputStream targetStream = new FileInputStream(initialFile)) { + Properties properties = new Properties(); + properties.load(targetStream); + temp = properties; + } catch (IOException e) { + temp = new HashMap<>(); + } + this.props = temp; + this.props.put("aai.endpoint", this.props.get("aai.endpoint").toString().replaceFirst(":\\d+", ":" + port)); + + } @Override public URL getEndpoint() throws MalformedURLException { return new URL(props.get("aai.endpoint").toString()); |