2  * Copyright © 2017-2018 AT&T Intellectual Property.
\r 
   4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
\r 
   5  * in compliance with the License. You may obtain a copy of the License at
\r 
   7  * http://www.apache.org/licenses/LICENSE-2.0
\r 
   9  * Unless required by applicable law or agreed to in writing, software distributed under the License
\r 
  10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
\r 
  11  * or implied. See the License for the specific language governing permissions and limitations under
\r 
  15 package org.onap.ccsdk.config.rest.adaptor.service;
\r 
  17 import static org.powermock.api.mockito.PowerMockito.mock;
\r 
  18 import static org.powermock.api.mockito.PowerMockito.when;
\r 
  19 import static org.powermock.api.mockito.PowerMockito.whenNew;
\r 
  20 import org.junit.Assert;
\r 
  21 import org.junit.Before;
\r 
  22 import org.junit.Test;
\r 
  23 import org.junit.runner.RunWith;
\r 
  24 import org.mockito.Matchers;
\r 
  25 import org.mockito.Mockito;
\r 
  26 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
\r 
  27 import org.powermock.core.classloader.annotations.PowerMockIgnore;
\r 
  28 import org.powermock.core.classloader.annotations.PrepareForTest;
\r 
  29 import org.powermock.modules.junit4.PowerMockRunner;
\r 
  30 import org.springframework.http.HttpMethod;
\r 
  31 import org.springframework.http.HttpStatus;
\r 
  32 import org.springframework.http.ResponseEntity;
\r 
  33 import org.springframework.web.client.RestTemplate;
\r 
  35 @SuppressWarnings("unchecked")
\r 
  36 @RunWith(PowerMockRunner.class)
\r 
  37 @PowerMockIgnore("javax.net.ssl.*")
\r 
  38 @PrepareForTest({AbstractConfigRestClientAdapter.class})
\r 
  39 public class SSLClientServiceTest {
\r 
  41     ConfigRestAdaptorService configRestAdaptorService;
\r 
  43     RestTemplate mockRestTemplate = mock(RestTemplate.class);
\r 
  45     String path = "path";
\r 
  48     public void before() throws Exception {
\r 
  49         whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);
\r 
  51         String propertyDir = "src/test/resources";
\r 
  52         configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);
\r 
  56     public void testGetResource() throws Exception {
\r 
  57         String responseBody = "sampleBodyString";
\r 
  58         ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
\r 
  59         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
\r 
  60                 Matchers.any(Class.class))).thenReturn(response);
\r 
  62         String body = configRestAdaptorService.getResource("aai", path, String.class);
\r 
  64         Assert.assertEquals(responseBody, body);
\r 
  68     public void testPostResource() throws Exception {
\r 
  69         String responseBody = "sampleBodyString";
\r 
  70         ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
\r 
  71         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.POST), Matchers.any(),
\r 
  72                 Matchers.any(Class.class))).thenReturn(response);
\r 
  74         String body = configRestAdaptorService.postResource("aai", path, null, String.class);
\r 
  76         Assert.assertEquals(responseBody, body);
\r 
  80     public void testExchange() throws Exception {
\r 
  81         String responseBody = "sampleBodyString";
\r 
  82         ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
\r 
  83         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
\r 
  84                 Matchers.any(Class.class))).thenReturn(response);
\r 
  86         String body = configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");
\r 
  88         Assert.assertEquals(responseBody, body);
\r 
  91     @Test(expected = ConfigRestAdaptorException.class)
\r 
  92     public void testGetResourceError() throws Exception {
\r 
  93         ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
\r 
  94         when(mockRestTemplate.getForEntity(Matchers.endsWith(path), Matchers.any())).thenReturn(response);
\r 
  96         configRestAdaptorService.getResource("aai", path, String.class);
\r 
  99     @Test(expected = ConfigRestAdaptorException.class)
\r 
 100     public void testPostResourceError() throws Exception {
\r 
 101         ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
\r 
 102         when(mockRestTemplate.postForEntity(Matchers.endsWith(path), Matchers.anyObject(), Matchers.any()))
\r 
 103                 .thenReturn(response);
\r 
 105         configRestAdaptorService.postResource("aai", path, null, String.class);
\r 
 108     @Test(expected = ConfigRestAdaptorException.class)
\r 
 109     public void testExchangeError() throws Exception {
\r 
 110         ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
\r 
 111         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
\r 
 112                 Matchers.any(Class.class))).thenReturn(response);
\r 
 114         configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");
\r