Skip to content

Commit

Permalink
add showcase tests. expose logging interceptors as public to setup te…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
zhumin8 committed Dec 10, 2024
1 parent 83eedf0 commit d85c848
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 172 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ jobs:
-P enable-integration-tests \
--batch-mode \
--no-transfer-progress
# The `envVarLoggingTest` profile runs tests that require an environment variable
- name: Showcase integration tests - Logging
run: |
verify -P '!showcase,enable-integration-tests,envVarLoggingTest'
--batch-mode \
--no-transfer-progress
# Set the Env Var for this step only
env:
GOOGLE_SDK_JAVA_LOGGING: true

showcase-native:
runs-on: ubuntu-22.04
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.slf4j.Logger;
import org.slf4j.event.Level;

class GrpcLoggingInterceptor implements ClientInterceptor {
public class GrpcLoggingInterceptor implements ClientInterceptor {

private static final Logger logger = LoggingUtils.getLogger(GrpcLoggingInterceptor.class);
private static final Gson gson = new Gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.slf4j.Logger;
import org.slf4j.event.Level;

class HttpJsonLoggingInterceptor implements HttpJsonClientInterceptor {
public class HttpJsonLoggingInterceptor implements HttpJsonClientInterceptor {

private static final Logger logger = LoggingUtils.getLogger(HttpJsonLoggingInterceptor.class);
private static final Gson gson = new Gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@
import com.google.api.gax.rpc.internal.EnvironmentProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLogger;

public class LoggingUtilsTest {
class LoggingUtilsTest {

private EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);

@BeforeEach
public void setup() {
void setup() {
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);

// need to setup a ConsoleAppender and attach to root logger because TestAppender
Expand All @@ -83,31 +84,31 @@ public void setup() {
}

@AfterEach
public void tearDown() {
void tearDown() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).detachAppender("CONSOLE");
}

@org.junit.Test
public void testGetLogger_loggingEnabled_slf4jBindingPresent() {
@Test
void testGetLogger_loggingEnabled_slf4jBindingPresent() {
Mockito.when(envProvider.getenv(LoggingUtils.GOOGLE_SDK_JAVA_LOGGING)).thenReturn("true");
LoggingUtils.setEnvironmentProvider(envProvider);
Logger logger = LoggingUtils.getLogger(LoggingUtilsTest.class);
assertTrue(logger instanceof org.slf4j.Logger);
assertNotEquals(logger.getClass(), NOPLogger.class);
}

@org.junit.Test
public void testGetLogger_loggingDisabled() {
@Test
void testGetLogger_loggingDisabled() {
Mockito.when(envProvider.getenv(LoggingUtils.GOOGLE_SDK_JAVA_LOGGING)).thenReturn("false");
LoggingUtils.setEnvironmentProvider(envProvider);

Logger logger = LoggingUtils.getLogger(LoggingUtilsTest.class);
assertEquals(NOPLogger.class, logger.getClass());
}

@org.junit.Test
public void testGetLogger_loggingEnabled_noBinding() {
@Test
void testGetLogger_loggingEnabled_noBinding() {
Mockito.when(envProvider.getenv(LoggingUtils.GOOGLE_SDK_JAVA_LOGGING)).thenReturn("true");
LoggingUtils.setEnvironmentProvider(envProvider);
// Create a mock LoggerFactoryProvider
Expand All @@ -124,8 +125,8 @@ public void testGetLogger_loggingEnabled_noBinding() {
assertTrue(logger instanceof org.slf4j.helpers.NOPLogger);
}

@org.junit.Test
public void testIsLoggingEnabled_true() {
@Test
void testIsLoggingEnabled_true() {
Mockito.when(envProvider.getenv(LoggingUtils.GOOGLE_SDK_JAVA_LOGGING)).thenReturn("true");
LoggingUtils.setEnvironmentProvider(envProvider);
assertTrue(LoggingUtils.isLoggingEnabled());
Expand All @@ -137,8 +138,8 @@ public void testIsLoggingEnabled_true() {
assertTrue(LoggingUtils.isLoggingEnabled());
}

@org.junit.Test
public void testIsLoggingEnabled_defaultToFalse() {
@Test
void testIsLoggingEnabled_defaultToFalse() {
LoggingUtils.setEnvironmentProvider(envProvider);
assertFalse(LoggingUtils.isLoggingEnabled());
}
Expand Down
24 changes: 1 addition & 23 deletions showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,7 @@
<scope>test</scope>
</dependency>

<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<!-- Logging testing dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand All @@ -232,23 +227,6 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.11</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,39 @@
import static com.google.common.truth.Truth.assertThat;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.google.api.gax.grpc.GrpcLoggingInterceptor;
import com.google.api.gax.httpjson.HttpJsonLoggingInterceptor;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.it.util.TestAppender;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

// This test needs to run with GOOGLE_SDK_JAVA_LOGGING=true
public class ITLogging {
private static EchoClient grpcClient;

private static EchoClient httpjsonClient;
static final Logger LOGGER = Logger.getLogger(ITUnaryCallable.class.getName());

private TestAppender setupTestLogger(Class<?> clazz) {
TestAppender testAppender = new TestAppender();
testAppender.start();
org.slf4j.Logger logger = LoggerFactory.getLogger(clazz);
((ch.qos.logback.classic.Logger) logger).addAppender(testAppender);
return testAppender;
}

@BeforeAll
static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
// Create Http JSON Echo Client
httpjsonClient = TestClientInitializer.createHttpJsonEchoClient();

LOGGER.log(Level.INFO, "This is log message directly from JUL. Clients created.");
}

@AfterAll
Expand All @@ -60,95 +66,37 @@ static void destroyClients() throws InterruptedException {

@Test
void testGrpc_receiveContent_logDebug() {
LOGGER.log(
Level.INFO,
"This is log message directly from JUL. Starting test: testGrpc_receiveContent.");

TestAppender.clearEvents();
TestAppender testAppender = setupTestLogger(GrpcLoggingInterceptor.class);
assertThat(echoGrpc("grpc-echo?")).isEqualTo("grpc-echo?");
assertThat(TestAppender.events.size()).isEqualTo(3);
assertThat(TestAppender.events.get(0).getMessage()).isEqualTo("Sending gRPC request");
assertThat(TestAppender.events.get(0).getLevel()).isEqualTo(ch.qos.logback.classic.Level.DEBUG);
assertThat(TestAppender.events.get(1).getMessage())
.isEqualTo("Sending gRPC request: request payload");
assertThat(TestAppender.events.get(2).getMessage()).isEqualTo("Received Grpc response");
assertThat(testAppender.events.size()).isEqualTo(2);
assertThat(testAppender.events.get(0).getMessage())
.isEqualTo(
"{\"serviceName\":\"google.showcase.v1beta1.Echo\",\"message\":\"Sending gRPC request\",\"rpcName\":\"google.showcase.v1beta1.Echo/Echo\"}");
assertThat(testAppender.events.get(0).getLevel()).isEqualTo(ch.qos.logback.classic.Level.INFO);
assertThat(testAppender.events.get(1).getMessage())
.isEqualTo(
"{\"serviceName\":\"google.showcase.v1beta1.Echo\",\"response.status\":\"OK\",\"message\":\"Received Grpc response\",\"rpcName\":\"google.showcase.v1beta1.Echo/Echo\"}");
testAppender.stop();
}

// @Test
// void testGrpc_receiveContent_logInfo() {
// ch.qos.logback.classic.Logger logger =
// (ch.qos.logback.classic.Logger) LoggingUtils.getLogger(GrpcLoggingInterceptor.class);
// ch.qos.logback.classic.Level originalLevel = logger.getLevel();
// try {
// logger.setLevel(ch.qos.logback.classic.Level.INFO);
// assertThat(logger.getLevel()).isEqualTo(ch.qos.logback.classic.Level.INFO);
//
// TestAppender.clearEvents();
// assertThat(echoGrpc("grpc-echo?")).isEqualTo("grpc-echo?");
// assertThat(TestAppender.events.size()).isEqualTo(2);
// ILoggingEvent loggingEvent1 = TestAppender.events.get(0);
// assertThat(loggingEvent1.getMessage()).isEqualTo("Sending gRPC request");
// assertThat(loggingEvent1.getLevel()).isEqualTo(ch.qos.logback.classic.Level.INFO);
// assertThat(loggingEvent1.getMDCPropertyMap()).hasSize(3);
// assertThat(loggingEvent1.getMDCPropertyMap())
// .containsEntry("serviceName", "google.showcase.v1beta1.Echo");
// assertThat(loggingEvent1.getMDCPropertyMap())
// .containsEntry("rpcName", "google.showcase.v1beta1.Echo/Echo");
// assertThat(TestAppender.events.get(1).getMessage()).isEqualTo("Received Grpc response");
// assertThat(TestAppender.events.get(1).getLevel())
// .isEqualTo(ch.qos.logback.classic.Level.INFO);
// } finally {
// logger.setLevel(originalLevel);
// }
// }

@Test
void testHttpJson_receiveContent_logDebug() {
TestAppender.clearEvents();
TestAppender testAppender = setupTestLogger(HttpJsonLoggingInterceptor.class);
assertThat(echoHttpJson("http-echo?")).isEqualTo("http-echo?");
assertThat(TestAppender.events.size()).isEqualTo(3);
ILoggingEvent loggingEvent1 = TestAppender.events.get(0);
assertThat(loggingEvent1.getMessage()).isEqualTo("Sending HTTP request");
assertThat(loggingEvent1.getLevel()).isEqualTo(ch.qos.logback.classic.Level.DEBUG);
assertThat(loggingEvent1.getMDCPropertyMap()).hasSize(5);
assertThat(loggingEvent1.getMDCPropertyMap()).containsKey("request.headers");
assertThat(TestAppender.events.get(1).getMessage())
.isEqualTo("Sending HTTP request: request payload");
assertThat(TestAppender.events.get(2).getMessage()).isEqualTo("Received HTTP response");
assertThat(testAppender.events.size()).isEqualTo(2);
ILoggingEvent loggingEvent1 = testAppender.events.get(0);
assertThat(loggingEvent1.getMessage())
.isEqualTo(
"{\"request.method\":\"POST\",\"request.url\":\"http://localhost:7469\",\"message\":\"Sending HTTP request\",\"rpcName\":\"google.showcase.v1beta1.Echo/Echo\"}");
assertThat(loggingEvent1.getLevel()).isEqualTo(ch.qos.logback.classic.Level.INFO);
assertThat(loggingEvent1.getMDCPropertyMap()).hasSize(3);
assertThat(loggingEvent1.getMDCPropertyMap()).containsKey("rpcName");
assertThat(testAppender.events.get(1).getMessage())
.isEqualTo(
"{\"response.status\":\"200\",\"message\":\"Received HTTP response\",\"rpcName\":\"google.showcase.v1beta1.Echo/Echo\"}");
testAppender.stop();
}

// @Test
// void testHttpJson_receiveContent_logInfo() {
//
// ch.qos.logback.classic.Logger logger =
// (ch.qos.logback.classic.Logger) LoggingUtils.getLogger(HttpJsonLoggingInterceptor.class);
// ch.qos.logback.classic.Level originalLevel = logger.getLevel();
// try {
// logger.setLevel(ch.qos.logback.classic.Level.INFO);
// assertThat(logger.getLevel()).isEqualTo(ch.qos.logback.classic.Level.INFO);
//
// TestAppender.clearEvents();
// assertThat(echoHttpJson("http-echo?")).isEqualTo("http-echo?");
// assertThat(TestAppender.events.size()).isEqualTo(2);
// ILoggingEvent loggingEvent1 = TestAppender.events.get(0);
// assertThat(loggingEvent1.getMessage()).isEqualTo("Sending HTTP request");
// assertThat(loggingEvent1.getLevel()).isEqualTo(ch.qos.logback.classic.Level.INFO);
// assertThat(loggingEvent1.getMDCPropertyMap()).hasSize(4);
// assertThat(loggingEvent1.getMDCPropertyMap())
// .containsEntry("rpcName", "google.showcase.v1beta1.Echo/Echo");
// assertThat(loggingEvent1.getMDCPropertyMap()).containsEntry("request.method", "POST");
// assertThat(loggingEvent1.getMDCPropertyMap())
// .containsEntry("request.url", "http://localhost:7469");
// assertThat(TestAppender.events.get(1).getMessage()).isEqualTo("Received HTTP response");
// assertThat(TestAppender.events.get(1).getLevel())
// .isEqualTo(ch.qos.logback.classic.Level.INFO);
// assertThat(TestAppender.events.get(1).getMDCPropertyMap())
// .containsEntry("response.status", "200");
// } finally {
// logger.setLevel(originalLevel);
// }
// }

private String echoGrpc(String value) {
EchoResponse response = grpcClient.echo(EchoRequest.newBuilder().setContent(value).build());
return response.getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -39,16 +37,13 @@ class ITUnaryCallable {
private static EchoClient grpcClient;

private static EchoClient httpjsonClient;
static final Logger LOGGER = Logger.getLogger(ITUnaryCallable.class.getName());

@BeforeAll
static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
// Create Http JSON Echo Client
httpjsonClient = TestClientInitializer.createHttpJsonEchoClient();

LOGGER.log(Level.INFO, "This is log message directly from JUL. Clients created.");
}

@AfterAll
Expand All @@ -63,9 +58,6 @@ static void destroyClients() throws InterruptedException {

@Test
void testGrpc_receiveContent() {
LOGGER.log(
Level.INFO,
"This is log message directly from JUL. Starting test: testGrpc_receiveContent.");
assertThat(echoGrpc("grpc-echo?")).isEqualTo("grpc-echo?");
assertThat(echoGrpc("grpc-echo!")).isEqualTo("grpc-echo!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/** Logback appender used to set up tests. */
public class TestAppender extends AppenderBase<ILoggingEvent> {
public static List<ILoggingEvent> events = new ArrayList<>();
public List<ILoggingEvent> events = new ArrayList<>();

@Override
protected void append(ILoggingEvent eventObject) {
Expand All @@ -33,7 +33,7 @@ protected void append(ILoggingEvent eventObject) {

events.add(eventObject);
}
public static void clearEvents() {
public void clearEvents() {
events.clear();
}
}
Expand Down
Loading

0 comments on commit d85c848

Please sign in to comment.