8ad450b161c502ea23d306c33b311dbf62de2931
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / test / java / io / fd / honeycomb / transportpce / device / test / RestAPICallsTest.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
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 io.fd.honeycomb.transportpce.device.test;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.HttpURLConnection;
22 import java.net.URL;
23 import java.util.Arrays;
24 import java.util.Base64;
25 import java.util.Collection;
26
27 import org.junit.Assert;
28 import org.junit.FixMethodOrder;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.MethodSorters;
32 import org.junit.runners.Parameterized;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
38  */
39 @RunWith(Parameterized.class)
40 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
41 public class RestAPICallsTest {
42     private static final String CONFIG_URI="http://localhost:8183/restconf/config/org-openroadm-device:org-openroadm-device";
43     private final String user = "admin";
44     private final String pass = "admin";
45     private String encodeUserPass;
46     private String uri;
47     private String input;
48     private static final Logger LOG = LoggerFactory.getLogger(RestAPICallsTest.class);
49
50     public RestAPICallsTest(String uriRest, String inputXml) {
51         uri = uriRest;
52         input = inputXml;
53         String usernameAndPassword = this.user+":"+this.pass;
54         try {
55             byte[] encodeBytes = Base64.getEncoder().encode(usernameAndPassword.getBytes());
56             this.encodeUserPass = new String(encodeBytes, "UTF-8");
57             LOG.info("encode : {}",encodeUserPass);
58         } catch (UnsupportedEncodingException e) {
59             LOG.error("encode usernameAndPassword failed !");
60         }
61     }
62
63     @Parameterized.Parameters
64     public static Collection<Object[]> stringXML() {
65         return Arrays.asList(new Object[][]{
66                 {CONFIG_URI,DeviceOperToConfig.operToConfig()}
67         });
68     }
69
70     /**
71      *send PUT request via REST API.
72      *
73      * @param uri request url
74      * @return String response
75      * @throws IOException
76      */
77     @Test
78     public void test1Put() throws IOException {
79         URL url = new URL(uri);
80         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
81         conn.setDoOutput(true);
82         conn.setRequestMethod("PUT");
83         conn.setRequestProperty ("Authorization", "Basic " + encodeUserPass);
84         conn.setRequestProperty("Content-Type", "application/xml");
85         LOG.info("input xml : {}",input);
86         if (input != null) {
87             OutputStream os = conn.getOutputStream();
88             os.write(input.getBytes());
89             os.flush();
90             Assert.assertEquals(HttpURLConnection.HTTP_CREATED,conn.getResponseCode());
91             conn.disconnect();
92         } else {
93             LOG.error("input xml gets is null !");
94         }
95         LOG.info("Test Succeed");
96     }
97
98     /**
99      * send GET request via REST API.
100      *
101      * @param uri request url
102      * @return String response
103      * @throws IOException
104      */
105     @Test
106     public void test2Get() throws IOException {
107         URL url = new URL(uri);
108         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
109         conn.setRequestProperty ("Authorization", "Basic " + encodeUserPass);
110         conn.setRequestMethod("GET");
111         conn.setRequestProperty("Accept", "application/json");
112         Assert.assertEquals(HttpURLConnection.HTTP_OK,conn.getResponseCode());
113 //        Assert.assertThat(result, CoreMatchers.containsString("org-openroadm-device"));
114         conn.disconnect();
115         LOG.info("Test Succeed");
116     }
117
118     /**
119      *send DELETE request via REST API.
120      *
121      * @param uri request url
122      * @return String response
123      * @throws IOException
124      */
125     @Test
126     public void test3Delete() throws IOException {
127         URL url = new URL(uri);
128         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
129         conn.setDoOutput(true);
130         conn.setRequestMethod("DELETE");
131         conn.setRequestProperty ("Authorization", "Basic " + encodeUserPass);
132         Assert.assertEquals(HttpURLConnection.HTTP_OK,conn.getResponseCode());
133         conn.disconnect();
134     }
135 }