7a0d47cd6b616bb07ac14192c5159966ef03388e
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42
43 import java.io.IOException;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Matchers;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.controller.sample.ElasticSearchController;
57 import org.onap.portalapp.framework.MockitoTestSuite;
58 import org.onap.portalapp.model.Result;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.http.ResponseEntity;
64 import org.springframework.web.servlet.ModelAndView;
65
66 import com.google.gson.Gson;
67
68 import io.searchbox.core.SearchResult;
69
70 import io.searchbox.client.JestClient;
71 import io.searchbox.client.JestClientFactory;
72 import io.searchbox.client.config.HttpClientConfig;
73 import io.searchbox.client.http.JestHttpClient;
74
75
76 @RunWith(PowerMockRunner.class)
77 @PrepareForTest({ JestClientFactory.class, JestClient.class })
78 public class ElasticSearchControllerTest {
79
80         @InjectMocks
81         ElasticSearchController elasticSearchController = new ElasticSearchController();
82
83         @Before
84         public void setup() {
85                 MockitoAnnotations.initMocks(this);
86         }
87         @Mock
88         JestClientFactory factory;
89         @Mock
90         JestClient client ;
91         
92         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
93         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
94         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
95         
96         @Test
97         public void searchTest()
98         {
99                  ModelAndView expectedResult = elasticSearchController.search();
100              assertEquals(expectedResult.getViewName(), "es_search_demo");
101         }
102         
103         @Test
104         public void suggestTest()
105         {
106                  ModelAndView expectedResult = elasticSearchController.suggest();
107              assertEquals(expectedResult.getViewName(), "es_suggest_demo");
108         }
109         
110 //      @Test
111 //      public void doSuggestTest() throws IOException
112 //      {
113 //              PowerMockito.mockStatic(JestClient.class);
114 //              PowerMockito.mockStatic(JestClientFactory.class);
115 //              SearchResult result = new SearchResult(new Gson());
116 //              Mockito.when(factory.getObject()).thenReturn(client);
117 //              Mockito.when(client.execute(Matchers.anyObject())).thenReturn(result);
118 ////        Mockito.doNothing().when(factory).setHttpClientConfig(new HttpClientConfig
119 ////                    .Builder(Matchers.anyString())
120 ////                .multiThreaded(Matchers.anyBoolean())
121 ////                .build());
122 //              
123 //              JestClientFactory factory = Mockito.spy(new JestClientFactory());
124 //        HttpClientConfig httpClientConfig = Mockito.spy(new HttpClientConfig.Builder("http://localhost:9200")
125 //                .discoveryEnabled(true)
126 //                .build());
127 //        factory.setHttpClientConfig(httpClientConfig);
128 //        JestHttpClient jestClient = (JestHttpClient) factory.getObject();
129 //        
130 //              elasticSearchController.doSearch("{\"data\":\"1\",\"size\":\"1\"}");
131 //      }
132         
133         @Test
134         public void sendResultTest()
135         {
136                 ResponseEntity<Result> result = elasticSearchController.sendResult(null);
137                 assertEquals(result.getStatusCode(), HttpStatus.OK);
138         }
139         
140         @Test
141         public void isRestFultest()
142         {
143                 assertTrue(elasticSearchController.isRESTfulCall());
144         }
145 }