2 * Copyright 2017 ZTE Corporation.
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.
16 package org.onap.workflow.activitiext.restservicetask;
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.MatcherAssert.assertThat;
21 import org.apache.commons.httpclient.HttpMethodBase;
22 import org.apache.commons.httpclient.NameValuePair;
23 import org.apache.commons.httpclient.methods.GetMethod;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.Assert;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mockito;
29 import org.onap.workflow.activitiext.common.RestInfo;
30 import org.powermock.api.mockito.PowerMockito;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
34 @PrepareForTest({LowLevelRestApi.class})
35 @RunWith(PowerMockRunner.class)
36 public class HighLevelRestApiTest {
38 private HighLevelRestApi highLevelRestApi;
42 highLevelRestApi = new HighLevelRestApi();
46 public void testInvoke() {
49 RestInfo info = new RestInfo();
50 info.setMethod("GET");
52 info.setPath("/catalog/v1");
55 info.setVersion("v1");
57 HttpResponseMessage msg = new HttpResponseMessage();
58 msg.setStatusCode(200);
59 msg.setResponseBody(null);
60 PowerMockito.mockStatic(LowLevelRestApi.class);
61 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
63 assertThat(HighLevelRestApi.invoke(info), is(msg));;
64 } catch (Exception e) {
70 public void testGet() {
73 HttpResponseMessage msg = new HttpResponseMessage();
74 msg.setStatusCode(200);
75 msg.setResponseBody(null);
76 PowerMockito.mockStatic(LowLevelRestApi.class);
77 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
79 String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5";
80 String acceptValue = "application/json";
81 String contentTypeValue = "application/json";
83 assertThat(HighLevelRestApi.Get(uri, acceptValue, contentTypeValue), is(msg));
84 } catch (Exception e) {
90 public void testPost() {
93 HttpResponseMessage msg = new HttpResponseMessage();
94 msg.setStatusCode(200);
95 msg.setResponseBody(null);
96 PowerMockito.mockStatic(LowLevelRestApi.class);
97 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
99 String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5";
100 String requestBody = "{'type':'NFAR'}";
101 String acceptValue = "application/json";
102 String contentTypeValue = "application/json";
104 assertThat(HighLevelRestApi.Post(uri, requestBody, acceptValue, contentTypeValue), is(msg));
105 } catch (Exception e) {
111 public void testDelete() {
114 HttpResponseMessage msg = new HttpResponseMessage();
115 msg.setStatusCode(200);
116 msg.setResponseBody(null);
117 PowerMockito.mockStatic(LowLevelRestApi.class);
118 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
120 String uri = "10.74.148.107/openoapi/catalog/v1/csars/aa1bc611c9fbc08247d5ea71fd67ec3f";
121 String acceptValue = "application/json";
122 String contentTypeValue = "application/json";
124 assertThat(HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue), is(msg));
125 } catch (Exception e) {
131 public void testPut() {
134 HttpResponseMessage msg = new HttpResponseMessage();
135 msg.setStatusCode(200);
136 msg.setResponseBody(null);
137 PowerMockito.mockStatic(LowLevelRestApi.class);
138 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
140 String uri = "10.74.148.107/openoapi/catalog/v1/csars";
141 String requestBody = "{'type':'NFAR'}";
142 String acceptValue = "application/json";
143 String contentTypeValue = "application/json";
145 assertThat(HighLevelRestApi.Put(uri, requestBody, acceptValue, contentTypeValue), is(msg));
146 } catch (Exception e) {
151 @SuppressWarnings("static-access")
153 public void testCreateNameValuePairArrayFromQuery() {
154 NameValuePair[] pair = new NameValuePair[2];
155 NameValuePair p1 = new NameValuePair();
157 p1.setValue("liuyao");
158 NameValuePair p2 = new NameValuePair();
165 String query = "name=liuyao&pwd=zte";
167 assertThat(highLevelRestApi.createNameValuePairArrayFromQuery(query), is(pair));
171 public void testSetAcceptHeader() {
173 HttpMethodBase method = new GetMethod();
174 HighLevelRestApi.setAcceptHeader(method, "[application/json]");
175 Assert.assertTrue(true);
176 } catch (Exception e) {
177 Assert.assertTrue(false);
182 public void testSetContentTypeHeader() {
185 HttpMethodBase method = new GetMethod();
186 HighLevelRestApi.setContentTypeHeader(method, "[application/json]");
187 Assert.assertTrue(true);
188 } catch (Exception e) {
189 Assert.assertTrue(false);