2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.features.rest.adaptor.service;
\r
20 import static org.powermock.api.mockito.PowerMockito.mock;
\r
21 import static org.powermock.api.mockito.PowerMockito.when;
\r
22 import static org.powermock.api.mockito.PowerMockito.whenNew;
\r
23 import org.junit.Assert;
\r
24 import org.junit.Before;
\r
25 import org.junit.Test;
\r
26 import org.junit.runner.RunWith;
\r
27 import org.mockito.Matchers;
\r
28 import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorException;
\r
29 import org.onap.ccsdk.features.rest.adaptor.service.AbstractConfigRestClientAdapter;
\r
30 import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorService;
\r
31 import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorServiceImpl;
\r
32 import org.powermock.core.classloader.annotations.PowerMockIgnore;
\r
33 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
34 import org.powermock.modules.junit4.PowerMockRunner;
\r
35 import org.springframework.http.HttpMethod;
\r
36 import org.springframework.http.HttpStatus;
\r
37 import org.springframework.http.ResponseEntity;
\r
38 import org.springframework.web.client.RestTemplate;
\r
40 @SuppressWarnings("unchecked")
\r
41 @RunWith(PowerMockRunner.class)
\r
42 @PowerMockIgnore("javax.net.ssl.*")
\r
43 @PrepareForTest({AbstractConfigRestClientAdapter.class})
\r
44 public class SSLClientServiceTest {
\r
46 ConfigRestAdaptorService configRestAdaptorService;
\r
48 RestTemplate mockRestTemplate = mock(RestTemplate.class);
\r
50 String path = "path";
\r
53 public void before() throws Exception {
\r
54 whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);
\r
56 String propertyDir = "src/test/resources";
\r
57 configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);
\r
61 public void testGetResource() throws Exception {
\r
62 String responseBody = "sampleBodyString";
\r
63 ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
\r
64 when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
\r
65 Matchers.any(Class.class))).thenReturn(response);
\r
67 String body = configRestAdaptorService.getResource("aai", path, String.class);
\r
69 Assert.assertEquals(responseBody, body);
\r
73 public void testPostResource() throws Exception {
\r
74 String responseBody = "sampleBodyString";
\r
75 ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
\r
76 when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.POST), Matchers.any(),
\r
77 Matchers.any(Class.class))).thenReturn(response);
\r
79 String body = configRestAdaptorService.postResource("aai", path, null, String.class);
\r
81 Assert.assertEquals(responseBody, body);
\r
85 public void testExchange() throws Exception {
\r
86 String responseBody = "sampleBodyString";
\r
87 ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
\r
88 when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
\r
89 Matchers.any(Class.class))).thenReturn(response);
\r
91 String body = configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");
\r
93 Assert.assertEquals(responseBody, body);
\r
96 @Test(expected = ConfigRestAdaptorException.class)
\r
97 public void testGetResourceError() throws Exception {
\r
98 ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
\r
99 when(mockRestTemplate.getForEntity(Matchers.endsWith(path), Matchers.any())).thenReturn(response);
\r
101 configRestAdaptorService.getResource("aai", path, String.class);
\r
104 @Test(expected = ConfigRestAdaptorException.class)
\r
105 public void testPostResourceError() throws Exception {
\r
106 ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
\r
107 when(mockRestTemplate.postForEntity(Matchers.endsWith(path), Matchers.anyObject(), Matchers.any()))
\r
108 .thenReturn(response);
\r
110 configRestAdaptorService.postResource("aai", path, null, String.class);
\r
113 @Test(expected = ConfigRestAdaptorException.class)
\r
114 public void testExchangeError() throws Exception {
\r
115 ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
\r
116 when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
\r
117 Matchers.any(Class.class))).thenReturn(response);
\r
119 configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");
\r