query clients now have more useable result methods
[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
25 import org.junit.Test;
26 import org.onap.so.client.graphinventory.entities.DSLNode;
27 import org.onap.so.client.graphinventory.entities.DSLQueryBuilder;
28 import org.onap.so.client.graphinventory.entities.__;
29
30 public class DSLQueryBuilderTest {
31
32         
33         @Test
34         public void whereTest() {
35                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.CLOUD_REGION,
36                                 __.key("cloud-owner", "att-nc"),
37                                 __.key("cloud-region-id", "test")));
38                 
39                 builder.to(__.node(AAIObjectType.VLAN_TAG)).where(
40                                 __.node(AAIObjectType.OWNING_ENTITY,
41                                                 __.key("owning-entity-name", "name")
42                                         )       
43                                 ).to(__.node(AAIObjectType.VLAN_TAG, __.key("vlan-id-outer", "108")).output());
44                 
45                 assertEquals("cloud-region('cloud-owner', 'att-nc')('cloud-region-id', 'test') > "
46                                 + "vlan-tag (> owning-entity('owning-entity-name', 'name')) > "
47                                 + "vlan-tag*('vlan-id-outer', '108')", builder.build());
48         }
49         
50         @Test
51         public void unionTest() {
52                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.GENERIC_VNF,
53                                 __.key("vnf-id", "vnfId")).output());
54                 
55                 builder.union(__.node(AAIObjectType.PSERVER).output().to(__.node(AAIObjectType.COMPLEX).output()),
56                                 __.node(AAIObjectType.VSERVER).to(__.node(AAIObjectType.PSERVER).output().to(__.node(AAIObjectType.COMPLEX).output())));
57                 
58                 assertEquals("generic-vnf*('vnf-id', 'vnfId') > " + "[ pserver* > complex*, "
59                                  + "vserver > pserver* > complex* ]", builder.build());
60         }
61         
62         @Test
63         public void whereUnionTest() {
64                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.GENERIC_VNF,
65                                 __.key("vnf-id", "vnfId")).output());
66                 
67                 builder.where(
68                         __.union(
69                                 __.node(AAIObjectType.PSERVER, __.key("hostname", "hostname1")),
70                                 __.node(AAIObjectType.VSERVER).to(__.node(AAIObjectType.PSERVER, __.key("hostname", "hostname1")))));
71                 
72                 assertEquals("generic-vnf*('vnf-id', 'vnfId') (> [ pserver('hostname', 'hostname1'), "
73                                 + "vserver > pserver('hostname', 'hostname1') ])", builder.build());
74         }
75         
76         @Test
77         public void notNullTest() {
78                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.CLOUD_REGION,
79                                 __.key("cloud-owner", "", "null").not()).output());
80                 
81                 assertEquals("cloud-region* !('cloud-owner', ' ', ' null ')", builder.build());
82         }
83         
84         @Test
85         public void shortCutToTest() {
86                 DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER,
87                                 __.key("hostname", "my-hostname")).output());
88                 
89                 builder.to(AAIObjectType.P_INTERFACE).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
90                 assertEquals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id')", builder.build());
91         }
92 }