d6cdbdfccdc7e9ebb37584910fb0b1f43585f6c7
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / adapters / vnf / QueryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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.so.adapters.vnf;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.Matchers.hasProperty;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Map;
30
31 import javax.xml.ws.Holder;
32
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.onap.so.adapters.vnf.exceptions.VnfException;
42 import org.onap.so.openstack.beans.HeatStatus;
43 import org.onap.so.openstack.beans.StackInfo;
44 import org.onap.so.openstack.beans.VnfStatus;
45 import org.onap.so.openstack.exceptions.MsoException;
46 import org.onap.so.openstack.exceptions.MsoOpenstackException;
47 import org.onap.so.openstack.utils.MsoHeatUtils;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class QueryTest {
51         
52         @Mock
53         private MsoHeatUtils heat;
54         @InjectMocks
55         private MsoVnfAdapterImpl vnfAdapter = new MsoVnfAdapterImpl();
56         
57         @Rule
58         public ExpectedException thrown = ExpectedException.none();
59         @Test
60         public void testQueryCreatedVnf() throws VnfException, MsoException {
61                 StackInfo info = new StackInfo("stackName", HeatStatus.CREATED);
62                 when(heat.queryStack(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(info);
63                 String cloudId = "MT";
64                 String tenantId = "MSO_Test";
65                 String vnfName = "VNF_TEST1";
66                 Holder<Boolean> vnfExists = new Holder<>();
67                 Holder<String> vnfId = new Holder<>();
68                 Holder<VnfStatus> status = new Holder<>();
69                 Holder<Map<String, String>> outputs = new Holder<>();
70
71                 vnfAdapter.queryVnf(cloudId, tenantId, vnfName, null, vnfExists, vnfId, status, outputs);
72
73                 assertTrue(vnfExists.value);
74         }
75
76         @Test
77         public void testQueryNotFoundVnf() throws VnfException, MsoException {
78                 StackInfo info = new StackInfo("stackName", HeatStatus.NOTFOUND);
79                 when(heat.queryStack(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(info);
80                 String cloudId = "MT";
81                 String tenantId = "MSO_Test";
82                 String vnfName = "VNF_TEST1";
83                 Holder<Boolean> vnfExists = new Holder<>();
84                 Holder<String> vnfId = new Holder<>();
85                 Holder<VnfStatus> status = new Holder<>();
86                 Holder<Map<String, String>> outputs = new Holder<>();
87
88                 vnfAdapter.queryVnf(cloudId, tenantId, vnfName, null, vnfExists, vnfId, status, outputs);
89
90                 assertFalse(vnfExists.value);
91         }
92
93         @Test()
94         // @Ignore // 1802 merge
95         public void testQueryVnfWithException() throws VnfException, MsoException {
96                 String cloudId = "MT";
97                 String tenantId = "MSO_Test";
98                 String vnfName = "VNF_TEST1";
99                 Holder<Boolean> vnfExists = new Holder<>();
100                 Holder<String> vnfId = new Holder<>();
101                 Holder<VnfStatus> status = new Holder<>();
102                 Holder<Map<String, String>> outputs = new Holder<>();
103                 thrown.expect(VnfException.class);
104                 thrown.expectCause(hasProperty("context", is("QueryVNF")));
105                 when(heat.queryStack(Mockito.any(), Mockito.any(), Mockito.any())).thenThrow(new MsoOpenstackException(1, "test messsage", "test detail"));
106                 vnfAdapter.queryVnf(cloudId, tenantId, vnfName, null, vnfExists, vnfId, status, outputs);
107         }
108 }