This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit e0be881d authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Still setting up Test Elastic Search

parent c3eed167
1 merge request!17Initial Merge to Prepare Release 1.0.0
......@@ -5,8 +5,22 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.InternalSettingsPreparer;
import org.elasticsearch.node.Node;
......@@ -25,6 +39,8 @@ import org.springframework.http.MediaType;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
......@@ -85,10 +101,51 @@ public class ElasticSearchTestServerConfiguration {
} catch (RestClientException e) {
LOGGER.info("Deletion of previous index failed: {}!", e.getMessage());
}
testNode.client().index(new IndexRequest());
List<String> indices2 = null;
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 29200)));
RestClient restClient = client.getLowLevelClient();
Response response2 = null;
try {
response2 = restClient.performRequest("GET", "/_cat/indices?v&format=json");
} catch (IOException e) {
LOGGER.warn(e.toString(), e);
}
// parse the JSON response
List<HashMap<String, String>> list = null;
if (response2 != null) {
ObjectMapper mapper = new ObjectMapper();
String rawBody = EntityUtils.toString(response2.getEntity());
TypeReference<List<HashMap<String, String>>> typeRef = new TypeReference<List<HashMap<String, String>>>() {};
list = mapper.readValue(rawBody, typeRef);
}
// get the index names
if (list != null) {
indices2 = list.stream()
.map(x -> x.get("index"))
.collect(Collectors.toList());
}
indices2.forEach((s) -> {LOGGER.info("Found index {}", s);});
// GetIndexRequest request2 = new GetIndexRequest();
//
// GetIndexResponse response = client.indices().get(request2, RequestOptions.DEFAULT);
// String[] indices = response.getIndices();
HttpEntity<String> request =
new HttpEntity<String>(getMetaDataConfigDefinition(), headers);
restTemplate.put("http://localhost:29200/idx_metadata_1", request, String.class);
}
......
package at.ac.uibk.gitsearch.service;
import static at.ac.uibk.gitsearch.web.rest.AccountResourceIT.TEST_USER_LOGIN;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.hasProperty;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.elasticsearch.node.NodeValidationException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import at.ac.uibk.gitsearch.GitsearchApp;
import at.ac.uibk.gitsearch.repository.search.testESService.ElasticSearchTestServerConfiguration;
import at.ac.uibk.gitsearch.service.dto.SearchInputDTO;
import at.ac.uibk.gitsearch.service.dto.SearchInputMetadataDTO;
import at.ac.uibk.gitsearch.service.dto.SearchResultsDTO;
/**
* Integration tests for {@link MailService}.
*/
@SpringBootTest(classes = GitsearchApp.class)
@WithMockUser(value = TEST_USER_LOGIN)
public class SearchServiceOldIT {
private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceOldIT.class);
@Autowired
private SearchService searchService;
@BeforeAll
public static void setUpESServer() throws IOException, NodeValidationException {
LOGGER.info("Starting es test server");
ElasticSearchTestServerConfiguration.getTestInstance().startTestNode();
LOGGER.info("Started es test server");
}
@BeforeEach
public void setup() {
// MockitoAnnotations.initMocks(this);
LOGGER.info("in setup");
}
@Test
public void testFullTextSearch() throws Exception {
LOGGER.info("starting testFullTextSearch");
final String WIEN = "Wien";
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, null);
SearchInputDTO searchQuery = new SearchInputDTO(WIEN, searchMetadata, null, null, null, 0);
SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery, 0, SearchInputDTO.PAGE_SIZE);
org.junit.Assert.assertNotNull(searchResultPage.getSearchResult());
org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1);
org.junit.Assert.assertThat(searchResultPage.getSearchResult(),
everyItem(anyOf(hasProperty("metadata", hasProperty("description", containsString(WIEN))),
hasProperty("metadata", hasProperty("title", containsString(WIEN)))
// hasProperty("keywords", containsString("Wien"))
)));
LOGGER.info("exiting testFullTextSearch");
}
@Test
public void testSearchByAutor() throws Exception {
final String PODLIPNIG = "Stefan Podlipnig";
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, PODLIPNIG);
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery, 0, SearchInputDTO.PAGE_SIZE);
org.junit.Assert.assertNotNull(searchResultPage.getSearchResult());
org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1);
org.junit.Assert.assertThat(searchResultPage.getSearchResult(), everyItem(
hasProperty("metadata", hasProperty("creator", hasItemInArray(hasProperty("name", containsString(PODLIPNIG)))))));
}
@Test
public void testProgrammingLanguageSearch() throws Exception {
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO("JAVA", null, null, null, null);
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery, 0, SearchInputDTO.PAGE_SIZE);
assertNotNull(searchResultPage.getSearchResult());
assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1);
assertThat(searchResultPage.getSearchResult(), everyItem(
hasProperty("metadata", hasProperty("programmingLanguage", hasItemInArray(containsString("Java"))))));
}
@Test
public void testCreatorSearch() throws IOException {
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, null, "Podlipnig");
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery, 0, SearchInputDTO.PAGE_SIZE);
org.junit.Assert.assertNotNull(searchResultPage.getSearchResult());
org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1);
assertThat(searchResultPage.getSearchResult(), everyItem(hasProperty("metadata",
hasProperty("creator", hasItemInArray(hasProperty("name", containsString("Podlipnig")))))));
}
@Test
public void testKeywordSearch() throws IOException {
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, "latex", null, null, null);
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery, 0, SearchInputDTO.PAGE_SIZE);
org.junit.Assert.assertNotNull(searchResultPage.getSearchResult());
org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1);
assertThat(searchResultPage.getSearchResult(),
everyItem(hasProperty("metadata", hasProperty("keyword", hasItemInArray(containsString("latex"))))));
}
@Test
public void testLicenseSearch() throws IOException {
final SearchInputMetadataDTO searchMetadata = new SearchInputMetadataDTO(null, null, null, "MIT", null);
SearchInputDTO searchQuery = new SearchInputDTO(null, searchMetadata, null, null, null, 0);
SearchResultsDTO searchResultPage = searchService.searchResultPage(searchQuery, 0, SearchInputDTO.PAGE_SIZE);
org.junit.Assert.assertNotNull(searchResultPage.getSearchResult());
org.junit.Assert.assertTrue("At least one test hit", searchResultPage.getHitCount() >= 1);
assertThat(searchResultPage.getSearchResult(),
everyItem(hasProperty("metadata", hasProperty("license", containsString("MIT")))));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment