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.onap.vfc.nfvo.vnfm.svnfm.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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmJsonUtil;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.AuthMgr;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.rest.AuthRoa;
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);
172 "{\"token\": {\"methods\": [\"password\"],\"expires_at\": \"\",\"user\": {\"id\": \"userName\",\"name\": \"userName\"},\"roa_rand\": \"roaRand\"}}",
177 public void testDelAuthToken() {
178 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
179 HttpServletRequest mockInstance = proxyStub.getMockInstance();
181 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
182 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
183 String result = authRoa.delAuthToken(mockInstance, null, null, mockResInstance);
185 JSONObject resultJson = new JSONObject();
186 resultJson.put("Information", "Operation success");
187 assertEquals(resultJson.toString(), result);
191 public void testShakehand() {
192 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
193 HttpServletRequest mockInstance = proxyStub.getMockInstance();
195 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
196 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
197 String result = authRoa.shakehand(mockInstance, null, mockResInstance);
199 JSONObject resultJson = new JSONObject();
200 resultJson.put("status", "running");
201 resultJson.put("description", "Operation success");
202 assertEquals(resultJson.toString(), result);