a5bbc64eb986e35567df3edcfdc2e9789cd51280
[so.git] / common / src / test / java / org / onap / so / client / aai / DSLQueryBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.so.client.aai;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import org.junit.Test;
27 import org.onap.so.client.graphinventory.entities.DSLNode;
28 import org.onap.so.client.graphinventory.entities.DSLQueryBuilder;
29 import org.onap.so.client.graphinventory.entities.__;
30
31 public class DSLQueryBuilderTest {
32
33         
34         @Test
35         public void whereTest() {
36                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.CLOUD_REGION,
37                                 __.key("cloud-owner", "att-nc"),
38                                 __.key("cloud-region-id", "test")));
39                 
40                 builder.to(__.node(AAIObjectType.VLAN_TAG)).where(
41                                 __.node(AAIObjectType.OWNING_ENTITY,
42                                                 __.key("owning-entity-name", "name")
43                                         )       
44                                 ).to(__.node(AAIObjectType.VLAN_TAG, __.key("vlan-id-outer", "108")).output());
45                 
46                 assertEquals("cloud-region('cloud-owner', 'att-nc')('cloud-region-id', 'test') > "
47                                 + "vlan-tag (> owning-entity('owning-entity-name', 'name')) > "
48                                 + "vlan-tag*('vlan-id-outer', '108')", builder.build());
49         }
50         
51         @Test
52         public void unionTest() {
53                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.GENERIC_VNF,
54                                 __.key("vnf-id", "vnfId")).output());
55                 
56                 builder.union(__.node(AAIObjectType.PSERVER).output().to(__.node(AAIObjectType.COMPLEX).output()),
57                                 __.node(AAIObjectType.VSERVER).to(__.node(AAIObjectType.PSERVER).output().to(__.node(AAIObjectType.COMPLEX).output())));
58                 
59                 assertEquals("generic-vnf*('vnf-id', 'vnfId') > " + "[ pserver* > complex*, "
60                                  + "vserver > pserver* > complex* ]", builder.build());
61         }
62         
63         @Test
64         public void whereUnionTest() {
65                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.GENERIC_VNF,
66                                 __.key("vnf-id", "vnfId")).output());
67                 
68                 builder.where(
69                         __.union(
70                                 __.node(AAIObjectType.PSERVER, __.key("hostname", "hostname1")),
71                                 __.node(AAIObjectType.VSERVER).to(__.node(AAIObjectType.PSERVER, __.key("hostname", "hostname1")))));
72                 
73                 assertEquals("generic-vnf*('vnf-id', 'vnfId') (> [ pserver('hostname', 'hostname1'), "
74                                 + "vserver > pserver('hostname', 'hostname1') ])", builder.build());
75         }
76         
77         @Test
78         public void notNullTest() {
79                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.CLOUD_REGION,
80                                 __.key("cloud-owner", "", "null").not()).output());
81                 
82                 assertEquals("cloud-region* !('cloud-owner', ' ', ' null ')", builder.build());
83         }
84         
85         @Test
86         public void shortCutToTest() {
87                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER,
88                                 __.key("hostname", "my-hostname")).output());
89                 
90                 builder.to(AAIObjectType.P_INTERFACE).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
91                 assertEquals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id')", builder.build());
92         }
93         
94         @Test
95         public void limitTest() {
96                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER,
97                                 __.key("hostname", "my-hostname")).output());
98                 
99                 builder.to(AAIObjectType.P_INTERFACE).limit(2).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
100                 assertEquals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id') LIMIT 2", builder.build());
101         }
102         
103         @Test
104         public void equalsTest() {
105                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER,
106                                 __.key("hostname", "my-hostname")).output());
107                 
108                 builder.to(AAIObjectType.P_INTERFACE).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
109                 assertTrue(builder.equals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id')"));
110                 assertTrue(builder.equals(builder));
111         }
112 }