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.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;
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;
45 import static org.hamcrest.CoreMatchers.is;
46 import static org.junit.Assert.assertThat;
47 import static org.junit.Assert.fail;
49 public class PserverDuplicateTest extends AAISetup {
51 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PserverDuplicateTest.class);
53 private HttpTestUtil testUtil;
55 private String hostname;
57 public boolean createDuplicate() throws InterruptedException {
59 hostname = getHostname();
60 final String aaiUri = "/cloud-infrastructure/pservers/pserver/" + hostname;
61 final int threads = getNumberOfThreads();
63 ExecutorService service = Executors.newFixedThreadPool(threads);
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();
70 IntStream.range(0, threads)
71 .mapToObj((i) -> (Callable<Void>) () -> {
72 JanusGraphTransaction transaction = janusGraph.newTransaction();
73 GraphTraversalSource g = transaction.traversal();
75 .property(AAIProperties.AAI_URI, aaiUri)
76 .property(AAIProperties.NODE_TYPE, "pserver")
77 .property("hostname", hostname)
81 }).collect(Collectors.toList())
85 JanusGraphTransaction readOnlyTransaction = AAIGraph.getInstance().getGraph().buildTransaction().readOnly().start();
86 GraphTraversalSource g = readOnlyTransaction.traversal();
88 List<Vertex> pserverList = g.V().has(AAIProperties.AAI_URI, aaiUri).toList();
89 LOGGER.debug("Number of pservers with uri {} is {}", aaiUri, pserverList.size());
91 testUtil = new HttpTestUtil(QueryStyle.TRAVERSAL_URI);
93 if(pserverList.size() == 1){
101 public void testWhenDuplicatesExistInGraphThatGetAllSuceeds() throws InterruptedException {
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");
111 // Successfully created a duplicate in the janus graph
116 String endpoint = "/aai/v14/cloud-infrastructure/pservers";
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));
123 public String getHostname(){
124 return UUID.randomUUID().toString();
127 public int getNumOfRetries(){
131 public int getNumberOfThreads(){