Skip to content

Commit

Permalink
[ZEPPELIN-5954] migrate elasticsearch to Junit5 (#4648)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reamer authored Sep 11, 2023
1 parent b1fbb8a commit 6181bfe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 56 deletions.
5 changes: 2 additions & 3 deletions elasticsearch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest.all.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@
package org.apache.zeppelin.elasticsearch;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.apache.commons.lang3.RandomUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -43,6 +41,7 @@
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.apache.zeppelin.completer.CompletionType;
import org.apache.zeppelin.display.AngularObjectRegistry;
Expand All @@ -51,10 +50,9 @@
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;

@RunWith(Theories.class)
public class ElasticsearchInterpreterTest {
@DataPoint public static ElasticsearchInterpreter transportInterpreter;
@DataPoint public static ElasticsearchInterpreter httpInterpreter;
public static ElasticsearchInterpreter transportInterpreter;
public static ElasticsearchInterpreter httpInterpreter;

private static Client elsClient;
private static Node elsNode;
Expand All @@ -70,7 +68,7 @@ public class ElasticsearchInterpreterTest {

private static final AtomicInteger deleteId = new AtomicInteger(2);

@BeforeClass
@BeforeAll
public static void populate() throws IOException {
final Settings settings = Settings.settingsBuilder()
.put("cluster.name", ELS_CLUSTER_NAME)
Expand Down Expand Up @@ -140,7 +138,7 @@ public static void populate() throws IOException {
httpInterpreter.open();
}

@AfterClass
@AfterAll
public static void clean() {
if (transportInterpreter != null) {
transportInterpreter.close();
Expand Down Expand Up @@ -168,8 +166,15 @@ private InterpreterContext buildContext(String noteAndParagraphId) {
.build();
}

@Theory
public void testCount(ElasticsearchInterpreter interpreter) {
private static Stream<Arguments> provideInterpreter() {
return Stream.of(
Arguments.of(transportInterpreter),
Arguments.of(httpInterpreter));
}

@ParameterizedTest
@MethodSource("provideInterpreter")
void testCount(ElasticsearchInterpreter interpreter) {
final InterpreterContext ctx = buildContext("testCount");

InterpreterResult res = interpreter.interpret("count /unknown", ctx);
Expand All @@ -186,8 +191,9 @@ public void testCount(ElasticsearchInterpreter interpreter) {
assertEquals(Code.SUCCESS, res.code());
}

@Theory
public void testGet(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testGet(ElasticsearchInterpreter interpreter) {
final InterpreterContext ctx = buildContext("get");

InterpreterResult res = interpreter.interpret("get /logs/http/unknown", ctx);
Expand All @@ -209,8 +215,9 @@ public void testGet(ElasticsearchInterpreter interpreter) {
assertEquals(Code.SUCCESS, res.code());
}

@Theory
public void testSearch(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testSearch(ElasticsearchInterpreter interpreter) {
final InterpreterContext ctx = buildContext("search");

InterpreterResult res = interpreter.interpret("size 10\nsearch /logs *", ctx);
Expand All @@ -231,8 +238,9 @@ public void testSearch(ElasticsearchInterpreter interpreter) {
assertEquals(Code.SUCCESS, res.code());
}

@Theory
public void testAgg(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testAgg(ElasticsearchInterpreter interpreter) {
final InterpreterContext ctx = buildContext("agg");

// Single-value metric
Expand Down Expand Up @@ -265,8 +273,9 @@ public void testAgg(ElasticsearchInterpreter interpreter) {
assertEquals(Code.SUCCESS, res.code());
}

@Theory
public void testIndex(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testIndex(ElasticsearchInterpreter interpreter) {
InterpreterResult res = interpreter.interpret("index /logs { \"date\": \"" + new Date() +
"\", \"method\": \"PUT\", \"status\": \"500\" }", null);
assertEquals(Code.ERROR, res.code());
Expand All @@ -283,8 +292,9 @@ public void testIndex(ElasticsearchInterpreter interpreter) {
assertEquals(Code.SUCCESS, res.code());
}

@Theory
public void testDelete(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testDelete(ElasticsearchInterpreter interpreter) {
InterpreterResult res = interpreter.interpret("delete /logs/http/unknown", null);
assertEquals(Code.ERROR, res.code());

Expand All @@ -297,17 +307,19 @@ public void testDelete(ElasticsearchInterpreter interpreter) {
assertEquals("" + testDeleteId, res.message().get(0).getData());
}

@Theory
public void testMisc(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testMisc(ElasticsearchInterpreter interpreter) {
InterpreterResult res = interpreter.interpret(null, null);
assertEquals(Code.SUCCESS, res.code());

res = interpreter.interpret(" \n \n ", null);
assertEquals(Code.SUCCESS, res.code());
}

@Theory
public void testCompletion(ElasticsearchInterpreter interpreter) {
@ParameterizedTest
@MethodSource("provideInterpreter")
void testCompletion(ElasticsearchInterpreter interpreter) {
final List<InterpreterCompletion> expectedResultOne = Arrays.asList(
new InterpreterCompletion("count", "count", CompletionType.command.name()));
final List<InterpreterCompletion> expectedResultTwo = Arrays.asList(
Expand All @@ -317,13 +329,13 @@ public void testCompletion(ElasticsearchInterpreter interpreter) {
final List<InterpreterCompletion> resultTwo = interpreter.completion("he", 0, null);
final List<InterpreterCompletion> resultAll = interpreter.completion("", 0, null);

Assert.assertEquals(expectedResultOne, resultOne);
Assert.assertEquals(expectedResultTwo, resultTwo);
assertEquals(expectedResultOne, resultOne);
assertEquals(expectedResultTwo, resultTwo);

final List<String> allCompletionList = new ArrayList<>();
for (final InterpreterCompletion ic : resultAll) {
allCompletionList.add(ic.getName());
}
Assert.assertEquals(ElasticsearchInterpreter.COMMANDS, allCompletionList);
assertEquals(ElasticsearchInterpreter.COMMANDS, allCompletionList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,69 @@

package org.apache.zeppelin.elasticsearch.client;

import org.junit.Test;

import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.HTTP;
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.HTTPS;
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.TRANSPORT;
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.UNKNOWN;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ElasticsearchClientTypeBuilderTest {
import org.junit.jupiter.api.Test;

class ElasticsearchClientTypeBuilderTest {

@Test
public void it_should_return_transport_as_default_value_when_property_is_empty() {
void it_should_return_transport_as_default_value_when_property_is_empty() {
//GIVEN
String empty = "";
//WHEN
ElasticsearchClientType clientType =
ElasticsearchClientTypeBuilder.withPropertyValue(empty).build();
//THEN
assertThat(clientType, is(TRANSPORT));
assertEquals(TRANSPORT, clientType);
}

@Test
public void it_should_return_transport_as_default_value_when_property_is_null() {
void it_should_return_transport_as_default_value_when_property_is_null() {
//GIVEN
String nullValue = null;
//WHEN
ElasticsearchClientType clientType =
ElasticsearchClientTypeBuilder.withPropertyValue(nullValue).build();
//THEN
assertThat(clientType, is(TRANSPORT));
assertEquals(TRANSPORT, clientType);
}

@Test
public void it_should_return_client_type_when_property_value_exists() {
void it_should_return_client_type_when_property_value_exists() {
//GIVEN
String clientType = "https";
//WHEN
ElasticsearchClientType esClientType =
ElasticsearchClientTypeBuilder.withPropertyValue(clientType).build();
//THEN
assertThat(esClientType, is(HTTPS));
assertEquals(HTTPS, esClientType);
}

@Test
public void it_should_return_client_type_and_ignore_case_when_property_value_exists() {
void it_should_return_client_type_and_ignore_case_when_property_value_exists() {
//GIVEN
String clientType = "hTtP";
//WHEN
ElasticsearchClientType esClientType =
ElasticsearchClientTypeBuilder.withPropertyValue(clientType).build();
//THEN
assertThat(esClientType, is(HTTP));
assertEquals(HTTP, esClientType);
}

@Test
public void it_should_return_unknown_when_property_value_does_not_exist() {
void it_should_return_unknown_when_property_value_does_not_exist() {
//GIVEN
String unknownValue = "an_unknown_value";
//WHEN
ElasticsearchClientType esClientType =
ElasticsearchClientTypeBuilder.withPropertyValue(unknownValue).build();
//THEN
assertThat(esClientType, is(UNKNOWN));
assertEquals(UNKNOWN, esClientType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

package org.apache.zeppelin.elasticsearch.client;

import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.junit.jupiter.api.Test;

public class ElasticsearchClientTypeTest {
class ElasticsearchClientTypeTest {

@Test
public void it_should_return_http_when_reducing_on_http_types() {
void it_should_return_http_when_reducing_on_http_types() {
//GIVEN
List<ElasticsearchClientType> httpTypes =
new ArrayList<>(Arrays.asList(ElasticsearchClientType.HTTP, ElasticsearchClientType.HTTPS));
Expand All @@ -38,6 +38,6 @@ public void it_should_return_http_when_reducing_on_http_types() {
.map(ElasticsearchClientType::isHttp)
.reduce(true, (ident, elasticsearchClientType) -> ident && elasticsearchClientType);
//THEN
assertThat(httpTypesReduced, is(true));
assertTrue(httpTypesReduced);
}
}

0 comments on commit 6181bfe

Please sign in to comment.