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