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