/******************************************************************************* | |
* Copyright (C) 2018 ANSYS medini Technologies AG | |
* | |
* This program and the accompanying materials are made | |
* available under the terms of the Eclipse Public License 2.0 | |
* which is available at https://www.eclipse.org/legal/epl-2.0/ | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
* | |
* Contributors: | |
* ANSYS medini Technologies AG - initial API and implementation | |
******************************************************************************/ | |
package org.eclipse.opencert.elastic; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import org.eclipse.opencert.elastic.search.ElasticFinderImpl; | |
import org.eclipse.opencert.elastic.search.Hit; | |
import org.junit.Assert; | |
import org.junit.Test; | |
public class ElasticFinderTest { | |
@Test | |
public void testSearch() throws Exception { | |
ElasticClient client = ElasticClientImpl.on("localhost", 9200, "http"); | |
Set<Hit> hits = ElasticFinderImpl.onClient(client).search("this AND that OR thus", null); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertTrue("empty set expected", hits.isEmpty()); | |
} | |
@Test | |
public void testDefaultClient() throws Exception { | |
Set<Hit> hits = ElasticFinderImpl.onDefaultClient().search("this AND that OR thus", null); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertTrue("empty set expected", hits.isEmpty()); | |
} | |
@Test | |
public void testNoHit() throws Exception { | |
// there is no "Requirement" in UML | |
Set<Hit> hits = ElasticFinderImpl.onDummy(TestData.uml()).search("Requirement", null); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertTrue("empty set expected", hits.isEmpty()); | |
} | |
@Test | |
public void testHit() throws Exception { | |
// we should find the "Component" class | |
Set<Hit> hits = ElasticFinderImpl.onDummy(TestData.uml()).search("Component", null); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertFalse("filled set expected", hits.isEmpty()); | |
Hit firstHit = hits.iterator().next(); | |
Assert.assertTrue("score must be > 0", firstHit.score > 0); | |
} | |
@Test | |
public void testHitWithExpression() throws Exception { | |
Set<Hit> hits = ElasticFinderImpl.onDummy(TestData.uml()).search(".*ompon.*", null); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertFalse("filled set expected", hits.isEmpty()); | |
} | |
@Test | |
public void testHitWithNegativeFilter() throws Exception { | |
// we use UML meta model for testing | |
Map<String, Object> filters = new HashMap<>(); | |
filters.put("abstract", Boolean.TRUE); | |
Set<Hit> hits = ElasticFinderImpl.onDummy(TestData.uml()).search("Component", filters); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertTrue("empty set expected - Component is not abstract", hits.isEmpty()); | |
} | |
@Test | |
public void testHitWithPositiveFilter() throws Exception { | |
Map<String, Object> filters = new HashMap<>(); | |
filters.put("abstract", Boolean.FALSE); | |
filters.put("interface", Boolean.FALSE); | |
Set<Hit> hits = ElasticFinderImpl.onDummy(TestData.uml()).search("Component", filters); | |
Assert.assertNotNull("set expected", hits); | |
Assert.assertFalse("filled set expected - Component is not abstract", hits.isEmpty()); | |
Hit firstHit = hits.iterator().next(); | |
Assert.assertTrue("score must be > 1 (due to filters matched)", firstHit.score > 1); | |
System.out.println(hits); | |
} | |
} |