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