aboutsummaryrefslogtreecommitdiffstats
path: root/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java
blob: 76033c9b42221901353ebb169688be9e78aa693f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2021 Wipro Limited.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.cps.tbdmt.client;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.onap.cps.tbdmt.exception.CpsClientException;
import org.onap.cps.tbdmt.model.AppConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringRunner.class)
@EnableConfigurationProperties(AppConfiguration.class)
@TestPropertySource("classpath:application-test.properties")
public class CpsRestClientTest {

    @TestConfiguration
    static class CpsRestClientTestContextConfiguration {

        @Bean
        public CpsRestClient cpsRestClient() {
            return new CpsRestClient();
        }
    }

    @Autowired
    private CpsRestClient cpsRestClient;

    @MockBean
    private RestTemplate restTemplate;

    @Rule
    public ExpectedException exception = ExpectedException.none();

    private HttpHeaders responseHeaders;
    private ResponseEntity<String> response;

    /**
     * Setup variables before test.
     *
     */
    @Before
    public void setUp() {
        responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        response = new ResponseEntity<>("sample response", responseHeaders,
            HttpStatus.OK);
    }

    @Test
    public void testFetchNode() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/node?xpath=sample&include-descendants=true";
        Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<String>>any()))
            .thenReturn(response);
        assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true));

        final ResponseEntity<String> errorResponse = new ResponseEntity<>("sample response",
            responseHeaders, HttpStatus.NOT_FOUND);
        Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<String>>any()))
            .thenReturn(errorResponse);
        exception.expect(CpsClientException.class);
        exception.expectMessage("Response code from CPS other than 200: 404");
        cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true);

    }

    @Test
    public void testQueryApi() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes/query?xpath=sample&include-descendants=true";
        Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<String>>any()))
            .thenReturn(response);
        assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "query", true));
    }

    @Test
    public void testFetchNodeException() throws Exception {
        Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<String>>any()))
            .thenThrow(new RestClientException("Connection refused"));
        exception.expect(CpsClientException.class);
        exception.expectMessage("Connection refused");
        cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true);
    }

    @Test
    public void testAddDataForPostRequest() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes";
        Mockito.when(restTemplate.postForEntity(ArgumentMatchers.eq(uri), ArgumentMatchers.any(),
                ArgumentMatchers.<Class<String>>any())).thenReturn(response);
        final Map<String, Object> payload = new HashMap<String, Object>();
        payload.put("idNearRTRIC", 11);
        assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC", "post", payload));

    }

    @Test
    public void testAddDataForPutRequest() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?xpath=NearRTRIC";
        Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), ArgumentMatchers.any(HttpMethod.class),
                ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any())).thenReturn(response);
        final Map<String, Object> payload = new HashMap<String, Object>();
        payload.put("idNearRTRIC", 11);
        assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC", "put", payload));

    }

    @Test
    public void testAddDataForPatchRequest() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?xpath=NearRTRIC";
        Mockito.when(restTemplate.patchForObject(ArgumentMatchers.eq(uri), ArgumentMatchers.any(),
                ArgumentMatchers.<Class<String>>any())).thenReturn("sample response");
        final Map<String, Object> payload = new HashMap<String, Object>();
        payload.put("idNearRTRIC", 11);
        assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC", "patch", payload));

    }

    @Test
    public void testAddDataForPostListNodesRequest() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/list-nodes?xpath=NearRTRIC";
        Mockito.when(restTemplate.postForEntity(ArgumentMatchers.eq(uri), ArgumentMatchers.any(),
                ArgumentMatchers.<Class<String>>any())).thenReturn(response);
        final Map<String, Object> payload = new HashMap<String, Object>();
        payload.put("idNearRTRIC", 11);
        assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC",
                               "post-list-node", payload));

    }

    @Test
    public void deleteListNodeData() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/list-nodes?xpath=sample";
        response = new ResponseEntity<String>(HttpStatus.NO_CONTENT);
        Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<String>>any()))
            .thenReturn(response);
        assertEquals("Success", cpsRestClient.deleteData("coverage-area-onap", "sample", "delete-list-node"));
    }

    @Test
    public void deleteNodeData() throws Exception {
        final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?xpath=sample";
        response = new ResponseEntity<String>(HttpStatus.NO_CONTENT);
        Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<String>>any()))
            .thenReturn(response);
        assertEquals("Success", cpsRestClient.deleteData("coverage-area-onap", "sample", "delete"));
    }
}