2 * Copyright 2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.onap.workflow.activitiext.restservicetask;
\r
18 import static org.hamcrest.CoreMatchers.is;
\r
19 import static org.hamcrest.MatcherAssert.assertThat;
\r
21 import org.apache.commons.httpclient.HttpMethodBase;
\r
22 import org.apache.commons.httpclient.NameValuePair;
\r
23 import org.apache.commons.httpclient.methods.GetMethod;
\r
24 import org.junit.Assert;
\r
25 import org.junit.Test;
\r
26 import org.junit.runner.RunWith;
\r
27 import org.mockito.Mockito;
\r
28 import org.onap.workflow.activitiext.common.RestInfo;
\r
29 import org.powermock.api.mockito.PowerMockito;
\r
30 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
31 import org.powermock.modules.junit4.PowerMockRunner;
\r
33 @PrepareForTest({LowLevelRestApi.class})
\r
34 @RunWith(PowerMockRunner.class)
\r
35 public class HighLevelRestApiTest {
\r
38 public void testInvoke() {
\r
41 RestInfo info = new RestInfo();
\r
42 info.setMethod("GET");
\r
43 info.setName("name");
\r
44 info.setPath("/catalog/v1");
\r
45 info.setUrl("csars");
\r
46 info.setRealUri("");
\r
47 info.setVersion("v1");
\r
49 HttpResponseMessage msg = new HttpResponseMessage();
\r
50 msg.setStatusCode(200);
\r
51 msg.setResponseBody(null);
\r
52 PowerMockito.mockStatic(LowLevelRestApi.class);
\r
53 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
\r
55 assertThat(HighLevelRestApi.invoke(info), is(msg));;
\r
56 } catch (Exception e) {
\r
62 public void testGet() {
\r
65 HttpResponseMessage msg = new HttpResponseMessage();
\r
66 msg.setStatusCode(200);
\r
67 msg.setResponseBody(null);
\r
68 PowerMockito.mockStatic(LowLevelRestApi.class);
\r
69 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
\r
71 String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5";
\r
72 String acceptValue = "application/json";
\r
73 String contentTypeValue = "application/json";
\r
75 assertThat(HighLevelRestApi.Get(uri, acceptValue, contentTypeValue), is(msg));
\r
76 } catch (Exception e) {
\r
82 public void testPost() {
\r
85 HttpResponseMessage msg = new HttpResponseMessage();
\r
86 msg.setStatusCode(200);
\r
87 msg.setResponseBody(null);
\r
88 PowerMockito.mockStatic(LowLevelRestApi.class);
\r
89 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
\r
91 String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5";
\r
92 String requestBody = "{'type':'NFAR'}";
\r
93 String acceptValue = "application/json";
\r
94 String contentTypeValue = "application/json";
\r
96 assertThat(HighLevelRestApi.Post(uri, requestBody, acceptValue, contentTypeValue), is(msg));
\r
97 } catch (Exception e) {
\r
103 public void testDelete() {
\r
106 HttpResponseMessage msg = new HttpResponseMessage();
\r
107 msg.setStatusCode(200);
\r
108 msg.setResponseBody(null);
\r
109 PowerMockito.mockStatic(LowLevelRestApi.class);
\r
110 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
\r
112 String uri = "10.74.148.107/openoapi/catalog/v1/csars/aa1bc611c9fbc08247d5ea71fd67ec3f";
\r
113 String acceptValue = "application/json";
\r
114 String contentTypeValue = "application/json";
\r
116 assertThat(HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue), is(msg));
\r
117 } catch (Exception e) {
\r
123 public void testPut() {
\r
126 HttpResponseMessage msg = new HttpResponseMessage();
\r
127 msg.setStatusCode(200);
\r
128 msg.setResponseBody(null);
\r
129 PowerMockito.mockStatic(LowLevelRestApi.class);
\r
130 PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
\r
132 String uri = "10.74.148.107/openoapi/catalog/v1/csars";
\r
133 String requestBody = "{'type':'NFAR'}";
\r
134 String acceptValue = "application/json";
\r
135 String contentTypeValue = "application/json";
\r
137 assertThat(HighLevelRestApi.Put(uri, requestBody, acceptValue, contentTypeValue), is(msg));
\r
138 } catch (Exception e) {
\r
143 @SuppressWarnings("static-access")
\r
145 public void testCreateNameValuePairArrayFromQuery() {
\r
146 NameValuePair[] pair = new NameValuePair[2];
\r
147 NameValuePair p1 = new NameValuePair();
\r
148 p1.setName("name");
\r
149 p1.setValue("liuyao");
\r
150 NameValuePair p2 = new NameValuePair();
\r
152 p2.setValue("zte");
\r
157 String query = "name=liuyao&pwd=zte";
\r
159 assertThat(HighLevelRestApi.createNameValuePairArrayFromQuery(query), is(pair));
\r
163 public void testSetAcceptHeader() {
\r
165 HttpMethodBase method = new GetMethod();
\r
166 HighLevelRestApi.setAcceptHeader(method, "[application/json]");
\r
167 Assert.assertTrue(true);
\r
168 } catch (Exception e) {
\r
169 Assert.assertTrue(false);
\r
174 public void testSetContentTypeHeader() {
\r
177 HttpMethodBase method = new GetMethod();
\r
178 HighLevelRestApi.setContentTypeHeader(method, "[application/json]");
\r
179 Assert.assertTrue(true);
\r
180 } catch (Exception e) {
\r
181 Assert.assertTrue(false);
\r