c6104297bba586195287b4fab046e1d0d1909b41
[appc.git] / appc-dg-util / appc-dg-util-bundle / src / test / java / org / onap / appc / dg / util / impl / ExecuteNodeActionImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2018 Nokia
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.util.impl;
27
28 import static junit.framework.TestCase.assertTrue;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.mockito.BDDMockito.given;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Matchers.anyMap;
35 import static org.mockito.Matchers.anyString;
36 import static org.mockito.Matchers.eq;
37 import static org.mockito.Mockito.times;
38 import static org.mockito.Mockito.verify;
39
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Map;
43 import java.util.Set;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.runners.MockitoJUnitRunner;
50 import org.onap.appc.exceptions.APPCException;
51 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
52 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
53 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
54 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class ExecuteNodeActionImplTest {
58
59     private static final String resourceType = "resourceType";
60     private static final String prefix = "prefix";
61     private static final String resourceKey = "resourceKey";
62     private static final String attributeName = "attributeName";
63     private static final String attributeValue = "attributeValue";
64     private static final Map<String, String> params = new HashMap<>();
65     private static final SvcLogicContext SVC_LOGIC_CONTEXT = new SvcLogicContext();
66     private static final SvcLogicResource.QueryStatus SUCCESS_STATUS = SvcLogicResource.QueryStatus.SUCCESS;
67     private static final QueryStatus FAILED_STATUS = SvcLogicResource.QueryStatus.FAILURE;
68
69     @Mock
70     private AAIServiceFactory aaiServiceFactory;
71     @Mock
72     private AAIService aaiService;
73     private ExecuteNodeActionImpl executeNodeAction;
74
75     @Before
76     public void setUp() {
77         executeNodeAction = new ExecuteNodeActionImpl(aaiServiceFactory);
78         given(aaiServiceFactory.getAAIService()).willReturn(aaiService);
79
80         params.put("resourceType", resourceType);
81         params.put("prefix", prefix);
82         params.put("resourceKey", resourceKey);
83         params.put("attributeName", attributeName);
84         params.put("attributeValue", attributeValue);
85         params.put("waitTime", "1");
86     }
87
88     @Test
89     public void testWaitMethod() throws Exception {
90         executeNodeAction.waitMethod(params, SVC_LOGIC_CONTEXT);
91     }
92
93     @Test
94     public void testGetResource() throws Exception {
95         given(aaiService.query(any(), Mockito.anyBoolean(),
96             any(), any(), any(), any(),
97             any(SvcLogicContext.class))).willReturn(SUCCESS_STATUS);
98
99         executeNodeAction.getResource(params, SVC_LOGIC_CONTEXT);
100
101         verify(aaiService, times(1)).query(resourceType, false, null, resourceKey, prefix, null, SVC_LOGIC_CONTEXT);
102         assertEquals(SUCCESS_STATUS.toString(), SVC_LOGIC_CONTEXT.getAttribute("getResource_result"));
103     }
104
105     @Test
106     public void testPostResource() throws Exception {
107         given(aaiService.update(eq(resourceType), eq(resourceKey), anyMap(),
108             eq(prefix), eq(SVC_LOGIC_CONTEXT))).willReturn(SUCCESS_STATUS);
109
110         executeNodeAction.postResource(params, SVC_LOGIC_CONTEXT);
111
112         verify(aaiService, times(1)).update(eq(resourceType), eq(resourceKey), anyMap(), eq(prefix),
113             eq(SVC_LOGIC_CONTEXT));
114         assertEquals(SUCCESS_STATUS.toString(), SVC_LOGIC_CONTEXT.getAttribute("postResource_result"));
115     }
116
117     @Test
118     public void testDeleteResource() throws Exception {
119         given(aaiService.delete(eq(resourceType), eq(resourceKey),
120             eq(SVC_LOGIC_CONTEXT))).willReturn(SUCCESS_STATUS);
121
122         executeNodeAction.deleteResource(params, SVC_LOGIC_CONTEXT);
123
124         verify(aaiService, times(1)).delete(eq(resourceType), eq(resourceKey), eq(SVC_LOGIC_CONTEXT));
125         assertEquals(SUCCESS_STATUS.toString(), SVC_LOGIC_CONTEXT.getAttribute("deleteResource_result"));
126     }
127
128     @Test
129     public void testGetVnfHierarchySuccess() throws Exception {
130         given(aaiService.query(any(), Mockito.anyBoolean(),
131             any(), any(), any(), any(),
132             any(SvcLogicContext.class))).willReturn(SUCCESS_STATUS);
133
134         executeNodeAction.getVnfHierarchy(params, SVC_LOGIC_CONTEXT);
135
136         assertEquals("0", SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFCCount"));
137         assertEquals("SUCCESS", SVC_LOGIC_CONTEXT.getAttribute("getVnfHierarchy_result"));
138     }
139
140     @Test(expected = APPCException.class)
141     public void testGetVnfHierarchyFailure() throws Exception {
142         given(aaiService.query(any(), Mockito.anyBoolean(),
143             any(), any(), any(), any(),
144             any(SvcLogicContext.class))).willReturn(FAILED_STATUS);
145
146         executeNodeAction.getVnfHierarchy(params, SVC_LOGIC_CONTEXT);
147
148         assertEquals("0", SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFCCount"));
149         assertEquals("FAILURE", SVC_LOGIC_CONTEXT.getAttribute("getVnfHierarchy_result"));
150         assertNotNull(SVC_LOGIC_CONTEXT.getAttribute("output.status.message"));
151     }
152
153     @Test
154     public void testPopulateVnfcsDetailsinContext() throws Exception {
155         Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
156         Set<String> vServersList = new HashSet<>();
157         vnfcHierarchyMap.put("SMP", vServersList);
158         vServersList.add("smp-0-url");
159         vServersList.add("smp-1-url");
160
161         given(aaiService.query(eq("vnfc"), eq(false), anyString(), eq("vnfc-name = 'SMP'"),
162             eq("vnfcRetrived"), anyString(), any(SvcLogicContext.class))).willReturn(SUCCESS_STATUS);
163
164         executeNodeAction.populateVnfcsDetailsinContext(vnfcHierarchyMap, SVC_LOGIC_CONTEXT);
165
166         assertNull(SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFC[0].TYPE"));
167         assertNull(SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFC[0].NAME"));
168         assertEquals("2", SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFC[0].VM_COUNT"));
169         assertTrue(vServersList.contains(SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFC[0].VM[0].URL")));
170         assertTrue(vServersList.contains(SVC_LOGIC_CONTEXT.getAttribute("VNF.VNFC[0].VM[1].URL")));
171     }
172 }