b19b4801ee7a8bd75a26aaf03673a75063d9e62a
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020 Wipro Limited.
6  *   ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *
20  *******************************************************************************/
21 package org.onap.slice.analysis.ms.restclients;
22
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.onap.slice.analysis.ms.utils.BeanUtil;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.core.ParameterizedTypeReference;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import org.springframework.web.client.RestTemplate;
46 import org.springframework.http.HttpEntity;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpMethod;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.http.MediaType;
51
52
53
54 @RunWith(PowerMockRunner.class)
55 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
56 @PowerMockRunnerDelegate(SpringRunner.class)
57 @PrepareForTest({ BeanUtil.class })
58 @SpringBootTest(classes = RestClient.class)
59 public class RestClientTest {
60
61         
62         @Mock
63         RestTemplate restTemplate;
64
65         
66         @InjectMocks
67         RestClient restclient;
68         
69
70         @SuppressWarnings({ "static-access", "unchecked", "rawtypes" })
71         @Test
72         public void sendGetRequestTest() {
73         
74                 PowerMockito.mockStatic(BeanUtil.class);
75                 PowerMockito.when(BeanUtil.getBean(Mockito.any())).thenReturn(restTemplate);
76                  ParameterizedTypeReference<Map<String,Integer>> responseType = null;
77                HttpHeaders headers = new HttpHeaders();
78                headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
79                headers.setContentType(MediaType.APPLICATION_JSON);
80                 HttpEntity<Object> requestEntity = new HttpEntity<>( headers);
81                 Map<String, Integer> responsemap=new HashMap<>();
82                 responsemap.put("dLThptPerSlice", 1);
83                 responsemap.put("uLThptPerSlice", 2);
84                 String requestUrl="";
85                 PowerMockito.when(restTemplate.exchange(requestUrl, HttpMethod.GET,requestEntity,responseType)).thenReturn(ResponseEntity.ok(responsemap));
86                  ResponseEntity<Map<String,Integer>> resp = restclient.sendGetRequest(headers, requestUrl, responseType);
87                 assertEquals(resp.getBody(),responsemap);       
88         }
89         
90         @SuppressWarnings({ "static-access", "unchecked", "rawtypes" })
91         @Test
92         public void sendPostRequestTest() { 
93        PowerMockito.mockStatic(BeanUtil.class);
94        PowerMockito.when(BeanUtil.getBean(RestTemplate.class)).thenReturn(restTemplate);
95        ParameterizedTypeReference<String> responseType = null;
96        HttpHeaders headers = new HttpHeaders();
97        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
98        headers.setContentType(MediaType.APPLICATION_JSON);
99        String requestUrl = "Url"; String requestBody = null;  
100        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
101        PowerMockito.when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.OK)); 
102        ResponseEntity<String> resp = restclient.sendPostRequest(headers, requestUrl, requestBody,responseType);
103        assertEquals(resp.getStatusCode(), HttpStatus.OK);  
104         }
105         
106         @Test
107         public void sendPostRequestTest2() { 
108        ParameterizedTypeReference<String> responseType = null;
109        HttpHeaders headers = new HttpHeaders();
110        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
111        headers.setContentType(MediaType.APPLICATION_JSON);
112        String requestUrl = "Url"; String requestBody = null;  
113        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
114        PowerMockito.when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.NOT_FOUND));        
115        ResponseEntity<String> resp = restclient.sendPostRequest(headers, requestUrl, requestBody,responseType);
116        assertEquals(resp.getStatusCode(), HttpStatus.NOT_FOUND);  
117         }  
118         
119         
120 }