4592de8195e3203303cbe6327133ea8e4e2d6e98
[ccsdk/features.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * \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
6  * \r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * \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
12  * the License.\r
13  */\r
14 \r
15 package org.onap.ccsdk.config.rest.adaptor.service;\r
16 \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
34 \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
40     \r
41     ConfigRestAdaptorService configRestAdaptorService;\r
42     \r
43     RestTemplate mockRestTemplate = mock(RestTemplate.class);\r
44     \r
45     String path = "path";\r
46     \r
47     @Before\r
48     public void before() throws Exception {\r
49         whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);\r
50         \r
51         String propertyDir = "src/test/resources";\r
52         configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);\r
53     }\r
54     \r
55     @Test\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
61         \r
62         String body = configRestAdaptorService.getResource("aai", path, String.class);\r
63         \r
64         Assert.assertEquals(responseBody, body);\r
65     }\r
66     \r
67     @Test\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
73         \r
74         String body = configRestAdaptorService.postResource("aai", path, null, String.class);\r
75         \r
76         Assert.assertEquals(responseBody, body);\r
77     }\r
78     \r
79     @Test\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
85         \r
86         String body = configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");\r
87         \r
88         Assert.assertEquals(responseBody, body);\r
89     }\r
90     \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
95         \r
96         configRestAdaptorService.getResource("aai", path, String.class);\r
97     }\r
98     \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
104         \r
105         configRestAdaptorService.postResource("aai", path, null, String.class);\r
106     }\r
107     \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
113         \r
114         configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");\r
115     }\r
116 }\r