Skip to content
Permalink
Browse files
fix: disable logback, use log4j2 (#46)
  • Loading branch information
tzssangglass committed Jul 1, 2021
1 parent 918e85a commit d6bc7fa86c62a9967b0bdf05bb00554249226c5c
Showing 6 changed files with 101 additions and 77 deletions.
@@ -112,6 +112,7 @@
<configuration>
<skip>false</skip>
<configLocation>${maven.multiModuleProjectDirectory}/src/main/checkstyle/checkstyle.xml</configLocation>
<suppressionsLocation>${maven.multiModuleProjectDirectory}/src/main/checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
<headerLocation>${maven.multiModuleProjectDirectory}/src/main/checkstyle/HEADER</headerLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
@@ -27,20 +27,15 @@
import org.apache.apisix.plugin.runner.A6Request;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;

import java.nio.ByteBuffer;

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

@ExtendWith(OutputCaptureExtension.class)
@DisplayName("test decode data")
class FlatBuffersDecoderTest {

@@ -53,65 +48,54 @@ void setUp() {
}

@Test
@Disabled
@DisplayName("test empty data")
void testEmptyData(CapturedOutput capturedOutput) {
void testEmptyData() {
byte[] bytes = new byte[]{};
ByteBuffer buffer = ByteBuffer.wrap(bytes);
A6Request result = flatBuffersDecoder.decode(buffer);
Assertions.assertEquals(Code.BAD_REQUEST, ((A6ErrRequest) result).getCode());
Assertions.assertTrue(capturedOutput.getOut().contains("receive empty data"));
}

@Test
@Disabled
@DisplayName("test unsupported type")
void testUnsupportedType(CapturedOutput capturedOutput) {
void testUnsupportedType() {
byte[] bytes = new byte[]{4};
ByteBuffer buffer = ByteBuffer.wrap(bytes);
A6Request result = flatBuffersDecoder.decode(buffer);
Assertions.assertEquals(Code.BAD_REQUEST, ((A6ErrRequest) result).getCode());
Assertions.assertTrue(capturedOutput.getOut().contains("receive unsupported type: 4"));
}

@Test
@Disabled
@DisplayName("test error data length(1)")
void testErrorDataLength1(CapturedOutput capturedOutput) {
void testErrorDataLength1() {
// data length is greater than actual length
byte[] bytes = new byte[]{1, 0, 0, 3, 0};
ByteBuffer buffer = ByteBuffer.wrap(bytes);
A6Request result = flatBuffersDecoder.decode(buffer);
Assertions.assertEquals(Code.BAD_REQUEST, ((A6ErrRequest) result).getCode());
Assertions.assertTrue(capturedOutput.getOut().contains("receive error data length"));
}

@Test
@Disabled
@DisplayName("test error data length(2)")
void testErrorDataLength2(CapturedOutput capturedOutput) {
void testErrorDataLength2() {
// data length equal to 0
byte[] bytes = new byte[]{1, 0, 0, 0, 0};
ByteBuffer buffer = ByteBuffer.wrap(bytes);
A6Request result = flatBuffersDecoder.decode(buffer);
Assertions.assertEquals(Code.BAD_REQUEST, ((A6ErrRequest) result).getCode());
Assertions.assertTrue(capturedOutput.getOut().contains("receive error data length"));
}

@Test
@Disabled
@DisplayName("test error data length(3)")
void testErrorDataLength3(CapturedOutput capturedOutput) {
void testErrorDataLength3() {
// wrong data content
byte[] bytes = new byte[]{1, 0, 0, 1, 0, 1};
ByteBuffer buffer = ByteBuffer.wrap(bytes);
A6Request result = flatBuffersDecoder.decode(buffer);
Assertions.assertEquals(Code.BAD_REQUEST, ((A6ErrRequest) result).getCode());
Assertions.assertTrue(capturedOutput.getOut().contains("receive error data length"));
}

@Test
@Disabled
@DisplayName("test get body")
void testGetBody() {
// mock client assembly data
@@ -31,19 +31,14 @@
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@ExtendWith(OutputCaptureExtension.class)
@DisplayName("test add filter")
class A6ConfigHandlerTest {

@@ -135,9 +130,8 @@ void testAddFilter2() {
}

@Test
@Disabled
@DisplayName("test skip the same name filter")
void testAddFilter3(CapturedOutput capturedOutput) {
void testAddFilter3() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int foo1 = builder.createString("FooFilter");
int bar1 = builder.createString("Bar1");
@@ -159,13 +153,11 @@ void testAddFilter3(CapturedOutput capturedOutput) {

A6Conf config = cache.getIfPresent(0L);
Assertions.assertEquals(config.getChain().getFilters().size(), 1);
Assertions.assertTrue(capturedOutput.getOut().contains("skip the same filter: FooFilter"));
}

@Test
@Disabled
@DisplayName("test receive undefined filter")
void testAddFilter4(CapturedOutput capturedOutput) {
void testAddFilter4() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int foo = builder.createString("UndefinedFilter");
int bar = builder.createString("Bar");
@@ -183,6 +175,5 @@ void testAddFilter4(CapturedOutput capturedOutput) {

A6Conf config = cache.getIfPresent(0L);
Assertions.assertEquals(config.getChain().getFilters().size(), 0);
Assertions.assertTrue(capturedOutput.getOut().contains("receive undefined filter: UndefinedFilter, skip it"));
}
}
@@ -31,27 +31,24 @@
import org.apache.apisix.plugin.runner.HttpResponse;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import reactor.core.publisher.Mono;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@ExtendWith(OutputCaptureExtension.class)
@DisplayName("test filter handle")
class A6HttpCallHandlerTest {
private final Logger logger = LoggerFactory.getLogger(A6HttpCallHandlerTest.class);
private PrintStream console = null;
private ByteArrayOutputStream bytes = null;

A6HttpCallHandler a6HttpCallHandler;

@@ -61,6 +58,11 @@ class A6HttpCallHandlerTest {

@BeforeEach
void setUp() {

bytes = new ByteArrayOutputStream();
console = System.out;
System.setOut(new PrintStream(bytes));

filters = new HashMap<>();
filters.put("FooFilter", new PluginFilter() {
@Override
@@ -71,33 +73,33 @@ public String name() {
@Override
@SuppressWarnings("unchecked")
public Mono<Void> filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
logger.info("do filter: FooFilter, order: {}", chain.getIndex());
logger.info("do filter: FooFilter, config: {}", request.getConfig(this));
System.out.println("do filter: FooFilter, order: " + chain.getIndex());
System.out.println("do filter: FooFilter, config: " + request.getConfig(this));
Gson gson = new Gson();
Map<String, Object> conf = new HashMap<>();
conf = gson.fromJson(request.getConfig(this), conf.getClass());
logger.info("do filter: FooFilter, conf_key1 value: {}", conf.get("conf_key1"));
logger.info("do filter: FooFilter, conf_key2 value: {}", conf.get("conf_key2"));
System.out.println("do filter: FooFilter, conf_key1 value: " + conf.get("conf_key1"));
System.out.println("do filter: FooFilter, conf_key2 value: " + conf.get("conf_key2"));
if (!Objects.isNull(request.getPath())) {
logger.info("do filter: path: {}", request.getPath());
System.out.println("do filter: path: " + request.getPath());
}

if (!Objects.isNull(request.getArgs())) {
for (Map.Entry<String, String> arg : request.getArgs().entrySet()) {
logger.info("do filter: arg key: {}", arg.getKey());
logger.info("do filter: arg value: {}", arg.getValue());
System.out.println("do filter: arg key: " + arg.getKey());
System.out.println("do filter: arg value: " + arg.getValue());
}
}

if (!Objects.isNull(request.getHeader())) {
for (Map.Entry<String, String> header : request.getHeader().entrySet()) {
logger.info("do filter: header key: {}", header.getKey());
logger.info("do filter: header value: {}", header.getValue());
System.out.println("do filter: header key: " + header.getKey());
System.out.println("do filter: header value: " + header.getValue());
}
}

if (!Objects.isNull(request.getMethod())) {
logger.info("do filter: method: {}", request.getMethod());
System.out.println("do filter: method: " + request.getMethod());
}

return chain.filter(request, response);
@@ -113,8 +115,8 @@ public String name() {

@Override
public Mono<Void> filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
logger.info("do filter: CatFilter, order: {}", chain.getIndex());
logger.info("do filter: CatFilter, config: {}", request.getConfig(this));
System.out.println("do filter: CatFilter, order: " + chain.getIndex());
System.out.println("do filter: CatFilter, config: " + request.getConfig(this));

response.setStatusCode(401);
return chain.filter(request, response);
@@ -147,10 +149,14 @@ public Mono<Void> filter(HttpRequest request, HttpResponse response, PluginFilte
a6HttpCallHandler = new A6HttpCallHandler(cache);
}

@AfterEach
void setDown() {
System.setOut(console);
}

@Test
@Disabled
@DisplayName("test cannot find conf token")
void testCannotFindConfToken(CapturedOutput capturedOutput) {
void testCannotFindConfToken() {
FlatBufferBuilder builder = new FlatBufferBuilder();

io.github.api7.A6.HTTPReqCall.Req.startReq(builder);
@@ -161,15 +167,13 @@ void testCannotFindConfToken(CapturedOutput capturedOutput) {
HttpRequest request = new HttpRequest(req);
HttpResponse response = new HttpResponse(1L);
a6HttpCallHandler.handle(request, response);
Assertions.assertTrue(capturedOutput.getOut().contains("cannot find conf token: 9999"));
io.github.api7.A6.Err.Resp err = io.github.api7.A6.Err.Resp.getRootAsResp(response.getErrResponse().encode());
Assertions.assertEquals(err.code(), Code.CONF_TOKEN_NOT_FOUND);
}

@Test
@Disabled
@DisplayName("test do filter and get config")
void testDoFilter1(CapturedOutput capturedOutput) {
void testDoFilter1() {
FlatBufferBuilder builder = new FlatBufferBuilder();

io.github.api7.A6.HTTPReqCall.Req.startReq(builder);
@@ -180,18 +184,18 @@ void testDoFilter1(CapturedOutput capturedOutput) {
HttpRequest request = new HttpRequest(req);
HttpResponse response = new HttpResponse(1L);
a6HttpCallHandler.handle(request, response);
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: FooFilter, order: 1"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: FooFilter, config: {\"conf_key1\":\"conf_value1\",\"conf_key2\":2}"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: FooFilter, conf_key1 value: conf_value1"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: FooFilter, conf_key2 value: 2.0"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: CatFilter, order: 2"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: CatFilter, config: Dog"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, order: 1"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, order: 1"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, config: {\"conf_key1\":\"conf_value1\",\"conf_key2\":2}"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, conf_key1 value: conf_value1"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, conf_key2 value: 2.0"));
Assertions.assertTrue(bytes.toString().contains("do filter: CatFilter, order: 2"));
Assertions.assertTrue(bytes.toString().contains("do filter: CatFilter, config: Dog"));
}

@Test
@Disabled
@DisplayName("test get request params")
void testDoFilter2(CapturedOutput capturedOutput) {
void testDoFilter2() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int argKey = builder.createString("argKey");
int argValue = builder.createString("argValue");
@@ -220,18 +224,18 @@ void testDoFilter2(CapturedOutput capturedOutput) {
HttpRequest request = new HttpRequest(req);
HttpResponse response = new HttpResponse(1L);
a6HttpCallHandler.handle(request, response);
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: FooFilter, order: 1"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: FooFilter, config: {\"conf_key1\":\"conf_value1\",\"conf_key2\":2}"));

Assertions.assertTrue(capturedOutput.getOut().contains("do filter: path: /path"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: arg key: argKey"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: arg value: argValue"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: header key: headerKey"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: header value: headerValue"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: method: GET"));

Assertions.assertTrue(capturedOutput.getOut().contains("do filter: CatFilter, order: 2"));
Assertions.assertTrue(capturedOutput.getOut().contains("do filter: CatFilter, config: Dog"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, order: 1"));
Assertions.assertTrue(bytes.toString().contains("do filter: FooFilter, config: {\"conf_key1\":\"conf_value1\",\"conf_key2\":2}"));

Assertions.assertTrue(bytes.toString().contains("do filter: path: /path"));
Assertions.assertTrue(bytes.toString().contains("do filter: arg key: argKey"));
Assertions.assertTrue(bytes.toString().contains("do filter: arg value: argValue"));
Assertions.assertTrue(bytes.toString().contains("do filter: header key: headerKey"));
Assertions.assertTrue(bytes.toString().contains("do filter: header value: headerValue"));
Assertions.assertTrue(bytes.toString().contains("do filter: method: GET"));

Assertions.assertTrue(bytes.toString().contains("do filter: CatFilter, order: 2"));
Assertions.assertTrue(bytes.toString().contains("do filter: CatFilter, config: Dog"));
}

@Test
@@ -42,8 +42,24 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-reactor-netty</artifactId>
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.0//EN"
"https://checkstyle.org/dtds/suppressions_1_0.dtd">

<suppressions>
<suppress checks="RegexpSingleline"
files="A6HttpCallHandlerTest.java"
lines="75-120"/>
</suppressions>

0 comments on commit d6bc7fa

Please sign in to comment.