95da3667b6f82614ee97e3512097f122da216b6d
[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 import static org.mockito.Mockito.when;
26
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.onap.slice.analysis.ms.service.SnssaiSamplesProcessorTest;
36 import org.onap.slice.analysis.ms.utils.BeanUtil;
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.HttpEntity;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.test.context.junit4.SpringRunner;
50 import org.springframework.web.client.RestTemplate;
51
52
53
54 @RunWith(SpringRunner.class)
55 @SpringBootTest(classes = RestClientTest.class)
56 public class RestClientTest {
57         
58         @Mock
59         RestTemplate restTemplate;
60
61         @InjectMocks
62         RestClient restclient;
63         
64         @SuppressWarnings({ "static-access"})
65         @Test
66         public void sendGetRequestTest() {
67                 ParameterizedTypeReference<Map<String,Integer>> responseType = null;
68                HttpHeaders headers = new HttpHeaders();
69                headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
70                headers.setContentType(MediaType.APPLICATION_JSON);
71                 HttpEntity<Object> requestEntity = new HttpEntity<>( headers);
72                 Map<String, Integer> responsemap=new HashMap<>();
73                 responsemap.put("dLThptPerSlice", 1);
74                 responsemap.put("uLThptPerSlice", 2);
75                 String requestUrl="";
76                 when(restTemplate.exchange(requestUrl, HttpMethod.GET,requestEntity,responseType)).thenReturn(ResponseEntity.ok(responsemap));
77                  ResponseEntity<Map<String,Integer>> resp = restclient.sendGetRequest(headers, requestUrl, responseType);
78                 assertEquals(resp.getBody(),responsemap);       
79         }
80         
81         @SuppressWarnings({ "static-access", "unchecked", "rawtypes" })
82         @Test
83         public void sendPostRequestTest() { 
84        ParameterizedTypeReference<String> responseType = null;
85        HttpHeaders headers = new HttpHeaders();
86        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
87        headers.setContentType(MediaType.APPLICATION_JSON);
88        String requestUrl = "Url"; String requestBody = null;  
89        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
90        when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.OK)); 
91        ResponseEntity<String> resp = restclient.sendPostRequest(headers, requestUrl, requestBody,responseType);
92        assertEquals(resp.getStatusCode(), HttpStatus.OK);  
93         }
94         
95         @Test
96         public void sendPostRequestTest2() { 
97        ParameterizedTypeReference<String> responseType = null;
98        HttpHeaders headers = new HttpHeaders();
99        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
100        headers.setContentType(MediaType.APPLICATION_JSON);
101        String requestUrl = "Url"; String requestBody = null;  
102        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
103        when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.NOT_FOUND));        
104        ResponseEntity<String> resp = restclient.sendPostRequest(headers, requestUrl, requestBody,responseType);
105        assertEquals(resp.getStatusCode(), HttpStatus.NOT_FOUND);  
106         }  
107         
108         
109 }