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