Controller Blueprints Nitrogen to Oxygen Migration
[ccsdk/features.git] / blueprints-processor / adaptors / rest-adaptor-provider / src / test / java / org / onap / ccsdk / features / 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.features.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.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
39 \r
40 @SuppressWarnings("unchecked")\r
41 @RunWith(PowerMockRunner.class)\r
42 @PowerMockIgnore("javax.net.ssl.*")\r
43 @PrepareForTest({AbstractConfigRestClientAdapter.class})\r
44 public class GenericRestClientServiceTest {\r
45 \r
46     ConfigRestAdaptorService configRestAdaptorService;\r
47 \r
48     RestTemplate mockRestTemplate = mock(RestTemplate.class);\r
49 \r
50     String path = "path";\r
51 \r
52     @Before\r
53     public void before() throws Exception {\r
54         whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);\r
55 \r
56         String propertyDir = "src/test/resources";\r
57         configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);\r
58     }\r
59 \r
60     @Test\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
66 \r
67         String body = configRestAdaptorService.getResource("modelservice", path, String.class);\r
68 \r
69         Assert.assertEquals(responseBody, body);\r
70     }\r
71 \r
72     @Test\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
78 \r
79         String body = configRestAdaptorService.postResource("modelservice", path, null, String.class);\r
80 \r
81         Assert.assertEquals(responseBody, body);\r
82     }\r
83 \r
84     @Test\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
90 \r
91         String body = configRestAdaptorService.exchangeResource("modelservice", path, null, String.class, "GET");\r
92 \r
93         Assert.assertEquals(responseBody, body);\r
94     }\r
95 \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
100 \r
101         configRestAdaptorService.getResource("modelservice", path, String.class);\r
102     }\r
103 \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
109 \r
110         configRestAdaptorService.postResource("modelservice", path, null, String.class);\r
111     }\r
112 \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
118 \r
119         configRestAdaptorService.exchangeResource("modelservice", path, null, String.class, "GET");\r
120     }\r
121 }\r