2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.vnfmadapter.service.rest;
19 import static org.junit.Assert.assertEquals;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
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;
33 import net.sf.json.JSONObject;
35 public class AuthRoaTest {
37 private AuthRoa authRoa;
39 private AuthMgr authMgr;
43 authRoa = new AuthRoa();
44 authMgr = new AuthMgr();
45 authRoa.setAuthMgr(authMgr);
49 public void tearDown() {
55 public void testAuthTokenBySubJsonObjectNull() {
56 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
57 HttpServletRequest mockInstance = proxyStub.getMockInstance();
58 new MockUp<VnfmJsonUtil>() {
61 public <T> T getJsonFromContexts(HttpServletRequest context) {
66 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
67 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
69 String result = authRoa.authToken(mockInstance, mockResInstance);
71 assertEquals("Login params insufficient", result);
75 public void testAuthTokenByFail() {
76 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
77 HttpServletRequest mockInstance = proxyStub.getMockInstance();
79 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
80 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
82 new MockUp<VnfmJsonUtil>() {
84 @SuppressWarnings("unchecked")
86 public <T> T getJsonFromContexts(HttpServletRequest context) {
87 JSONObject subJsonObject = new JSONObject();
88 return (T)subJsonObject;
91 new MockUp<AuthMgr>() {
94 public JSONObject authToken(JSONObject params) {
95 JSONObject restJson = new JSONObject();
96 restJson.put("retCode", Constant.REST_FAIL);
97 restJson.put("data", "Fail!");
101 String result = authRoa.authToken(mockInstance, mockResInstance);
103 assertEquals("{\"Information\": \"Fail!\"}", result);
107 public void testAuthTokenByHttpInnerError() {
108 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
109 HttpServletRequest mockInstance = proxyStub.getMockInstance();
111 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
112 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
114 new MockUp<VnfmJsonUtil>() {
116 @SuppressWarnings("unchecked")
118 public <T> T getJsonFromContexts(HttpServletRequest context) {
119 JSONObject subJsonObject = new JSONObject();
120 return (T)subJsonObject;
123 new MockUp<AuthMgr>() {
126 public JSONObject authToken(JSONObject params) {
127 JSONObject restJson = new JSONObject();
128 restJson.put("retCode", Constant.HTTP_INNERERROR);
129 restJson.put("data", "HttpInnerError!");
133 String result = authRoa.authToken(mockInstance, mockResInstance);
135 assertEquals("{\"Information\": \"HttpInnerError!\"}", result);
139 public void testAuthToken() {
140 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
141 HttpServletRequest mockInstance = proxyStub.getMockInstance();
143 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
144 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
145 new MockUp<VnfmJsonUtil>() {
147 @SuppressWarnings("unchecked")
149 public <T> T getJsonFromContexts(HttpServletRequest context) {
150 JSONObject subJsonObject = new JSONObject();
151 return (T)subJsonObject;
154 new MockUp<AuthMgr>() {
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);
168 String result = authRoa.authToken(mockInstance, mockResInstance);
171 "{\"token\": {\"methods\": [\"password\"],\"expires_at\": \"\",\"user\": {\"id\": \"userName\",\"name\": \"userName\"},\"roa_rand\": \"roaRand\"}}",
176 public void testDelAuthToken() {
177 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
178 HttpServletRequest mockInstance = proxyStub.getMockInstance();
180 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
181 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
182 String result = authRoa.delAuthToken(mockInstance, null, null, mockResInstance);
184 JSONObject resultJson = new JSONObject();
185 resultJson.put("Information", "Operation success");
186 assertEquals(resultJson.toString(), result);
190 public void testShakehand() {
191 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
192 HttpServletRequest mockInstance = proxyStub.getMockInstance();
194 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
195 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
196 String result = authRoa.shakehand(mockInstance, null, mockResInstance);
198 JSONObject resultJson = new JSONObject();
199 resultJson.put("status", "running");
200 resultJson.put("description", "Operation success");
201 assertEquals(resultJson.toString(), result);