Merge "DG changes for the closed loop and async support in MDONS"
[sdnc/oam.git] / data-migrator / src / test / java / org / onap / sdnc / oam / datamigrator / common / RestconfClientTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : SDNC
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.sdnc.oam.datamigrator.common;
21
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;
29
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34
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;
42
43 public class RestconfClientTest {
44
45     @Rule
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())));
51     
52     
53     JsonObject expectedJsonObject = new JsonParser().parse(preloadVnfResponseJson).getAsJsonObject();
54
55     public RestconfClientTest() throws IOException, URISyntaxException {
56     }
57
58     @Test
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;
63         try {
64             actualResponse =  restconfClient.get("GENERIC-RESOURCE-API:preload-vnfs");
65         } catch (RestconfException e) {
66             e.printStackTrace();
67         }
68         assertEquals(expectedJsonObject,actualResponse);
69     }
70
71     @Test
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;
76         try {
77             actualResponse = restconfClient.get("GENERIC-RESOURCE-API:preload-vnfs");
78         } catch (RestconfException e) {
79             e.printStackTrace();
80         }
81         assertNull(actualResponse);
82     }
83
84     @Test
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)));
88         Exception ex = null;
89         try {
90             restconfClient.put("GENERIC-RESOURCE-API:preload-information", preloadInformationRequestJson);
91         } catch (RestconfException e) {
92             ex =e;
93         }
94         assertNull(ex);
95     }
96
97     @Test
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)));
101         try {
102             restconfClient.put("GENERIC-RESOURCE-API:preload-information", preloadInformationRequestJson);
103         } catch (RestconfException e) {
104            assertTrue(e.getErrorMessage().contains("Error during restconf operation: PUT."));
105         }
106     }
107 }