aboutsummaryrefslogtreecommitdiffstats
path: root/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/SSLClientServiceTest.java
blob: 4592de8195e3203303cbe6327133ea8e4e2d6e98 (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
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * 
 * 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.
 */

package org.onap.ccsdk.config.rest.adaptor.service;

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@SuppressWarnings("unchecked")
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.net.ssl.*")
@PrepareForTest({AbstractConfigRestClientAdapter.class})
public class SSLClientServiceTest {
    
    ConfigRestAdaptorService configRestAdaptorService;
    
    RestTemplate mockRestTemplate = mock(RestTemplate.class);
    
    String path = "path";
    
    @Before
    public void before() throws Exception {
        whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);
        
        String propertyDir = "src/test/resources";
        configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);
    }
    
    @Test
    public void testGetResource() throws Exception {
        String responseBody = "sampleBodyString";
        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
                Matchers.any(Class.class))).thenReturn(response);
        
        String body = configRestAdaptorService.getResource("aai", path, String.class);
        
        Assert.assertEquals(responseBody, body);
    }
    
    @Test
    public void testPostResource() throws Exception {
        String responseBody = "sampleBodyString";
        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.POST), Matchers.any(),
                Matchers.any(Class.class))).thenReturn(response);
        
        String body = configRestAdaptorService.postResource("aai", path, null, String.class);
        
        Assert.assertEquals(responseBody, body);
    }
    
    @Test
    public void testExchange() throws Exception {
        String responseBody = "sampleBodyString";
        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);
        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
                Matchers.any(Class.class))).thenReturn(response);
        
        String body = configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");
        
        Assert.assertEquals(responseBody, body);
    }
    
    @Test(expected = ConfigRestAdaptorException.class)
    public void testGetResourceError() throws Exception {
        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
        when(mockRestTemplate.getForEntity(Matchers.endsWith(path), Matchers.any())).thenReturn(response);
        
        configRestAdaptorService.getResource("aai", path, String.class);
    }
    
    @Test(expected = ConfigRestAdaptorException.class)
    public void testPostResourceError() throws Exception {
        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
        when(mockRestTemplate.postForEntity(Matchers.endsWith(path), Matchers.anyObject(), Matchers.any()))
                .thenReturn(response);
        
        configRestAdaptorService.postResource("aai", path, null, String.class);
    }
    
    @Test(expected = ConfigRestAdaptorException.class)
    public void testExchangeError() throws Exception {
        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);
        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),
                Matchers.any(Class.class))).thenReturn(response);
        
        configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");
    }
}