Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / adaptors / rest-adaptor-provider / src / test / java / org / onap / ccsdk / config / rest / adaptor / service / GenericRestClientServiceTest.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \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
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \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
16  */\r
17 \r
18 package org.onap.ccsdk.config.rest.adaptor.service;\r
19 \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.config.rest.adaptor.ConfigRestAdaptorException;\r
29 import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
30 import org.powermock.core.classloader.annotations.PrepareForTest;\r
31 import org.powermock.modules.junit4.PowerMockRunner;\r
32 import org.springframework.http.HttpMethod;\r
33 import org.springframework.http.HttpStatus;\r
34 import org.springframework.http.ResponseEntity;\r
35 import org.springframework.web.client.RestTemplate;\r
36 \r
37 @SuppressWarnings("unchecked")\r
38 @RunWith(PowerMockRunner.class)\r
39 @PowerMockIgnore("javax.net.ssl.*")\r
40 @PrepareForTest({AbstractConfigRestClientAdapter.class})\r
41 public class GenericRestClientServiceTest {\r
42 \r
43     ConfigRestAdaptorService configRestAdaptorService;\r
44 \r
45     RestTemplate mockRestTemplate = mock(RestTemplate.class);\r
46 \r
47     String path = "path";\r
48 \r
49     @Before\r
50     public void before() throws Exception {\r
51         whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);\r
52 \r
53         String propertyDir = "src/test/resources";\r
54         configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);\r
55     }\r
56 \r
57     @Test\r
58     public void testGetResource() throws Exception {\r
59         String responseBody = "sampleBodyString";\r
60         ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
61         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
62                 Matchers.any(Class.class))).thenReturn(response);\r
63 \r
64         String body = configRestAdaptorService.getResource("modelservice", path, String.class);\r
65 \r
66         Assert.assertEquals(responseBody, body);\r
67     }\r
68 \r
69     @Test\r
70     public void testPostResource() throws Exception {\r
71         String responseBody = "sampleBodyString";\r
72         ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
73         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.POST), Matchers.any(),\r
74                 Matchers.any(Class.class))).thenReturn(response);\r
75 \r
76         String body = configRestAdaptorService.postResource("modelservice", path, null, String.class);\r
77 \r
78         Assert.assertEquals(responseBody, body);\r
79     }\r
80 \r
81     @Test\r
82     public void testExchange() throws Exception {\r
83         String responseBody = "sampleBodyString";\r
84         ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
85         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
86                 Matchers.any(Class.class))).thenReturn(response);\r
87 \r
88         String body = configRestAdaptorService.exchangeResource("modelservice", path, null, String.class, "GET");\r
89 \r
90         Assert.assertEquals(responseBody, body);\r
91     }\r
92 \r
93     @Test(expected = ConfigRestAdaptorException.class)\r
94     public void testGetResourceError() throws Exception {\r
95         ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
96         when(mockRestTemplate.getForEntity(Matchers.endsWith(path), Matchers.any())).thenReturn(response);\r
97 \r
98         configRestAdaptorService.getResource("modelservice", path, String.class);\r
99     }\r
100 \r
101     @Test(expected = ConfigRestAdaptorException.class)\r
102     public void testPostResourceError() throws Exception {\r
103         ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
104         when(mockRestTemplate.postForEntity(Matchers.endsWith(path), Matchers.anyObject(), Matchers.any()))\r
105                 .thenReturn(response);\r
106 \r
107         configRestAdaptorService.postResource("modelservice", path, null, String.class);\r
108     }\r
109 \r
110     @Test(expected = ConfigRestAdaptorException.class)\r
111     public void testExchangeError() throws Exception {\r
112         ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
113         when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
114                 Matchers.any(Class.class))).thenReturn(response);\r
115 \r
116         configRestAdaptorService.exchangeResource("modelservice", path, null, String.class, "GET");\r
117     }\r
118 }\r