Change the unit test of SDNC with mock.
[aai/esr-server.git] / esr-mgr / src / test / java / org / onap / aai / esr / wrapper / ThirdpartySdncWrapperTest.java
1 /**
2  * Copyright 2018 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.aai.esr.wrapper;
17
18 import static org.junit.Assert.assertEquals;
19 import java.util.ArrayList;
20 import java.util.List;
21 import javax.ws.rs.core.Response;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.onap.aai.esr.common.MsbConfig;
26 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
27 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
28 import org.onap.aai.esr.exception.ExtsysException;
29 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
30 import org.onap.aai.esr.util.ExtsysUtil;
31
32 public class ThirdpartySdncWrapperTest {
33
34     static {
35         MsbConfig.setMsbServerAddr("http://127.0.0.1:80");
36     }
37
38     @Test
39     public void test_registerThirdpartySdnc() throws ExtsysException {
40         ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
41         sdncRegisterInfo.setLocation("edge");
42         sdncRegisterInfo.setName("SDNC_TEST");
43         sdncRegisterInfo.setPassword("123987");
44         sdncRegisterInfo.setProductName("thirdparty SDNC");
45         sdncRegisterInfo.setProtocol("protocol");
46         sdncRegisterInfo.setThirdpartySdncId("123456");
47         sdncRegisterInfo.setType("SDNC");
48         sdncRegisterInfo.setUrl("http://ip:8000");
49         sdncRegisterInfo.setUserName("nancy");
50         sdncRegisterInfo.setVendor("zte");
51         sdncRegisterInfo.setVersion("v1");
52         ExternalSystemProxy mockExternalSystemProxy = Mockito.mock(ExternalSystemProxy.class);
53         Mockito.doNothing().when(mockExternalSystemProxy).registerSdnc(Mockito.anyString(), (EsrThirdpartySdncDetail)Mockito.anyObject());
54         ThirdpartySdncWrapper thirdpartySdncWrapper = new ThirdpartySdncWrapper(mockExternalSystemProxy);
55         Response response = thirdpartySdncWrapper.registerThirdpartySdnc(sdncRegisterInfo);
56         if (response != null) {
57             Assert.assertTrue(response.getStatus() == 200);
58         }
59     }
60
61     @Test
62     public void test_delThirdpartySdnc() throws ExtsysException {
63         String sdncDetail = "{\"thirdparty-sdnc-id\":\"123456\",\"location\":\"edge\","
64                 + "\"product-name\":\"thirdparty SDNC\",\"esr-system-info-list\":{\"esr-system-info\":"
65                 + "[{\"esr-system-info-id\":\"987654\",\"system-name\":\"SDNC_TEST\",\"type\":\"SDNC\","
66                 + "\"vendor\":\"zte\",\"version\":\"v1\",\"service-url\":\"http://ip:8000\","
67                 + "\"user-name\":\"nancy\",\"password\":\"123987\",\"system-type\":\"thirdparty_SDNC\","
68                 + "\"protocol\":\"protocol\"}]}}";
69         ExternalSystemProxy mockExternalSystemProxy = Mockito.mock(ExternalSystemProxy.class);
70         Mockito.doNothing().when(mockExternalSystemProxy).deleteThirdpartySdnc(Mockito.anyString(), Mockito.anyString());
71         Mockito.when(mockExternalSystemProxy.queryThirdpartySdncDetail(Mockito.anyString())).thenReturn(sdncDetail);
72         ThirdpartySdncWrapper thirdpartySdncWrapper = new ThirdpartySdncWrapper(mockExternalSystemProxy);
73         Response response = thirdpartySdncWrapper.delThirdpartySdnc("123456");
74         if (response != null) {
75             Assert.assertTrue(response.getStatus() == 204);
76         }
77     }
78
79     @Test
80     public void test_queryThirdpartySdncById() throws ExtsysException {
81         ExtsysUtil extsysUtil = new ExtsysUtil();
82         ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
83         sdncRegisterInfo.setLocation("edge");
84         sdncRegisterInfo.setName("SDNC_TEST");
85         sdncRegisterInfo.setPassword("123987");
86         sdncRegisterInfo.setProductName("thirdparty SDNC");
87         sdncRegisterInfo.setProtocol("protocol");
88         sdncRegisterInfo.setThirdpartySdncId("123456");
89         sdncRegisterInfo.setType("SDNC");
90         sdncRegisterInfo.setUrl("http://ip:8000");
91         sdncRegisterInfo.setUserName("nancy");
92         sdncRegisterInfo.setVendor("zte");
93         sdncRegisterInfo.setVersion("v1");
94         String sdncDetail = "{\"thirdparty-sdnc-id\":\"123456\",\"location\":\"edge\","
95                 + "\"product-name\":\"thirdparty SDNC\",\"esr-system-info-list\":{\"esr-system-info\":"
96                 + "[{\"esr-system-info-id\":\"987654\",\"system-name\":\"SDNC_TEST\",\"type\":\"SDNC\","
97                 + "\"vendor\":\"zte\",\"version\":\"v1\",\"service-url\":\"http://ip:8000\","
98                 + "\"user-name\":\"nancy\",\"password\":\"123987\",\"system-type\":\"thirdparty_SDNC\","
99                 + "\"protocol\":\"protocol\"}]}}";
100         ExternalSystemProxy mockExternalSystemProxy = Mockito.mock(ExternalSystemProxy.class);
101         Mockito.when(mockExternalSystemProxy.queryThirdpartySdncDetail(Mockito.anyString())).thenReturn(sdncDetail);
102         ThirdpartySdncWrapper thirdpartySdncWrapper = new ThirdpartySdncWrapper(mockExternalSystemProxy);
103         Response response = thirdpartySdncWrapper.queryThirdpartySdncById("123456");
104         if (response != null) {
105             Assert.assertTrue(response.getStatus() == 200);
106             assertEquals(extsysUtil.objectToString(sdncRegisterInfo), extsysUtil.objectToString(response.getEntity()));
107         }
108     }
109
110     @Test
111     public void test_queryThirdpartySdncList() throws ExtsysException {
112         ExtsysUtil extsysUtil = new ExtsysUtil();
113         List<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<>();
114         ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
115         sdncRegisterInfo.setLocation("edge");
116         sdncRegisterInfo.setName("SDNC_TEST");
117         sdncRegisterInfo.setPassword("123987");
118         sdncRegisterInfo.setProductName("thirdparty SDNC");
119         sdncRegisterInfo.setProtocol("protocol");
120         sdncRegisterInfo.setThirdpartySdncId("123456");
121         sdncRegisterInfo.setType("SDNC");
122         sdncRegisterInfo.setUrl("http://ip:8000");
123         sdncRegisterInfo.setUserName("nancy");
124         sdncRegisterInfo.setVendor("zte");
125         sdncRegisterInfo.setVersion("v1");
126         sdncList.add(sdncRegisterInfo);
127         String sdncDetail = "{\"thirdparty-sdnc-id\":\"123456\",\"location\":\"edge\","
128                 + "\"product-name\":\"thirdparty SDNC\",\"esr-system-info-list\":{\"esr-system-info\":"
129                 + "[{\"esr-system-info-id\":\"987654\",\"system-name\":\"SDNC_TEST\",\"type\":\"SDNC\","
130                 + "\"vendor\":\"zte\",\"version\":\"v1\",\"service-url\":\"http://ip:8000\","
131                 + "\"user-name\":\"nancy\",\"password\":\"123987\",\"system-type\":\"thirdparty_SDNC\","
132                 + "\"protocol\":\"protocol\"}]}}";
133         String sdncListStr =
134                 "{\"esr-thirdparty-sdnc\": [{\"thirdparty-sdnc-id\": \"123456\",\"location\": \"edge\","
135                         + "\"product-name\": \"thirdparty SDNC\",\"resource-version\": \"1\"}]}";
136         ExternalSystemProxy mockExternalSystemProxy = Mockito.mock(ExternalSystemProxy.class);
137         Mockito.when(mockExternalSystemProxy.queryThirdpartySdncDetail(Mockito.anyString())).thenReturn(sdncDetail);
138         Mockito.when(mockExternalSystemProxy.querySdncList()).thenReturn(sdncListStr);
139         ThirdpartySdncWrapper thirdpartySdncWrapper = new ThirdpartySdncWrapper(mockExternalSystemProxy);
140         Response response = thirdpartySdncWrapper.queryThirdpartySdncList();
141         if (response != null) {
142             Assert.assertTrue(response.getStatus() == 200);
143             assertEquals(extsysUtil.objectToString(sdncList), extsysUtil.objectToString(response.getEntity()));
144         }
145     }
146
147     @Test
148     public void test_updateThirdpartySdnc() throws ExtsysException {
149         ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
150         sdncRegisterInfo.setLocation("edge");
151         sdncRegisterInfo.setName("SDNC_TEST");
152         sdncRegisterInfo.setPassword("123987");
153         sdncRegisterInfo.setProductName("thirdparty SDNC");
154         sdncRegisterInfo.setProtocol("protocol");
155         sdncRegisterInfo.setThirdpartySdncId("123456");
156         sdncRegisterInfo.setType("SDNC");
157         sdncRegisterInfo.setUrl("http://ip:8000");
158         sdncRegisterInfo.setUserName("nancy");
159         sdncRegisterInfo.setVendor("zte");
160         sdncRegisterInfo.setVersion("v1");
161         String sdncDetail = "{\"thirdparty-sdnc-id\":\"123456\",\"location\":\"edge\","
162                 + "\"product-name\":\"thirdparty SDNC\",\"esr-system-info-list\":{\"esr-system-info\":"
163                 + "[{\"esr-system-info-id\":\"987654\",\"system-name\":\"SDNC_TEST\",\"type\":\"SDNC\","
164                 + "\"vendor\":\"zte\",\"version\":\"v1\",\"service-url\":\"http://ip:8000\","
165                 + "\"user-name\":\"nancy\",\"password\":\"123987\",\"system-type\":\"thirdparty_SDNC\","
166                 + "\"protocol\":\"protocol\"}]}}";
167         ExternalSystemProxy mockExternalSystemProxy = Mockito.mock(ExternalSystemProxy.class);
168         Mockito.when(mockExternalSystemProxy.queryThirdpartySdncDetail(Mockito.anyString())).thenReturn(sdncDetail);
169         Mockito.doNothing().when(mockExternalSystemProxy).registerSdnc(Mockito.anyString(), (EsrThirdpartySdncDetail)Mockito.anyObject());
170         ThirdpartySdncWrapper thirdpartySdncWrapper = new ThirdpartySdncWrapper(mockExternalSystemProxy);
171         Response response = thirdpartySdncWrapper.updateThirdpartySdnc(sdncRegisterInfo, "123456");
172         if (response != null) {
173             Assert.assertTrue(response.getStatus() == 200);
174         }
175     }
176 }