Improve the performance of resoures microservice
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / EdgeNotValidAnymoreTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.rest;
21
22 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23 import org.apache.tinkerpop.gremlin.structure.Edge;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraph;
26 import org.janusgraph.core.JanusGraphTransaction;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.HttpTestUtil;
32 import org.onap.aai.PayloadUtil;
33 import org.onap.aai.db.props.AAIProperties;
34 import org.onap.aai.dbmap.AAIGraph;
35 import org.onap.aai.edges.enums.EdgeField;
36 import org.onap.aai.edges.enums.EdgeProperty;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.serialization.engines.QueryStyle;
39
40 import javax.ws.rs.core.Response;
41 import java.io.IOException;
42 import java.util.UUID;
43
44 import static org.hamcrest.CoreMatchers.containsString;
45 import static org.hamcrest.CoreMatchers.is;
46 import static org.hamcrest.Matchers.not;
47 import static org.junit.Assert.assertThat;
48
49 public class EdgeNotValidAnymoreTest extends AAISetup {
50
51     private HttpTestUtil testUtil;
52
53     @Before
54     public void setupData() throws IOException, AAIException {
55
56         String cloudRegionEndpoint = "/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/junit-cloud-owner-with-vlan/junit-cloud-region-with-vlan";
57
58         String cloudRegionBody = PayloadUtil.getResourcePayload("cloud-region-with-vlan.json");
59         testUtil = new HttpTestUtil(QueryStyle.TRAVERSAL_URI);
60         testUtil.doPut(cloudRegionEndpoint, cloudRegionBody);
61
62         JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
63         GraphTraversalSource g = transaction.traversal();
64
65         Vertex configurationVertex = g.addV()
66             .property( AAIProperties.NODE_TYPE, "configuration")
67             .property( "configuration-id", "ci1")
68             .property( "configuration-type", "ci1")
69             .property( AAIProperties.AAI_URI, "/network/configurations/configuration/ci1")
70             .property(AAIProperties.SOURCE_OF_TRUTH, "JUNIT")
71             .next();
72
73         Vertex vlanVertex = g.V()
74             .has("vlan-interface", "test-vlan-interface-1")
75             .has(AAIProperties.NODE_TYPE, "vlan")
76             .next();
77
78         Edge edge = configurationVertex.addEdge("org.onap.relationships.inventory.PartOf", vlanVertex);
79         addEdge(edge);
80
81         transaction.commit();
82     }
83
84     public void addEdge(Edge edge) {
85         edge.property(EdgeProperty.CONTAINS.toString(), "NONE");
86         edge.property(EdgeProperty.DELETE_OTHER_V.toString(), "NONE");
87         edge.property(EdgeProperty.PREVENT_DELETE.toString(), "NONE");
88         edge.property(EdgeField.PRIVATE.toString(), false);
89         edge.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString());
90     }
91
92     @Test
93     public void testWhenEdgeRuleIsNoLongerValidEnsureItRetrievesVertexesWithoutOldEdges() {
94
95         String endpoint = "/aai/v14/network/configurations";
96
97         Response response = testUtil.doGet(endpoint, null);
98         assertThat(response.getStatus(), is(200));
99
100         String body = response.getEntity().toString();
101
102         assertThat(body, containsString("configuration-id"));
103         assertThat(body, not(containsString("vlan")));
104     }
105
106     @After
107     public void teardown(){
108
109         JanusGraph janusGraph = AAIGraph.getInstance().getGraph();
110         JanusGraphTransaction transaction = janusGraph.newTransaction();
111         GraphTraversalSource g = transaction.traversal();
112
113         g.V()
114             .has(AAIProperties.SOURCE_OF_TRUTH, "JUNIT")
115             .toList()
116             .forEach((edge) -> edge.remove());
117
118         transaction.commit();
119     }
120 }