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