92d17e9e72932968ca66a18fa66325c488a5d23e
[aai/sparky-be.git] / sparkybe-onap-service / src / test / java / org / onap / aai / sparky / util / RestletUtilsTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.util;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mockito;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.sparky.search.SearchServiceAdapter;
30 import org.restlet.Response;
31 import org.restlet.Server;
32 import org.restlet.engine.adapter.Call;
33 import org.restlet.engine.adapter.HttpResponse;
34 import org.restlet.ext.servlet.internal.ServletCall;
35
36 import static org.junit.Assert.assertNotNull;
37 import static org.junit.Assert.assertEquals;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 @RunWith(MockitoJUnitRunner.class)
42 public class RestletUtilsTest {
43
44   static final String EMPTY_STRING = "";
45   RestletUtils restletUtils;
46
47   @Before
48   public void init() {
49     restletUtils = new RestletUtils();
50   }
51
52   @Test
53   public void testConvertRestletResponseToHttpServletResponse() {
54     Response restletResponse = Mockito.mock(HttpResponse.class);
55     Call call = new ServletCall(Mockito.mock(Server.class),Mockito.mock(HttpServletRequest.class),Mockito.mock(HttpServletResponse.class));
56     Mockito.when(((HttpResponse)restletResponse).getHttpCall()).thenReturn((ServletCall)call);
57     restletUtils.convertRestletResponseToHttpServletResponse(restletResponse);
58   }
59
60   @Test
61   public void testExecutePostQueryNullCheck() {
62     Logger logger = Mockito.mock(Logger.class);
63     Response response = Mockito.mock(Response.class);
64     SearchServiceAdapter search = Mockito.mock(SearchServiceAdapter.class);
65     String requestUrl = EMPTY_STRING;
66     String requestJsonPayload = EMPTY_STRING;
67     try{
68       restletUtils.executePostQuery(logger,search,response,requestUrl,requestJsonPayload);
69     }catch (NullPointerException e){
70       //expected
71     }
72   }
73
74   @Test
75   public void testHandleRestletErrors() {
76     Logger logger = Mockito.mock(Logger.class);
77     Response response = Mockito.mock(Response.class);
78     Exception exc = Mockito.mock(Exception.class);
79     String errorMsg = "";
80     restletUtils.handleRestletErrors(logger,errorMsg,exc,response);
81   }
82
83   @Test
84   public void testSetRestletResponse() {
85     Logger logger = Mockito.mock(Logger.class);
86     Response response = Mockito.mock(Response.class);
87     Exception exc = Mockito.mock(Exception.class);
88     boolean isError = true;
89     int responseCode =200;
90     String payload = "";
91     restletUtils.setRestletResponse(logger,false,responseCode,response,payload);
92   }
93 }