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