2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 AMDOCS
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.sdnc.oam.datamigrator.common;
22 import com.github.tomakehurst.wiremock.client.WireMock;
23 import com.github.tomakehurst.wiremock.junit.WireMockRule;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.onap.sdnc.oam.datamigrator.exceptions.RestconfException;
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
35 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
36 import static com.github.tomakehurst.wiremock.client.WireMock.get;
37 import static com.github.tomakehurst.wiremock.client.WireMock.put;
38 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNull;
41 import static org.junit.Assert.assertTrue;
43 public class RestconfClientTest {
46 public WireMockRule service = new WireMockRule(8081);
47 private RestconfClient restconfClient = new RestconfClient("http://localhost:8081","admin","Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U");
48 private ClassLoader classLoader = getClass().getClassLoader();
49 private String preloadVnfResponseJson = new String(Files.readAllBytes(Paths.get(classLoader.getResource("wiremock/preloadVnfResponse.json").toURI())));
50 private String preloadInformationRequestJson = new String(Files.readAllBytes(Paths.get(classLoader.getResource("wiremock/preloadInformationRequest.json").toURI())));
53 JsonObject expectedJsonObject = new JsonParser().parse(preloadVnfResponseJson).getAsJsonObject();
55 public RestconfClientTest() throws IOException, URISyntaxException {
59 public void getPositiveTest() {
60 service.stubFor(get(urlEqualTo("/restconf/config/GENERIC-RESOURCE-API:preload-vnfs"))
61 .willReturn(aResponse().withStatus(200).withBody(preloadVnfResponseJson)));
62 JsonObject actualResponse=null;
64 actualResponse = restconfClient.get("GENERIC-RESOURCE-API:preload-vnfs");
65 } catch (RestconfException e) {
68 assertEquals(expectedJsonObject,actualResponse);
72 public void getNegativeTest() {
73 service.stubFor(get(urlEqualTo("/restconf/config/GENERIC-RESOURCE-API:preload-vnfs"))
74 .willReturn(aResponse().withStatus(404)));
75 JsonObject actualResponse=null;
77 actualResponse = restconfClient.get("GENERIC-RESOURCE-API:preload-vnfs");
78 } catch (RestconfException e) {
81 assertNull(actualResponse);
85 public void putPositiveTest() {
86 service.stubFor(put(urlEqualTo("/restconf/config/GENERIC-RESOURCE-API:preload-information"))
87 .withRequestBody(WireMock.equalTo(preloadInformationRequestJson)).willReturn(aResponse().withStatus(200)));
90 restconfClient.put("GENERIC-RESOURCE-API:preload-information", preloadInformationRequestJson);
91 } catch (RestconfException e) {
98 public void putNegativeTest() {
99 service.stubFor(put(urlEqualTo("/restconf/config/GENERIC-RESOURCE-API:preload-information"))
100 .withRequestBody(WireMock.equalTo(preloadInformationRequestJson)).willReturn(aResponse().withStatus(500)));
102 restconfClient.put("GENERIC-RESOURCE-API:preload-information", preloadInformationRequestJson);
103 } catch (RestconfException e) {
104 assertTrue(e.getErrorMessage().contains("Error during restconf operation: PUT."));