68e530394c6b5e8e00aea62e54eaf71993eaa705
[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.apache.http.conn.ssl.SSLInitializationException;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.InjectMocks;
53 import org.mockito.Matchers;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.portalapp.controller.sample.ElasticSearchController;
58 import org.onap.portalapp.framework.MockitoTestSuite;
59 import org.onap.portalapp.model.Result;
60 import org.powermock.api.mockito.PowerMockito;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.web.servlet.ModelAndView;
66
67 import com.google.gson.Gson;
68
69 import io.searchbox.core.SearchResult;
70
71 import io.searchbox.client.JestClient;
72 import io.searchbox.client.JestClientFactory;
73 import io.searchbox.client.config.HttpClientConfig;
74 import io.searchbox.client.http.JestHttpClient;
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
88         @Mock
89         JestClientFactory factory;
90         @Mock
91         JestClient client;
92
93         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
94         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
95         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
96
97         @Test
98         public void searchTest() {
99                 ModelAndView expectedResult = elasticSearchController.search();
100                 assertEquals(expectedResult.getViewName(), "es_search_demo");
101         }
102
103         @Test
104         public void suggestTest() {
105                 ModelAndView expectedResult = elasticSearchController.suggest();
106                 assertEquals(expectedResult.getViewName(), "es_suggest_demo");
107         }
108
109         @Test(expected = Exception.class)
110         public void doSuggestTest() throws IOException {
111                 String task = "{ \"data\" : \"Data\" , \"size\" : \"Size\" , \"fuzzy\" : \"Fuzzy\", \"resultname\" : \"Result Name\" }";
112                 elasticSearchController.doSuggest(task);
113         }
114         
115         @Test(expected = Exception.class)
116         public void doSearchTest() throws IOException {
117                 String task = "{ \"data\" : \"Data\" , \"size\" : \"Size\" , \"fuzzy\" : \"Fuzzy\", \"resultname\" : \"Result Name\" }";
118                 elasticSearchController.doSearch(task);
119         }
120
121         @Test
122         public void sendResultTest() {
123                 ResponseEntity<Result> result = elasticSearchController.sendResult(null);
124                 assertEquals(result.getStatusCode(), HttpStatus.OK);
125         }
126
127         @Test
128         public void isRestFultest() {
129                 assertTrue(elasticSearchController.isRESTfulCall());
130         }
131 }