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 java.util.List;
28 import java.util.UUID;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.TimeUnit;
33 import java.util.stream.Collectors;
34 import java.util.stream.IntStream;
35 import javax.ws.rs.core.Response;
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
37 import org.apache.tinkerpop.gremlin.structure.Vertex;
38 import org.janusgraph.core.JanusGraph;
39 import org.janusgraph.core.JanusGraphTransaction;
40 import org.junit.Ignore;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.HttpTestUtil;
43 import org.onap.aai.db.props.AAIProperties;
44 import org.onap.aai.dbmap.AAIGraph;
45 import org.onap.aai.serialization.engines.QueryStyle;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class PserverDuplicateTest extends AAISetup {
51 private static final Logger LOGGER = LoggerFactory.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();
69 service.invokeAll(IntStream.range(0, threads).mapToObj((i) -> (Callable<Void>) () -> {
70 JanusGraphTransaction transaction = janusGraph.newTransaction();
71 GraphTraversalSource g = transaction.traversal();
73 g.addV().property(AAIProperties.AAI_URI, aaiUri).property(AAIProperties.NODE_TYPE, "pserver")
74 .property("hostname", hostname).next();
76 } catch (Exception e) {
77 throw new Exception("Duplicate was found, error");
80 }).collect(Collectors.toList()), 7, TimeUnit.SECONDS);
82 JanusGraphTransaction readOnlyTransaction =
83 AAIGraph.getInstance().getGraph().buildTransaction().readOnly().start();
84 GraphTraversalSource g = readOnlyTransaction.traversal();
86 List<Vertex> pserverList = g.V().has(AAIProperties.AAI_URI, aaiUri).toList();
87 LOGGER.debug("Number of pservers with uri {} is {}", aaiUri, pserverList.size());
89 testUtil = new HttpTestUtil(QueryStyle.TRAVERSAL_URI);
91 return pserverList.size() != 1;
95 public void testWhenDuplicatesExistInGraphThatGetAllSuceeds() throws InterruptedException {
97 int totalRetries = getNumOfRetries();
98 for (int retry = 0; retry < totalRetries; retry++) {
99 if (!this.createDuplicate()) {
100 if (retry == (totalRetries - 1)) {
101 fail("Unable to produce duplicates in the graph, "
102 + "please increase retry or ignore test if it becomes impossible to create duplicate this test");
105 // Successfully created a duplicate in the janus graph
110 String endpoint = "/aai/v14/cloud-infrastructure/pservers";
112 Response response = testUtil.doGet(endpoint, null);
113 LOGGER.info("GET ALL Pservers with duplicates status code {} and body {}", response.getStatus(),
114 response.getEntity());
115 assertThat(response.getStatus(), is(200));
118 public String getHostname() {
119 return UUID.randomUUID().toString();
122 public int getNumOfRetries() {
126 public int getNumberOfThreads() {