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