Improve the performance of resoures microservice
[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.Test;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.HttpTestUtil;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.dbmap.AAIGraph;
33 import org.onap.aai.serialization.engines.QueryStyle;
34
35 import javax.ws.rs.core.Response;
36 import java.util.List;
37 import java.util.UUID;
38 import java.util.concurrent.Callable;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Executors;
41 import java.util.concurrent.TimeUnit;
42 import java.util.stream.Collectors;
43 import java.util.stream.IntStream;
44
45 import static org.hamcrest.CoreMatchers.is;
46 import static org.junit.Assert.assertThat;
47 import static org.junit.Assert.fail;
48
49 public class PserverDuplicateTest extends AAISetup {
50
51     private static final EELFLogger LOGGER = EELFManager.getInstance().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(
70             IntStream.range(0, threads)
71             .mapToObj((i) -> (Callable<Void>) () -> {
72                 JanusGraphTransaction transaction = janusGraph.newTransaction();
73                 GraphTraversalSource g = transaction.traversal();
74                 g.addV()
75                     .property(AAIProperties.AAI_URI, aaiUri)
76                     .property(AAIProperties.NODE_TYPE, "pserver")
77                     .property("hostname", hostname)
78                     .next();
79                 transaction.commit();
80                 return null;
81             }).collect(Collectors.toList())
82             , 7, TimeUnit.SECONDS
83         );
84
85         JanusGraphTransaction readOnlyTransaction = 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         if(pserverList.size() == 1){
94             return false;
95         }
96         return true;
97     }
98
99
100     @Test
101     public void testWhenDuplicatesExistInGraphThatGetAllSuceeds() throws InterruptedException {
102
103         int totalRetries = getNumOfRetries();
104         for(int retry = 0; retry < totalRetries; retry++){
105             if(!this.createDuplicate()){
106                 if(retry == (totalRetries-1)){
107                     fail("Unable to produce duplicates in the graph, " +
108                         "please increase retry or ignore test if it becomes impossible to create duplicate this test");
109                 }
110             } else {
111                 // Successfully created a duplicate in the janus graph
112                 break;
113             }
114         }
115
116         String endpoint = "/aai/v14/cloud-infrastructure/pservers";
117
118         Response response = testUtil.doGet(endpoint, null);
119         LOGGER.info("GET ALL Pservers with duplicates status code {} and body {}", response.getStatus(), response.getEntity());
120         assertThat(response.getStatus(), is(200));
121     }
122
123     public String getHostname(){
124         return UUID.randomUUID().toString();
125     }
126
127     public int getNumOfRetries(){
128         return 10;
129     }
130
131     public int getNumberOfThreads(){
132        return 10;
133     }
134 }
135