CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / CommonLibrary / rest-client / src / test / java / org / openo / baseservice / roa / util / clientsdk / TestRestClientUtil.java
1 /*
2  * Copyright (c) 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.openo.baseservice.roa.util.clientsdk;
18
19 import org.openo.baseservice.remoteservice.exception.ServiceException;
20 import org.openo.baseservice.roa.util.restclient.Restful;
21 import org.openo.baseservice.roa.util.restclient.RestfulAsyncCallback;
22 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
23 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
24
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32
33 import java.util.Arrays;
34 import java.util.Date;
35
36 import junit.framework.Assert;
37 import mockit.Expectations;
38 import mockit.Mocked;
39
40 /**
41  * <br/>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version SDNO 0.5 13-Jun-2016
47  */
48 public class TestRestClientUtil {
49
50     @Mocked
51     Restful restFullMock;
52
53     ExpectedException thrown = ExpectedException.none();;
54
55     /**
56      * <br/>
57      * 
58      * @throws java.lang.Exception
59      * @since SDNO 0.5
60      */
61     @BeforeClass
62     public static void setUpBeforeClass() throws Exception {
63     }
64
65     /**
66      * <br/>
67      * 
68      * @throws java.lang.Exception
69      * @since SDNO 0.5
70      */
71     @AfterClass
72     public static void tearDownAfterClass() throws Exception {
73     }
74
75     /**
76      * <br/>
77      * 
78      * @throws java.lang.Exception
79      * @since SDNO 0.5
80      */
81     @Before
82     public void setUp() throws Exception {
83     }
84
85     /**
86      * <br/>
87      * 
88      * @throws java.lang.Exception
89      * @since SDNO 0.5
90      */
91     @After
92     public void tearDown() throws Exception {
93     }
94
95     /**
96      * <br/>
97      * 
98      * @throws ServiceException
99      * @since SDNO 0.5
100      */
101     @Ignore
102     @Test
103     public void testInvokeMethod() throws ServiceException {
104         final String path = "test/path";
105         final RestfulParametes parameters = null;
106         final RestfulResponse expected = new RestfulResponse();
107         expected.setStatus(200);
108
109         new Expectations() {
110
111             {
112                 restFullMock.get(path, parameters);
113                 returns(expected);
114
115                 restFullMock.post(path, parameters);
116                 returns(expected);
117
118                 restFullMock.patch(path, parameters);
119                 returns(expected);
120
121                 restFullMock.delete(path, parameters);
122                 returns(expected);
123
124                 restFullMock.put(path, parameters);
125                 returns(expected);
126             }
127         };
128         RestfulResponse actual = RestClientUtil.invokeMethod("GET", path, parameters, restFullMock);
129         Assert.assertEquals(200, actual.getStatus());
130
131         actual = RestClientUtil.invokeMethod("POST", path, parameters, restFullMock);
132         Assert.assertEquals(200, actual.getStatus());
133
134         actual = RestClientUtil.invokeMethod("PATCH", path, parameters, restFullMock);
135         Assert.assertEquals(200, actual.getStatus());
136
137         actual = RestClientUtil.invokeMethod("DELETE", path, parameters, restFullMock);
138         Assert.assertEquals(200, actual.getStatus());
139
140         actual = RestClientUtil.invokeMethod("PUT", path, parameters, restFullMock);
141         Assert.assertEquals(200, actual.getStatus());
142     }
143
144     @Ignore
145     @Test(expected = ServiceException.class)
146     public void testInvokeMethodException() throws ServiceException {
147         RestClientUtil.invokeMethod("UNKNOWN-METHOD", "some/path", null, restFullMock);
148     }
149
150     /**
151      * <br/>
152      * 
153      * @throws ServiceException
154      * @since SDNO 0.5
155      */
156     @Ignore
157     @Test(expected = ServiceException.class)
158     public void testInvokeAsyncMethod() throws ServiceException {
159         final String path = "test/path";
160         final RestfulParametes parameters = null;
161         final RestfulAsyncCallback callback = null;
162
163         RestClientUtil.invokeAsyncMethod("GET", path, parameters, restFullMock, callback);
164
165         RestClientUtil.invokeAsyncMethod("POST", path, parameters, restFullMock, callback);
166
167         RestClientUtil.invokeAsyncMethod("PATCH", path, parameters, restFullMock, callback);
168
169         RestClientUtil.invokeAsyncMethod("DELETE", path, parameters, restFullMock, callback);
170
171         RestClientUtil.invokeAsyncMethod("PUT", path, parameters, restFullMock, callback);
172
173         RestClientUtil.invokeAsyncMethod("UNKNOWN", path, parameters, restFullMock, callback);
174     }
175
176     /**
177      * <br/>
178      * 
179      * @since SDNO 0.5
180      */
181     @Ignore
182     @Test
183     public void testIsPrimitive() {
184
185         Assert.assertTrue(RestClientUtil.isPrimitive(Integer.class));
186         Assert.assertTrue(RestClientUtil.isPrimitive(Long.class));
187         Assert.assertTrue(RestClientUtil.isPrimitive(Double.class));
188         Assert.assertTrue(RestClientUtil.isPrimitive(Void.class));
189         Assert.assertTrue(RestClientUtil.isPrimitive(String.class));
190         Assert.assertTrue(RestClientUtil.isPrimitive(Boolean.class));
191         Assert.assertTrue(RestClientUtil.isPrimitive(Byte.class));
192         Assert.assertTrue(RestClientUtil.isPrimitive(Character.class));
193         Assert.assertTrue(RestClientUtil.isPrimitive(Short.class));
194         Assert.assertTrue(RestClientUtil.isPrimitive(Float.class));
195
196         Assert.assertTrue(RestClientUtil.isPrimitive(int.class));
197         Assert.assertTrue(RestClientUtil.isPrimitive(long.class));
198         Assert.assertTrue(RestClientUtil.isPrimitive(double.class));
199         Assert.assertTrue(RestClientUtil.isPrimitive(void.class));
200         Assert.assertTrue(RestClientUtil.isPrimitive(boolean.class));
201         Assert.assertTrue(RestClientUtil.isPrimitive(byte.class));
202         Assert.assertTrue(RestClientUtil.isPrimitive(char.class));
203         Assert.assertTrue(RestClientUtil.isPrimitive(short.class));
204         Assert.assertTrue(RestClientUtil.isPrimitive(float.class));
205
206         Assert.assertFalse(RestClientUtil.isPrimitive(Object.class));
207         Assert.assertFalse(RestClientUtil.isPrimitive(Date.class));
208         Assert.assertFalse(RestClientUtil.isPrimitive(Arrays.class));
209     }
210 }