Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / PserverDuplicateTest.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
21 package org.onap.aai.rest;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.fail;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.util.List;
31 import java.util.UUID;
32 import java.util.concurrent.Callable;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.TimeUnit;
36 import java.util.stream.Collectors;
37 import java.util.stream.IntStream;
38
39 import javax.ws.rs.core.Response;
40
41 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
42 import org.apache.tinkerpop.gremlin.structure.Vertex;
43 import org.janusgraph.core.JanusGraph;
44 import org.janusgraph.core.JanusGraphTransaction;
45 import org.junit.Ignore;
46 import org.junit.Test;
47 import org.onap.aai.AAISetup;
48 import org.onap.aai.HttpTestUtil;
49 import org.onap.aai.db.props.AAIProperties;
50 import org.onap.aai.dbmap.AAIGraph;
51 import org.onap.aai.serialization.engines.QueryStyle;
52
53 public class PserverDuplicateTest extends AAISetup {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(PserverDuplicateTest.class);
56
57     private HttpTestUtil testUtil;
58
59     private String hostname;
60
61     public boolean createDuplicate() throws InterruptedException {
62
63         hostname = getHostname();
64         final String aaiUri = "/cloud-infrastructure/pservers/pserver/" + hostname;
65         final int threads = getNumberOfThreads();
66
67         ExecutorService service = Executors.newFixedThreadPool(threads);
68
69         JanusGraph janusGraph = AAIGraph.getInstance().getGraph();
70         // Due to the lazy instantiation of the graph, it needs to create a new transaction to create schema
71         janusGraph.newTransaction().rollback();
72
73         service.invokeAll(IntStream.range(0, threads).mapToObj((i) -> (Callable<Void>) () -> {
74             JanusGraphTransaction transaction = janusGraph.newTransaction();
75             GraphTraversalSource g = transaction.traversal();
76             try {
77                 g.addV().property(AAIProperties.AAI_URI, aaiUri).property(AAIProperties.NODE_TYPE, "pserver")
78                         .property("hostname", hostname).next();
79                 transaction.commit();
80             } catch (Exception e) {
81                 throw new Exception("Duplicate was found, error");
82             }
83             return null;
84         }).collect(Collectors.toList()), 7, TimeUnit.SECONDS);
85
86         JanusGraphTransaction readOnlyTransaction =
87                 AAIGraph.getInstance().getGraph().buildTransaction().readOnly().start();
88         GraphTraversalSource g = readOnlyTransaction.traversal();
89
90         List<Vertex> pserverList = g.V().has(AAIProperties.AAI_URI, aaiUri).toList();
91         LOGGER.debug("Number of pservers with uri {} is {}", aaiUri, pserverList.size());
92
93         testUtil = new HttpTestUtil(QueryStyle.TRAVERSAL_URI);
94
95         if (pserverList.size() == 1) {
96             return false;
97         }
98         return true;
99     }
100
101     @Ignore
102     public void testWhenDuplicatesExistInGraphThatGetAllSuceeds() throws InterruptedException {
103
104         int totalRetries = getNumOfRetries();
105         for (int retry = 0; retry < totalRetries; retry++) {
106             if (!this.createDuplicate()) {
107                 if (retry == (totalRetries - 1)) {
108                     fail("Unable to produce duplicates in the graph, "
109                             + "please increase retry or ignore test if it becomes impossible to create duplicate this test");
110                 }
111             } else {
112                 // Successfully created a duplicate in the janus graph
113                 break;
114             }
115         }
116
117         String endpoint = "/aai/v14/cloud-infrastructure/pservers";
118
119         Response response = testUtil.doGet(endpoint, null);
120         LOGGER.info("GET ALL Pservers with duplicates status code {} and body {}", response.getStatus(),
121                 response.getEntity());
122         assertThat(response.getStatus(), is(200));
123     }
124
125     public String getHostname() {
126         return UUID.randomUUID().toString();
127     }
128
129     public int getNumOfRetries() {
130         return 10;
131     }
132
133     public int getNumberOfThreads() {
134         return 10;
135     }
136 }