Skip to content

Commit

Permalink
Support for kotlin and JUnit5 with mockito (#29)
Browse files Browse the repository at this point in the history
Updated unit tests to JUnit5

Signed-off-by: @akbhatta
  • Loading branch information
akbhatta authored Jun 3, 2021
1 parent 357cbd0 commit 596534e
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 41 deletions.
68 changes: 67 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ buildscript {
ext {
opensearch_group = "org.opensearch"
opensearch_version = System.getProperty("opensearch.version", "1.0.0-rc1")
kotlin_version = System.getProperty("kotlin.version", "1.5.10")
}

repositories {
Expand All @@ -24,6 +25,9 @@ buildscript {

dependencies {
classpath "${opensearch_group}.gradle:build-tools:${opensearch_version}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
classpath "org.jetbrains.kotlin:kotlin-allopen:${kotlin_version}"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.17.1"
}
}

Expand All @@ -49,11 +53,37 @@ apply plugin: 'jacoco'
apply plugin: 'signing'
apply plugin: 'maven-publish'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'io.gitlab.arturbosch.detekt'
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'org.jetbrains.kotlin.plugin.allopen'

configurations {
ktlint
}

dependencies {
compileOnly "org.opensearch.client:opensearch-rest-high-level-client:${opensearch_version}"
compile "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
compile "org.jetbrains.kotlin:kotlin-stdlib-common:${kotlin_version}"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0" // ${kotlin_version} may not work for some versions
testCompile "org.opensearch.test:framework:${opensearch_version}"
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
testCompile "org.mockito:mockito-core:3.10.0"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.mockito:mockito-junit-jupiter:3.10.0'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'

ktlint "com.pinterest:ktlint:0.41.0"
}

test {
useJUnitPlatform()
testLogging {
exceptionFormat "full"
events "skipped", "passed", "failed" // "started"
showStandardStreams true
}
}

spotless {
Expand All @@ -65,6 +95,42 @@ spotless {
eclipse().configFile rootProject.file('.eclipseformat.xml')
}
}
detekt {
config = files("detekt.yml")
buildUponDefaultConfig = true
}

task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
main = "com.pinterest.ktlint.Main"
classpath = configurations.ktlint
args "src/**/*.kt"
// to generate report in checkstyle format prepend following args:
// "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
// see https://github.com/pinterest/ktlint#usage for more
}

check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
main = "com.pinterest.ktlint.Main"
classpath = configurations.ktlint
args "-F", "src/**/*.kt"
}

compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = "1.8"
}
}

compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

shadowJar {
classifier = null
Expand Down
23 changes: 23 additions & 0 deletions detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
#
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#

style:
ForbiddenComment:
active: false
MaxLineLength:
maxLineLength: 150
ThrowsCount:
active: true
max: 10
ReturnCount:
active: true
max: 10
11 changes: 5 additions & 6 deletions src/test/java/org/opensearch/commons/InjectSecurityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@

package org.opensearch.commons;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.opensearch.commons.ConfigConstants.INJECTED_USER;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_INJECTED_ROLES;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS;

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;

Expand All @@ -48,7 +47,7 @@ public void testUsersRolesEmpty() {
try (InjectSecurity helper = new InjectSecurity("test-name", Settings.EMPTY, tc)) {
helper.inject("", null);
}
Assert.assertNull(tc.getTransient(OPENSEARCH_SECURITY_INJECTED_ROLES));
assertNull(tc.getTransient(OPENSEARCH_SECURITY_INJECTED_ROLES));
}

@Test
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/org/opensearch/commons/authuser/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@

package org.opensearch.commons.authuser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT;

import java.io.IOException;
import java.util.Arrays;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput;
Expand All @@ -59,7 +59,7 @@ public void testEmptyConst() {
assertEquals(0, user.getBackendRoles().size());
assertEquals(0, user.getRoles().size());
assertEquals(0, user.getCustomAttNames().size());
assertEquals(null, user.getRequestedTenant());
assertNull(user.getRequestedTenant());
}

@Test
Expand Down Expand Up @@ -115,8 +115,8 @@ public void testStreamConstForNoTenantUser() throws IOException {
user.writeTo(out);
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
User newUser = new User(in);
assertEquals("Round tripping User doesn't work", user.toString(), newUser.toString());
assertEquals("Round tripping User doesn't work", user, newUser);
assertEquals(user.toString(), newUser.toString(), "Round tripping User doesn't work");
assertEquals(user, newUser, "Round tripping User doesn't work");
}

@Test
Expand All @@ -126,8 +126,8 @@ public void testStreamConstForTenantUser() throws IOException {
user.writeTo(out);
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
User newUser = new User(in);
assertEquals("Round tripping User doesn't work", user.toString(), newUser.toString());
assertEquals("Round tripping User doesn't work", user, newUser);
assertEquals(user.toString(), newUser.toString(), "Round tripping User doesn't work");
assertEquals(user, newUser, "Round tripping User doesn't work");
}

@Test
Expand All @@ -150,7 +150,7 @@ public void testParseUserStringEmpty() {
ThreadContext tc = new ThreadContext(Settings.EMPTY);
String str = tc.getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
User user = User.parse(str);
assertEquals(null, user);
assertNull(user);
}

@Test
Expand Down Expand Up @@ -221,6 +221,6 @@ public void testParseUserStringMalformed() {
tc.putTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT, "|backendrole1,backendrole2|role1,role2");
String str = tc.getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
User user = User.parse(str);
assertEquals(null, user);
assertNull(user);
}
}
10 changes: 5 additions & 5 deletions src/test/java/org/opensearch/commons/rest/IntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

package org.opensearch.commons.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_ENABLED;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_FILEPATH;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_KEYPASSWORD;
Expand All @@ -39,15 +39,15 @@
import java.nio.file.Paths;

import org.apache.http.util.EntityUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.opensearch.client.Request;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;
import org.opensearch.common.settings.Settings;

@Ignore("Enable this after integration with security plugin is done")
@Disabled("Enable this after integration with security plugin is done")
public class IntegrationTests {

private Request createSampleRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@

package org.opensearch.commons.rest;

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

import java.io.File;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.opensearch.OpenSearchException;
import org.opensearch.client.RestClient;
import org.opensearch.common.settings.Settings;
Expand All @@ -42,7 +44,7 @@ public void testHttpRestClient() throws Exception {
Settings settings = Settings.builder().put("http.port", 9200).put("plugins.security.ssl.http.enabled", false).build();
SecureRestClientBuilder builder = new SecureRestClientBuilder(settings, null);
RestClient restClient = builder.build();
Assert.assertNotNull(restClient);
Assertions.assertNotNull(restClient);
restClient.close();
}

Expand All @@ -60,7 +62,7 @@ public void testHttpsRestClient() throws Exception {

SecureRestClientBuilder builder = new SecureRestClientBuilder(settings, Paths.get(configFolder));
RestClient restClient = builder.build();
Assert.assertNotNull(restClient);
Assertions.assertNotNull(restClient);
restClient.close();
}

Expand All @@ -72,14 +74,16 @@ public void testMissingPem() throws Exception {
new SecureRestClientBuilder(settings, Paths.get(configFolder)).build();
}

@Test(expected = OpenSearchException.class)
public void testMissingConfigPath() throws Exception {
Settings settings = Settings
.builder()
.put("http.port", 9200)
.put("plugins.security.ssl.http.enabled", true)
.put("plugins.security.ssl.http.pemcert_filepath", "sample.pem")
.build();
new SecureRestClientBuilder(settings, Paths.get("sample.pem")).build();
@Test
public void testMissingConfigPath() {
assertThrows(OpenSearchException.class, () -> {
Settings settings = Settings
.builder()
.put("http.port", 9200)
.put("plugins.security.ssl.http.enabled", true)
.put("plugins.security.ssl.http.pemcert_filepath", "sample.pem")
.build();
new SecureRestClientBuilder(settings, Paths.get("sample.pem")).build();
});
}
}
8 changes: 4 additions & 4 deletions src/test/java/org/opensearch/commons/rest/TrustStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

package org.opensearch.commons.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.security.KeyStore;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public class TrustStoreTest {

Expand Down

0 comments on commit 596534e

Please sign in to comment.