2 * Copyright (c) 2018 Orange and/or its affiliates.
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 io.fd.honeycomb.transportpce.device.test;
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.HttpURLConnection;
23 import java.util.Arrays;
24 import java.util.Base64;
25 import java.util.Collection;
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;
37 * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
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;
48 private static final Logger LOG = LoggerFactory.getLogger(RestAPICallsTest.class);
50 public RestAPICallsTest(String uriRest, String inputXml) {
53 String usernameAndPassword = this.user+":"+this.pass;
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 !");
63 @Parameterized.Parameters
64 public static Collection<Object[]> stringXML() {
65 return Arrays.asList(new Object[][]{
66 {CONFIG_URI,DeviceOperToConfig.operToConfig()}
71 *send PUT request via REST API.
73 * @param uri request url
74 * @return String response
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);
87 OutputStream os = conn.getOutputStream();
88 os.write(input.getBytes());
90 Assert.assertEquals(HttpURLConnection.HTTP_CREATED,conn.getResponseCode());
93 LOG.error("input xml gets is null !");
95 LOG.info("Test Succeed");
99 * send GET request via REST API.
101 * @param uri request url
102 * @return String response
103 * @throws IOException
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"));
115 LOG.info("Test Succeed");
119 *send DELETE request via REST API.
121 * @param uri request url
122 * @return String response
123 * @throws IOException
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());