Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / VnfcRelationshipIssueTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.rest;
22
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.janusgraph.core.JanusGraphTransaction;
25 import org.junit.*;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.Parameterized;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.HttpTestUtil;
30 import org.onap.aai.PayloadUtil;
31 import org.onap.aai.dbmap.AAIGraph;
32 import org.onap.aai.serialization.engines.QueryStyle;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.test.context.junit4.rules.SpringClassRule;
36 import org.springframework.test.context.junit4.rules.SpringMethodRule;
37
38 import javax.ws.rs.core.Response;
39 import java.util.Arrays;
40 import java.util.Collection;
41
42 import static junit.framework.TestCase.fail;
43 import static org.junit.Assert.assertEquals;
44
45 @RunWith(value = Parameterized.class)
46 public class VnfcRelationshipIssueTest extends AAISetup {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(VnfcRelationshipIssueTest.class);
49     private HttpTestUtil httpTestUtil;
50
51     @ClassRule
52     public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
53
54     @Rule
55     public final SpringMethodRule springMethodRule = new SpringMethodRule();
56
57     @Parameterized.Parameter(value = 0)
58     public QueryStyle queryStyle;
59
60     @Parameterized.Parameters(name = "QueryStyle.{0}")
61     public static Collection<Object[]> data() {
62         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
63     }
64
65     @Before
66     public void setUp() {
67         httpTestUtil = new HttpTestUtil(queryStyle);
68     }
69
70     @Test
71     public void testCreateVnfWithVfModuleAndCreateVnfcRelatedToVfModule() throws Exception {
72
73         String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/test-vnf11";
74         String genericVnfPayload = PayloadUtil.getResourcePayload("generic-vnf-with-vf-module.json");
75
76         Response response = httpTestUtil.doPut(genericVnfUri, genericVnfPayload);
77         assertEquals("Expected the generic vnf to be created", 201, response.getStatus());
78
79         String vnfcUri = "/aai/v14/network/vnfcs/vnfc/test-vnfc11";
80         String vnfcPaylaod = PayloadUtil.getResourcePayload("vnfc-related-to-vf-module.json");
81
82         response = httpTestUtil.doPut(vnfcUri, vnfcPaylaod);
83         assertEquals("Expected the generic vnf to be created", 201, response.getStatus());
84     }
85
86     @After
87     public void tearDown() {
88
89         JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
90         boolean success = true;
91
92         try {
93
94             GraphTraversalSource g = transaction.traversal();
95
96             g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
97
98         } catch (Exception ex) {
99             success = false;
100             LOGGER.error("Unable to remove the vertexes", ex);
101         } finally {
102             if (success) {
103                 transaction.commit();
104             } else {
105                 transaction.rollback();
106                 fail("Unable to teardown the graph");
107             }
108         }
109
110     }
111 }