Junit coverage increased
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / rest / AuthRoaTest.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.rest;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmJsonUtil;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.AuthMgr;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 public class AuthRoaTest {
37
38     private AuthRoa authRoa;
39
40     private AuthMgr authMgr;
41
42     @Before
43     public void setUp() {
44         authRoa = new AuthRoa();
45         authMgr = new AuthMgr();
46         authRoa.setAuthMgr(authMgr);
47     }
48
49     @After
50     public void tearDown() {
51         authRoa = null;
52         authMgr = null;
53     }
54
55     @Test
56     public void testAuthTokenBySubJsonObjectNull() {
57         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
58         HttpServletRequest mockInstance = proxyStub.getMockInstance();
59         new MockUp<VnfmJsonUtil>() {
60
61             @Mock
62             public <T> T getJsonFromContexts(HttpServletRequest context) {
63                 return null;
64             }
65         };
66
67         MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
68         HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
69
70         String result = authRoa.authToken(mockInstance, mockResInstance);
71
72         assertEquals("Login params insufficient", result);
73     }
74
75     @Test
76     public void testAuthTokenByFail() {
77         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
78         HttpServletRequest mockInstance = proxyStub.getMockInstance();
79
80         MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
81         HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
82
83         new MockUp<VnfmJsonUtil>() {
84
85             @SuppressWarnings("unchecked")
86             @Mock
87             public <T> T getJsonFromContexts(HttpServletRequest context) {
88                 JSONObject subJsonObject = new JSONObject();
89                 return (T)subJsonObject;
90             }
91         };
92         new MockUp<AuthMgr>() {
93
94             @Mock
95             public JSONObject authToken(JSONObject params) {
96                 JSONObject restJson = new JSONObject();
97                 restJson.put("retCode", Constant.REST_FAIL);
98                 restJson.put("data", "Fail!");
99                 return restJson;
100             }
101         };
102         String result = authRoa.authToken(mockInstance, mockResInstance);
103
104         assertEquals("{\"Information\": \"Fail!\"}", result);
105     }
106
107     @Test
108     public void testAuthTokenByHttpInnerError() {
109         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
110         HttpServletRequest mockInstance = proxyStub.getMockInstance();
111
112         MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
113         HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
114
115         new MockUp<VnfmJsonUtil>() {
116
117             @SuppressWarnings("unchecked")
118             @Mock
119             public <T> T getJsonFromContexts(HttpServletRequest context) {
120                 JSONObject subJsonObject = new JSONObject();
121                 return (T)subJsonObject;
122             }
123         };
124         new MockUp<AuthMgr>() {
125
126             @Mock
127             public JSONObject authToken(JSONObject params) {
128                 JSONObject restJson = new JSONObject();
129                 restJson.put("retCode", Constant.HTTP_INNERERROR);
130                 restJson.put("data", "HttpInnerError!");
131                 return restJson;
132             }
133         };
134         String result = authRoa.authToken(mockInstance, mockResInstance);
135
136         assertEquals("{\"Information\": \"HttpInnerError!\"}", result);
137     }
138
139     @Test
140     public void testAuthToken() {
141         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
142         HttpServletRequest mockInstance = proxyStub.getMockInstance();
143
144         MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
145         HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
146         new MockUp<VnfmJsonUtil>() {
147
148             @SuppressWarnings("unchecked")
149             @Mock
150             public <T> T getJsonFromContexts(HttpServletRequest context) {
151                 JSONObject subJsonObject = new JSONObject();
152                 return (T)subJsonObject;
153             }
154         };
155         new MockUp<AuthMgr>() {
156
157             @Mock
158             public JSONObject authToken(JSONObject params) {
159                 JSONObject restJson = new JSONObject();
160                 restJson.put("retCode", Constant.REST_SUCCESS);
161                 JSONObject data = new JSONObject();
162                 data.put("accessSession", "accessSession");
163                 data.put("userName", "userName");
164                 data.put("roaRand", "roaRand");
165                 restJson.put("data", data);
166                 return restJson;
167             }
168         };
169         String result = authRoa.authToken(mockInstance, mockResInstance);
170
171         assertNotNull(result);
172     }
173
174     @Test
175     public void testDelAuthToken() {
176         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
177
178         MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
179         HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
180         String result = authRoa.delAuthToken(null, null, mockResInstance);
181
182         JSONObject resultJson = new JSONObject();
183         resultJson.put("Information", "Operation success");
184         assertEquals(resultJson.toString(), result);
185     }
186
187     @Test
188     public void testShakehand() {
189         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
190
191         MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
192         HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
193         String result = authRoa.shakehand(null, mockResInstance);
194
195         JSONObject resultJson = new JSONObject();
196         resultJson.put("status", "running");
197         resultJson.put("description", "Operation success");
198         assertEquals(resultJson.toString(), result);
199     }
200 }