This is an automated email from the ASF dual-hosted git repository.

tzssangglass pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/apisix-java-plugin-runner.git


The following commit(s) were added to refs/heads/main by this push:
     new feac053  fix(sample): StopRequestDemoFilter (#74)
feac053 is described below

commit feac0530a29a51a70acf7120e4059e8bf4dc9f4b
Author: Hugo-X <[email protected]>
AuthorDate: Tue Oct 19 11:34:23 2021 +0800

    fix(sample): StopRequestDemoFilter (#74)
---
 sample/pom.xml                                     | 21 +++++-
 .../runner/filter/StopRequestDemoFilter.java       |  2 +-
 .../runner/filter/StopRequestDemoFilterTest.java   | 87 ++++++++++++++++++++++
 3 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/sample/pom.xml b/sample/pom.xml
index 313fd60..466d756 100644
--- a/sample/pom.xml
+++ b/sample/pom.xml
@@ -49,5 +49,24 @@
             <groupId>io.projectreactor</groupId>
             <artifactId>reactor-core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.junit.vintage</groupId>
+                    <artifactId>junit-vintage-engine</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>ch.qos.logback</groupId>
+                    <artifactId>logback-classic</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.apache.logging.log4j</groupId>
+                    <artifactId>log4j-to-slf4j</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
     </dependencies>
-</project>
\ No newline at end of file
+</project>
diff --git 
a/sample/src/main/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilter.java
 
b/sample/src/main/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilter.java
index 97ae800..706e573 100644
--- 
a/sample/src/main/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilter.java
+++ 
b/sample/src/main/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilter.java
@@ -47,7 +47,7 @@ public class StopRequestDemoFilter implements PluginFilter {
         /*
          * You can use the parameters in the configuration.
          */
-        response.setStatusCode((Integer) conf.get("stop_response_code"));
+        
response.setStatusCode(Double.valueOf(conf.get("stop_response_code").toString()).intValue());
 
         response.setHeader((String) conf.get("stop_response_header_name"), 
(String) conf.get("stop_response_header_value"));
         /* note: The body is currently a string type.
                  If you need the json type, you need to escape the json 
content here.
diff --git 
a/sample/src/test/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilterTest.java
 
b/sample/src/test/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilterTest.java
new file mode 100644
index 0000000..88dfbb6
--- /dev/null
+++ 
b/sample/src/test/java/org/apache/apisix/plugin/runner/filter/StopRequestDemoFilterTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.
+ */
+
+package org.apache.apisix.plugin.runner.filter;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.google.gson.Gson;
+
+import org.apache.apisix.plugin.runner.HttpRequest;
+import org.apache.apisix.plugin.runner.HttpResponse;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+public class StopRequestDemoFilterTest {
+    
+    @Test
+    @DisplayName("test stop response code of config string")
+    void testConfigStringResponseCodeConverter() {
+
+        String configStr;
+
+        Gson gson = new Gson();
+        Map<String, Object> conf = new HashMap<String, Object>();
+
+        configStr = "{\"stop_response_code\": 200, 
\"stop_response_header_name\": \"header_java_runner\", 
\"stop_response_header_value\": \"via-java-runner\",  \"stop_response_body\": 
\"hellox\"}";
+        conf = gson.fromJson(configStr, conf.getClass());
+        Assertions.assertTrue(conf.get("stop_response_code") instanceof 
Double);
+        
Assertions.assertTrue(Double.valueOf(conf.get("stop_response_code").toString()).intValue()
 == 200);
+
+        configStr = "{\"stop_response_code\": \"200\", 
\"stop_response_header_name\": \"header_java_runner\", 
\"stop_response_header_value\": \"via-java-runner\",  \"stop_response_body\": 
\"hellox\"}";
+        conf = gson.fromJson(configStr, conf.getClass());
+        Assertions.assertTrue(conf.get("stop_response_code") instanceof 
String);
+        
Assertions.assertTrue(Double.valueOf(conf.get("stop_response_code").toString()).intValue()
 == 200);
+
+    }
+
+    @Test
+    @DisplayName("test filter")
+    void testFilter() {
+
+        StopRequestDemoFilter filter = new StopRequestDemoFilter();
+
+        HttpRequest request = mock(HttpRequest.class);
+        HttpResponse response = new HttpResponse(1);
+        PluginFilterChain chain = mock(PluginFilterChain.class);
+
+        String configStr = "{\"stop_response_code\": 200, 
\"stop_response_header_name\": \"header_java_runner\", 
\"stop_response_header_value\": \"via-java-runner\",  \"stop_response_body\": 
\"hellox\"}";
+        when(request.getConfig(filter)).thenReturn(configStr);
+        Assertions.assertNull(filter.filter(request, response, chain));
+
+        configStr = "{\"stop_response_code\": 200.0, 
\"stop_response_header_name\": \"header_java_runner\", 
\"stop_response_header_value\": \"via-java-runner\",  \"stop_response_body\": 
\"hellox\"}";
+        when(request.getConfig(filter)).thenReturn(configStr);
+        Assertions.assertNull(filter.filter(request, response, chain));
+
+        configStr = "{\"stop_response_code\": \"200\", 
\"stop_response_header_name\": \"header_java_runner\", 
\"stop_response_header_value\": \"via-java-runner\",  \"stop_response_body\": 
\"hellox\"}";
+        when(request.getConfig(filter)).thenReturn(configStr);
+        Assertions.assertNull(filter.filter(request, response, chain));
+    }
+
+    @Test
+    @DisplayName("test name")
+    void testName() {      
+        StopRequestDemoFilter filter = new StopRequestDemoFilter();
+
+        Assertions.assertEquals("StopRequestDemoFilter", filter.name());
+    }
+}

Reply via email to