Added test coverage aaiinterfaceruleshandler
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / test / java / org / onap / appc / aai / interfaceImpl / TestAaiInterfaceRulesHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  * file except in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.aai.interfaceImpl;
20
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.sdnc.config.params.data.Parameter;
33 import org.onap.sdnc.config.params.data.ResponseKey;
34
35 public class TestAaiInterfaceRulesHandler {
36
37   private AaiInterfaceRulesHandler aaiInterfaceRulesHandlerSpy;
38   private Parameter parameters;
39   private SvcLogicContext context;
40   ResponseKey responseKey;
41   private List<ResponseKey> responseKeys;
42   private Set<String> params = new HashSet<>();
43
44   @Before
45   public void setUp() {
46     parameters = Mockito.mock(Parameter.class);
47     context = Mockito.mock(SvcLogicContext.class);
48     responseKey = new ResponseKey();
49     responseKey.setFieldKeyName("ipaddress-v4-oam-vip");
50     responseKey.setFilterByValue("vnfc-function-code");
51     responseKey.setFilterByField("vnfc-function-code");
52     responseKeys = new ArrayList<>();
53     responseKeys.add(responseKey);
54     params.add("param1");
55     when(context.getAttributeKeySet()).thenReturn(params);
56     when(context.getAttribute("tmp.vnfInfo.vm-count")).thenReturn("1");
57     when(context.getAttribute("tmp.vnfInfo.vm[" + 0 + "].vnfc-count")).thenReturn("1");
58     when(context.getAttribute("tmp.vnfInfo.vm[" + 0 + "].vnfc-function-code"))
59         .thenReturn("vnfc-function-code");
60     when(context.getAttribute("tmp.vnfInfo.vm[" + 0 + "].vnfc-ipaddress-v4-oam-vip"))
61         .thenReturn("0.0.0.0");
62     when(context.getAttribute("tmp.vnfInfo.vm[" + 0 + "].vnfc-name")).thenReturn("vnfc-name");
63     when(context.getAttribute("tmp.vnfInfo.vm[" + 0 + "].vserver-name")).thenReturn("vserver");
64     AaiInterfaceRulesHandler aaiInterfaceRulesHandler =
65         new AaiInterfaceRulesHandler(parameters, context);
66     aaiInterfaceRulesHandlerSpy = Mockito.spy(aaiInterfaceRulesHandler);
67   }
68
69   @Test
70   public void testProcessRuleVnf() {
71     responseKey.setUniqueKeyValue("vnf");
72     when(parameters.getName()).thenReturn("aaikey");
73     when(parameters.getResponseKeys()).thenReturn(responseKeys);
74     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
75     aaiInterfaceRulesHandlerSpy.processRule();
76     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
77   }
78
79   @Test
80   public void testProcessRuleVnfc() {
81     responseKey.setUniqueKeyValue("vnfc");
82     when(parameters.getName()).thenReturn("aaikey");
83     when(parameters.getResponseKeys()).thenReturn(responseKeys);
84     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
85     aaiInterfaceRulesHandlerSpy.processRule();
86     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
87   }
88
89   @Test
90   public void testProcessRuleVserver() {
91     responseKey.setUniqueKeyValue("vserver");
92     responseKey.setFieldKeyName("vserver-name");
93     responseKey.setFilterByValue("1");
94     responseKey.setFilterByField("vm-number");
95     when(parameters.getName()).thenReturn("aaikey");
96     when(parameters.getResponseKeys()).thenReturn(responseKeys);
97     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
98     aaiInterfaceRulesHandlerSpy.processRule();
99     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
100   }
101
102   @Test
103   public void testProcessRuleVserverElseCase() {
104     responseKey.setUniqueKeyValue("vserver");
105     responseKey.setFieldKeyName("vserver-name");
106     responseKey.setFilterByValue(null);
107     responseKey.setFilterByField("vm-number");
108     when(parameters.getName()).thenReturn("aaikey");
109     when(parameters.getResponseKeys()).thenReturn(responseKeys);
110     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
111     aaiInterfaceRulesHandlerSpy.processRule();
112     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
113   }
114
115   @Test
116   public void testProcessRuleWithVnfcname() {
117     responseKey.setFieldKeyName("vnfc-name");
118     responseKey.setUniqueKeyValue("vnfc");
119     when(parameters.getName()).thenReturn("aaikey");
120     when(parameters.getResponseKeys()).thenReturn(responseKeys);
121     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
122     aaiInterfaceRulesHandlerSpy.processRule();
123     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
124   }
125
126   @Test
127   public void testProcessRuleWithNoVnfcname() {
128     responseKey.setFieldKeyName("vnfc-name");
129     responseKey.setUniqueKeyValue("vnfc");
130     responseKey.setFilterByValue(null);
131     when(parameters.getName()).thenReturn("aaikey");
132     when(parameters.getResponseKeys()).thenReturn(responseKeys);
133     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
134     aaiInterfaceRulesHandlerSpy.processRule();
135     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
136   }
137
138   @Test
139   public void testProcessRuleWithIp() {
140     responseKey.setFieldKeyName("ipaddress-v4-oam-vip");
141     responseKey.setUniqueKeyValue("vnfc");
142     responseKey.setFilterByValue(null);
143     when(parameters.getName()).thenReturn("aaikey");
144     when(parameters.getResponseKeys()).thenReturn(responseKeys);
145     when(context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address")).thenReturn("0.0.0.0");
146     aaiInterfaceRulesHandlerSpy.processRule();
147     verify(aaiInterfaceRulesHandlerSpy, times(1)).processRule();
148   }
149
150 }