2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
43 import java.io.IOException;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
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;
66 import com.google.gson.Gson;
68 import io.searchbox.core.SearchResult;
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;
76 @RunWith(PowerMockRunner.class)
77 @PrepareForTest({ JestClientFactory.class, JestClient.class })
78 public class ElasticSearchControllerTest {
81 ElasticSearchController elasticSearchController = new ElasticSearchController();
85 MockitoAnnotations.initMocks(this);
88 JestClientFactory factory;
92 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
93 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
94 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
97 public void searchTest()
99 ModelAndView expectedResult = elasticSearchController.search();
100 assertEquals(expectedResult.getViewName(), "es_search_demo");
104 public void suggestTest()
106 ModelAndView expectedResult = elasticSearchController.suggest();
107 assertEquals(expectedResult.getViewName(), "es_suggest_demo");
111 // public void doSuggestTest() throws IOException
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())
123 // JestClientFactory factory = Mockito.spy(new JestClientFactory());
124 // HttpClientConfig httpClientConfig = Mockito.spy(new HttpClientConfig.Builder("http://localhost:9200")
125 // .discoveryEnabled(true)
127 // factory.setHttpClientConfig(httpClientConfig);
128 // JestHttpClient jestClient = (JestHttpClient) factory.getObject();
130 // elasticSearchController.doSearch("{\"data\":\"1\",\"size\":\"1\"}");
134 public void sendResultTest()
136 ResponseEntity<Result> result = elasticSearchController.sendResult(null);
137 assertEquals(result.getStatusCode(), HttpStatus.OK);
141 public void isRestFultest()
143 assertTrue(elasticSearchController.isRESTfulCall());