2 * Copyright 2016-2017 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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.rest;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
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;
34 import net.sf.json.JSONObject;
36 public class AuthRoaTest {
38 private AuthRoa authRoa;
40 private AuthMgr authMgr;
44 authRoa = new AuthRoa();
45 authMgr = new AuthMgr();
46 authRoa.setAuthMgr(authMgr);
50 public void tearDown() {
56 public void testAuthTokenBySubJsonObjectNull() {
57 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
58 HttpServletRequest mockInstance = proxyStub.getMockInstance();
59 new MockUp<VnfmJsonUtil>() {
62 public <T> T getJsonFromContexts(HttpServletRequest context) {
67 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
68 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
70 String result = authRoa.authToken(mockInstance, mockResInstance);
72 assertEquals("Login params insufficient", result);
76 public void testAuthTokenByFail() {
77 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
78 HttpServletRequest mockInstance = proxyStub.getMockInstance();
80 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
81 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
83 new MockUp<VnfmJsonUtil>() {
85 @SuppressWarnings("unchecked")
87 public <T> T getJsonFromContexts(HttpServletRequest context) {
88 JSONObject subJsonObject = new JSONObject();
89 return (T)subJsonObject;
92 new MockUp<AuthMgr>() {
95 public JSONObject authToken(JSONObject params) {
96 JSONObject restJson = new JSONObject();
97 restJson.put("retCode", Constant.REST_FAIL);
98 restJson.put("data", "Fail!");
102 String result = authRoa.authToken(mockInstance, mockResInstance);
104 assertEquals("{\"Information\": \"Fail!\"}", result);
108 public void testAuthTokenByHttpInnerError() {
109 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
110 HttpServletRequest mockInstance = proxyStub.getMockInstance();
112 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
113 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
115 new MockUp<VnfmJsonUtil>() {
117 @SuppressWarnings("unchecked")
119 public <T> T getJsonFromContexts(HttpServletRequest context) {
120 JSONObject subJsonObject = new JSONObject();
121 return (T)subJsonObject;
124 new MockUp<AuthMgr>() {
127 public JSONObject authToken(JSONObject params) {
128 JSONObject restJson = new JSONObject();
129 restJson.put("retCode", Constant.HTTP_INNERERROR);
130 restJson.put("data", "HttpInnerError!");
134 String result = authRoa.authToken(mockInstance, mockResInstance);
136 assertEquals("{\"Information\": \"HttpInnerError!\"}", result);
140 public void testAuthToken() {
141 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
142 HttpServletRequest mockInstance = proxyStub.getMockInstance();
144 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
145 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
146 new MockUp<VnfmJsonUtil>() {
148 @SuppressWarnings("unchecked")
150 public <T> T getJsonFromContexts(HttpServletRequest context) {
151 JSONObject subJsonObject = new JSONObject();
152 return (T)subJsonObject;
155 new MockUp<AuthMgr>() {
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);
169 String result = authRoa.authToken(mockInstance, mockResInstance);
171 assertNotNull(result);
175 public void testDelAuthToken() {
176 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
178 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
179 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
180 String result = authRoa.delAuthToken(null, null, mockResInstance);
182 JSONObject resultJson = new JSONObject();
183 resultJson.put("Information", "Operation success");
184 assertEquals(resultJson.toString(), result);
188 public void testShakehand() {
189 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
191 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
192 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
193 String result = authRoa.shakehand(null, mockResInstance);
195 JSONObject resultJson = new JSONObject();
196 resultJson.put("status", "running");
197 resultJson.put("description", "Operation success");
198 assertEquals(resultJson.toString(), result);