7e791f75945d23e75c6d30b2cc0c4ea0a192c79a
[vfc/nfvo/wfengine.git] / activiti-extension / src / test / java / org / onap / workflow / activitiext / restservicetask / HighLevelRestApiTest.java
1 /*
2  * Copyright 2017 ZTE Corporation.
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 package org.onap.workflow.activitiext.restservicetask;
17
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.MatcherAssert.assertThat;
20
21 import org.apache.commons.httpclient.NameValuePair;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mockito;
26 import org.powermock.api.mockito.PowerMockito;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.junit4.PowerMockRunner;
29
30 @PrepareForTest({HighLevelRestApi.class,LowLevelRestApi.class})
31 @RunWith(PowerMockRunner.class)
32 public class HighLevelRestApiTest {
33         
34         private HighLevelRestApi highLevelRestApi;
35         
36         @Before
37         public void setUp() {
38                 highLevelRestApi = new HighLevelRestApi();
39         }
40
41         @Test
42         public void testGet() {
43
44                 try {
45                         HttpResponseMessage msg = new HttpResponseMessage();
46                         msg.setStatusCode(200);
47                         msg.setResponseBody(null);
48                         PowerMockito.mockStatic(LowLevelRestApi.class);
49                         PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
50
51                         String uri = "10.74.148.107/openoapi/catalog/v1/csars";
52                         String acceptValue = "application/json";
53                         String contentTypeValue = "application/json";
54                         
55                         HighLevelRestApi.Get(uri, acceptValue, contentTypeValue);
56                         assertThat(HighLevelRestApi.Get(uri, acceptValue, contentTypeValue), is(msg));
57                 } catch (Exception e) {
58                         // TODO Auto-generated catch block
59                         e.printStackTrace();
60                 }
61         }
62
63         @Test
64         public void testPost() {
65
66                 try {
67                         HttpResponseMessage msg = new HttpResponseMessage();
68                         msg.setStatusCode(200);
69                         msg.setResponseBody(null);
70                         PowerMockito.mockStatic(LowLevelRestApi.class);
71                         PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
72
73                         String uri = "10.74.148.107/openoapi/catalog/v1/csars";
74                         String requestBody = "{'type':'NFAR'}";
75                         String acceptValue = "application/json";
76                         String contentTypeValue = "application/json";
77
78                         assertThat(HighLevelRestApi.Post(uri, requestBody, acceptValue, contentTypeValue), is(msg));
79                 } catch (Exception e) {
80                         // TODO Auto-generated catch block
81                         e.printStackTrace();
82                 }
83         }
84
85         @Test
86         public void testDelete() {
87
88                 try {
89                         HttpResponseMessage msg = new HttpResponseMessage();
90                         msg.setStatusCode(200);
91                         msg.setResponseBody(null);
92                         PowerMockito.mockStatic(LowLevelRestApi.class);
93                         PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
94
95                         String uri = "10.74.148.107/openoapi/catalog/v1/csars/aa1bc611c9fbc08247d5ea71fd67ec3f";
96                         String acceptValue = "application/json";
97                         String contentTypeValue = "application/json";
98
99                         assertThat(HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue), is(msg));
100                 } catch (Exception e) {
101                         // TODO Auto-generated catch block
102                         e.printStackTrace();
103                 }
104         }
105
106         @Test
107         public void testPut() {
108
109                 try {
110                         HttpResponseMessage msg = new HttpResponseMessage();
111                         msg.setStatusCode(200);
112                         msg.setResponseBody(null);
113                         PowerMockito.mockStatic(LowLevelRestApi.class);
114                         PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
115
116                         String uri = "10.74.148.107/openoapi/catalog/v1/csars";
117                         String requestBody = "{'type':'NFAR'}";
118                         String acceptValue = "application/json";
119                         String contentTypeValue = "application/json";
120
121                         assertThat(HighLevelRestApi.Put(uri, requestBody, acceptValue, contentTypeValue), is(msg));
122                 } catch (Exception e) {
123                         // TODO Auto-generated catch block
124                         e.printStackTrace();
125                 }
126         }
127
128         @Test
129         public void testCreateNameValuePairArrayFromQuery() {
130                 NameValuePair[] pair = new NameValuePair[2];
131                 NameValuePair p1 = new NameValuePair();
132                 p1.setName("name");
133                 p1.setValue("liuyao");
134                 NameValuePair p2 = new NameValuePair();
135                 p2.setName("pwd");
136                 p2.setValue("zte");
137
138                 pair[0] = p1;
139                 pair[1] = p2;
140
141                 String query = "name=liuyao&pwd=zte";
142
143                 assertThat(highLevelRestApi.createNameValuePairArrayFromQuery(query), is(pair));
144         }
145         
146 }