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;
36 import javax.ws.rs.core.Response;
38 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.janusgraph.core.JanusGraph;
41 import org.janusgraph.core.JanusGraphTransaction;
42 import org.junit.Ignore;
43 import org.onap.aai.AAISetup;
44 import org.onap.aai.HttpTestUtil;
45 import org.onap.aai.db.props.AAIProperties;
46 import org.onap.aai.dbmap.AAIGraph;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class PserverDuplicateTest extends AAISetup {
53 private static final Logger LOGGER = LoggerFactory.getLogger(PserverDuplicateTest.class);
55 private HttpTestUtil testUtil;
57 private String hostname;
59 public boolean createDuplicate() throws InterruptedException {
61 hostname = getHostname();
62 final String aaiUri = "/cloud-infrastructure/pservers/pserver/" + hostname;
63 final int threads = getNumberOfThreads();
65 ExecutorService service = Executors.newFixedThreadPool(threads);
67 JanusGraph janusGraph = AAIGraph.getInstance().getGraph();
68 // Due to the lazy instantiation of the graph, it needs to create a new transaction to create schema
69 janusGraph.newTransaction().rollback();
71 service.invokeAll(IntStream.range(0, threads).mapToObj((i) -> (Callable<Void>) () -> {
72 JanusGraphTransaction transaction = janusGraph.newTransaction();
73 GraphTraversalSource g = transaction.traversal();
75 g.addV().property(AAIProperties.AAI_URI, aaiUri).property(AAIProperties.NODE_TYPE, "pserver")
76 .property("hostname", hostname).next();
78 } catch (Exception e) {
79 throw new Exception("Duplicate was found, error");
82 }).collect(Collectors.toList()), 7, TimeUnit.SECONDS);
84 JanusGraphTransaction readOnlyTransaction =
85 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 return pserverList.size() != 1;
97 public void testWhenDuplicatesExistInGraphThatGetAllSuceeds() throws InterruptedException {
99 int totalRetries = getNumOfRetries();
100 for (int retry = 0; retry < totalRetries; retry++) {
101 if (!this.createDuplicate()) {
102 if (retry == (totalRetries - 1)) {
103 fail("Unable to produce duplicates in the graph, "
104 + "please increase retry or ignore test if it becomes impossible to create duplicate this test");
107 // Successfully created a duplicate in the janus graph
112 String endpoint = "/aai/v14/cloud-infrastructure/pservers";
114 Response response = testUtil.doGet(endpoint, null);
115 LOGGER.info("GET ALL Pservers with duplicates status code {} and body {}", response.getStatus(),
116 response.getEntity());
117 assertThat(response.getStatus(), is(200));
120 public String getHostname() {
121 return UUID.randomUUID().toString();
124 public int getNumOfRetries() {
128 public int getNumberOfThreads() {