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 java.io.IOException;
24 import javax.servlet.ServletInputStream;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
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;
38 import net.sf.json.JSONObject;
39 @RunWith(MockitoJUnitRunner.class)
40 public class AuthRoaTest {
42 private AuthRoa authRoa;
44 private AuthMgr authMgr;
48 authRoa = new AuthRoa();
49 authMgr = new AuthMgr();
50 authRoa.setAuthMgr(authMgr);
54 HttpServletRequest context;
57 HttpServletResponse resp;
60 ServletInputStream servletInputStream;
63 public void tearDown() {
69 public void testAuthTokenBySubJsonObjectNull() {
70 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
71 HttpServletRequest mockInstance = proxyStub.getMockInstance();
72 // new MockUp<VnfmJsonUtil>() {
75 // public <T> T getJsonFromContexts(HttpServletRequest context) {
80 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
81 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
83 String result = authRoa.authToken(mockInstance, mockResInstance);
85 assertEquals("Login params insufficient", result);
89 public void testAuthTokenByFail() {
90 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
91 HttpServletRequest mockInstance = proxyStub.getMockInstance();
93 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
94 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
96 // new MockUp<VnfmJsonUtil>() {
98 // @SuppressWarnings("unchecked")
100 // public <T> T getJsonFromContexts(HttpServletRequest context) {
101 // JSONObject subJsonObject = new JSONObject();
102 // return (T)subJsonObject;
105 // new MockUp<AuthMgr>() {
108 // public JSONObject authToken(JSONObject params) {
109 // JSONObject restJson = new JSONObject();
110 // restJson.put("retCode", Constant.REST_FAIL);
111 // restJson.put("data", "Fail!");
115 String result = authRoa.authToken(mockInstance, mockResInstance);
117 assertEquals("{\"Information\": \"Fail!\"}", result);
121 public void testAuthTokenByHttpInnerError() {
122 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
123 HttpServletRequest mockInstance = proxyStub.getMockInstance();
125 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
126 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
128 // new MockUp<VnfmJsonUtil>() {
130 // @SuppressWarnings("unchecked")
132 // public <T> T getJsonFromContexts(HttpServletRequest context) {
133 // JSONObject subJsonObject = new JSONObject();
134 // return (T)subJsonObject;
137 // new MockUp<AuthMgr>() {
140 // public JSONObject authToken(JSONObject params) {
141 // JSONObject restJson = new JSONObject();
142 // restJson.put("retCode", Constant.HTTP_INNERERROR);
143 // restJson.put("data", "HttpInnerError!");
147 String result = authRoa.authToken(mockInstance, mockResInstance);
149 assertEquals("{\"Information\": \"HttpInnerError!\"}", result);
153 public void testAuthToken() throws Exception {
154 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
155 // HttpServletRequest mockInstance = proxyStub.getMockInstance();
157 // MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
158 // HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
159 // new MockUp<VnfmJsonUtil>() {
161 // @SuppressWarnings("unchecked")
163 // public <T> T getJsonFromContexts(HttpServletRequest context) {
164 // JSONObject subJsonObject = new JSONObject();
165 // return (T)subJsonObject;
168 // new MockUp<AuthMgr>() {
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);
182 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
183 String result = authRoa.authToken(context, resp);
185 assertNotNull(result);
189 public void testDelAuthToken() {
190 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
192 // MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
193 // HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
194 String result = authRoa.delAuthToken(null, null, resp);
196 JSONObject resultJson = new JSONObject();
197 resultJson.put("Information", "Operation success");
198 assertEquals(resultJson.toString(), result);
202 public void testShakehand() {
203 String result = authRoa.shakehand("roattr", resp);
205 JSONObject resultJson = new JSONObject();
206 resultJson.put("status", "running");
207 resultJson.put("description", "Operation success");
208 assertEquals(resultJson.toString(), result);
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);