2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest;
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;
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;
46 import static org.hamcrest.CoreMatchers.is;
47 import static org.junit.Assert.assertThat;
48 import static org.junit.Assert.fail;
50 public class PserverDuplicateTest extends AAISetup {
52 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PserverDuplicateTest.class);
54 private HttpTestUtil testUtil;
56 private String hostname;
58 public boolean createDuplicate() throws InterruptedException {
60 hostname = getHostname();
61 final String aaiUri = "/cloud-infrastructure/pservers/pserver/" + hostname;
62 final int threads = getNumberOfThreads();
64 ExecutorService service = Executors.newFixedThreadPool(threads);
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();
71 IntStream.range(0, threads)
72 .mapToObj((i) -> (Callable<Void>) () -> {
73 JanusGraphTransaction transaction = janusGraph.newTransaction();
74 GraphTraversalSource g = transaction.traversal();
77 .property(AAIProperties.AAI_URI, aaiUri)
78 .property(AAIProperties.NODE_TYPE, "pserver")
79 .property("hostname", hostname)
82 } catch (Exception e) {
83 throw new Exception("Duplicate was found, error");
86 }).collect(Collectors.toList())
90 JanusGraphTransaction readOnlyTransaction = AAIGraph.getInstance().getGraph().buildTransaction().readOnly().start();
91 GraphTraversalSource g = readOnlyTransaction.traversal();
93 List<Vertex> pserverList = g.V().has(AAIProperties.AAI_URI, aaiUri).toList();
94 LOGGER.debug("Number of pservers with uri {} is {}", aaiUri, pserverList.size());
96 testUtil = new HttpTestUtil(QueryStyle.TRAVERSAL_URI);
98 if(pserverList.size() == 1){
106 public void testWhenDuplicatesExistInGraphThatGetAllSuceeds() throws InterruptedException {
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");
116 // Successfully created a duplicate in the janus graph
121 String endpoint = "/aai/v14/cloud-infrastructure/pservers";
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));
128 public String getHostname(){
129 return UUID.randomUUID().toString();
132 public int getNumOfRetries(){
136 public int getNumberOfThreads(){