You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shenyu.apache.org by GitBox <gi...@apache.org> on 2021/06/18 21:18:47 UTC

[GitHub] [incubator-shenyu] midnight2104 opened a new pull request #1643: [ISSUE #1642] add test case for shenyu-protocol-grpc module.

midnight2104 opened a new pull request #1643:
URL: https://github.com/apache/incubator-shenyu/pull/1643


   // Describe your PR here; eg. Fixes #issueNo
   
   <!--
   Thank you for proposing a pull request. This template will guide you through the essential steps necessary for a pull request.
   -->
   Make sure that:
   
   - [ ] You have read the [contribution guidelines](https://dromara.org/projects/soul/contributor/).
   - [ ] You submit test cases (unit or integration tests) that back your changes.
   - [ ] Your local test passed `mvn clean install -Dmaven.javadoc.skip=true`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-shenyu] midnight2104 commented on a change in pull request #1643: [ISSUE #1642] add test case for shenyu-protocol-grpc module.

Posted by GitBox <gi...@apache.org>.
midnight2104 commented on a change in pull request #1643:
URL: https://github.com/apache/incubator-shenyu/pull/1643#discussion_r654351150



##########
File path: shenyu-protocol/shenyu-protocol-grpc/src/test/java/org/apache/shenyu/protocol/grpc/message/JsonMessageTest.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.shenyu.protocol.grpc.message;
+
+import com.google.protobuf.Descriptors;
+import com.google.protobuf.DynamicMessage;
+import io.grpc.MethodDescriptor;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.protocol.grpc.constant.GrpcConstants;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.List;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+
+/**
+ * The Test Case For {@link JsonMessage}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class JsonMessageTest {
+
+    @Test
+    public void testBuildJsonMessage() {
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage();
+
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+    }
+
+    @Test
+    public void testBuildJsonMessageWithParam() {
+        String jsonParam = "{\"text\":\"hello world\"}";
+
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+
+        Descriptors.FieldDescriptor fieldDescriptor = jsonMessage.getDescriptorForType().findFieldByName(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
+        assertTrue(jsonMessage.hasField(fieldDescriptor));
+
+        String field = (String) jsonMessage.getField(fieldDescriptor);
+        assertEquals(jsonParam, field);
+    }
+
+    @Test
+    public void testBuildJsonMessageList() {
+        String jsonParam = "{\"data\":[{\"text\":\"hello\"}, {\"text\":\"world\"}]}\n";
+
+        List<DynamicMessage> jsonMessageList = JsonMessage.buildJsonMessageList(GsonUtils.getInstance().toObjectMap(jsonParam));
+        assertEquals(2, jsonMessageList.size());
+
+        DynamicMessage jsonMessage = jsonMessageList.get(0);
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+
+        Descriptors.FieldDescriptor fieldDescriptor = jsonMessage.getDescriptorForType().findFieldByName(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
+        assertTrue(jsonMessage.hasField(fieldDescriptor));
+
+        String field = (String) jsonMessage.getField(fieldDescriptor);
+        assertEquals("{\"text\":\"hello\"}", field);
+
+        jsonMessage = jsonMessageList.get(1);
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+
+        fieldDescriptor = jsonMessage.getDescriptorForType().findFieldByName(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
+        assertTrue(jsonMessage.hasField(fieldDescriptor));
+
+        field = (String) jsonMessage.getField(fieldDescriptor);
+        assertEquals("{\"text\":\"world\"}", field);
+    }
+
+    @Test
+    public void testGetDataFromDynamicMessage() {
+        String jsonParam = "{\"text\":\"hello world\"}";
+
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        String data = JsonMessage.getDataFromDynamicMessage(jsonMessage);
+        assertEquals(jsonParam, data);
+    }
+
+    @Test
+    public void testCreateJsonMarshallerMethodDescriptor() {
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage();
+        MethodDescriptor<DynamicMessage, DynamicMessage> echo = JsonMessage.createJsonMarshallerMethodDescriptor("echo.service",
+                "echo",
+                MethodDescriptor.MethodType.UNARY,
+                jsonMessage,
+                jsonMessage);
+
+        assertEquals("echo.service" + GrpcConstants.GRPC_JSON_SERVICE + "/echo", echo.getFullMethodName());
+        assertEquals(MethodDescriptor.MethodType.UNARY, echo.getType());
+        assertFalse(echo.isIdempotent());
+        assertFalse(echo.isSafe());
+    }
+
+    @Test
+    public void testParseOfDynamicMessageMarshaller() {
+        String jsonParam = "{\"text\":\"hello world\"}";
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        MethodDescriptor<DynamicMessage, DynamicMessage> echo = JsonMessage.createJsonMarshallerMethodDescriptor("echo.service",
+                "echo",
+                MethodDescriptor.MethodType.BIDI_STREAMING,
+                jsonMessage,
+                jsonMessage);
+
+        MethodDescriptor.Marshaller<DynamicMessage> requestMarshaller = echo.getRequestMarshaller();
+
+        InputStream inputStream = new ByteArrayInputStream("".getBytes());
+        requestMarshaller.parse(inputStream);
+    }
+
+    @Test
+    public void testStreamOfDynamicMessageMarshaller() {
+        DynamicMessage dynamicMessage = mock(DynamicMessage.class, RETURNS_DEEP_STUBS);
+        InputStream inputStream = mock(InputStream.class);
+        when(dynamicMessage.toByteString().newInput()).thenReturn(inputStream);
+
+        String jsonParam = "{\"text\":\"hello world\"}";
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        MethodDescriptor<DynamicMessage, DynamicMessage> echo = JsonMessage.createJsonMarshallerMethodDescriptor("echo.service",
+                "echo",
+                MethodDescriptor.MethodType.CLIENT_STREAMING,
+                jsonMessage,
+                jsonMessage);
+
+        MethodDescriptor.Marshaller<DynamicMessage> requestMarshaller = echo.getRequestMarshaller();
+
+        InputStream actualInputStream = requestMarshaller.stream(dynamicMessage);
+        Assert.assertEquals(inputStream, actualInputStream);
+    }
+

Review comment:
       ok~




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-shenyu] codecov-commenter edited a comment on pull request #1643: [ISSUE #1642] add test case for shenyu-protocol-grpc module.

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1643:
URL: https://github.com/apache/incubator-shenyu/pull/1643#issuecomment-863964036






-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-shenyu] codecov-commenter commented on pull request #1643: [ISSUE #1642] add test case for shenyu-protocol-grpc module.

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on pull request #1643:
URL: https://github.com/apache/incubator-shenyu/pull/1643#issuecomment-863964036


   # [Codecov](https://codecov.io/gh/apache/incubator-shenyu/pull/1643?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > :exclamation: No coverage uploaded for pull request base (`master@9bfe200`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#section-missing-base-commit).
   > The diff coverage is `n/a`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/incubator-shenyu/pull/1643/graphs/tree.svg?width=650&height=150&src=pr&token=k89XYIkOHK&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/incubator-shenyu/pull/1643?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff            @@
   ##             master    #1643   +/-   ##
   =========================================
     Coverage          ?   62.76%           
     Complexity        ?     2243           
   =========================================
     Files             ?      458           
     Lines             ?     9632           
     Branches          ?      975           
   =========================================
     Hits              ?     6046           
     Misses            ?     3097           
     Partials          ?      489           
   ```
   
   
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/incubator-shenyu/pull/1643?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/incubator-shenyu/pull/1643?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [9bfe200...3685b3e](https://codecov.io/gh/apache/incubator-shenyu/pull/1643?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-shenyu] SaberSola commented on a change in pull request #1643: [ISSUE #1642] add test case for shenyu-protocol-grpc module.

Posted by GitBox <gi...@apache.org>.
SaberSola commented on a change in pull request #1643:
URL: https://github.com/apache/incubator-shenyu/pull/1643#discussion_r654347520



##########
File path: shenyu-protocol/shenyu-protocol-grpc/src/test/java/org/apache/shenyu/protocol/grpc/message/JsonMessageTest.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.shenyu.protocol.grpc.message;
+
+import com.google.protobuf.Descriptors;
+import com.google.protobuf.DynamicMessage;
+import io.grpc.MethodDescriptor;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.protocol.grpc.constant.GrpcConstants;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.List;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+
+/**
+ * The Test Case For {@link JsonMessage}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class JsonMessageTest {
+
+    @Test
+    public void testBuildJsonMessage() {
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage();
+
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+    }
+
+    @Test
+    public void testBuildJsonMessageWithParam() {
+        String jsonParam = "{\"text\":\"hello world\"}";
+
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+
+        Descriptors.FieldDescriptor fieldDescriptor = jsonMessage.getDescriptorForType().findFieldByName(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
+        assertTrue(jsonMessage.hasField(fieldDescriptor));
+
+        String field = (String) jsonMessage.getField(fieldDescriptor);
+        assertEquals(jsonParam, field);
+    }
+
+    @Test
+    public void testBuildJsonMessageList() {
+        String jsonParam = "{\"data\":[{\"text\":\"hello\"}, {\"text\":\"world\"}]}\n";
+
+        List<DynamicMessage> jsonMessageList = JsonMessage.buildJsonMessageList(GsonUtils.getInstance().toObjectMap(jsonParam));
+        assertEquals(2, jsonMessageList.size());
+
+        DynamicMessage jsonMessage = jsonMessageList.get(0);
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+
+        Descriptors.FieldDescriptor fieldDescriptor = jsonMessage.getDescriptorForType().findFieldByName(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
+        assertTrue(jsonMessage.hasField(fieldDescriptor));
+
+        String field = (String) jsonMessage.getField(fieldDescriptor);
+        assertEquals("{\"text\":\"hello\"}", field);
+
+        jsonMessage = jsonMessageList.get(1);
+        assertEquals(GrpcConstants.JSON_DESCRIPTOR_PROTO_NAME, jsonMessage.getDescriptorForType().getFullName());
+
+        fieldDescriptor = jsonMessage.getDescriptorForType().findFieldByName(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
+        assertTrue(jsonMessage.hasField(fieldDescriptor));
+
+        field = (String) jsonMessage.getField(fieldDescriptor);
+        assertEquals("{\"text\":\"world\"}", field);
+    }
+
+    @Test
+    public void testGetDataFromDynamicMessage() {
+        String jsonParam = "{\"text\":\"hello world\"}";
+
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        String data = JsonMessage.getDataFromDynamicMessage(jsonMessage);
+        assertEquals(jsonParam, data);
+    }
+
+    @Test
+    public void testCreateJsonMarshallerMethodDescriptor() {
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage();
+        MethodDescriptor<DynamicMessage, DynamicMessage> echo = JsonMessage.createJsonMarshallerMethodDescriptor("echo.service",
+                "echo",
+                MethodDescriptor.MethodType.UNARY,
+                jsonMessage,
+                jsonMessage);
+
+        assertEquals("echo.service" + GrpcConstants.GRPC_JSON_SERVICE + "/echo", echo.getFullMethodName());
+        assertEquals(MethodDescriptor.MethodType.UNARY, echo.getType());
+        assertFalse(echo.isIdempotent());
+        assertFalse(echo.isSafe());
+    }
+
+    @Test
+    public void testParseOfDynamicMessageMarshaller() {
+        String jsonParam = "{\"text\":\"hello world\"}";
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        MethodDescriptor<DynamicMessage, DynamicMessage> echo = JsonMessage.createJsonMarshallerMethodDescriptor("echo.service",
+                "echo",
+                MethodDescriptor.MethodType.BIDI_STREAMING,
+                jsonMessage,
+                jsonMessage);
+
+        MethodDescriptor.Marshaller<DynamicMessage> requestMarshaller = echo.getRequestMarshaller();
+
+        InputStream inputStream = new ByteArrayInputStream("".getBytes());
+        requestMarshaller.parse(inputStream);
+    }
+
+    @Test
+    public void testStreamOfDynamicMessageMarshaller() {
+        DynamicMessage dynamicMessage = mock(DynamicMessage.class, RETURNS_DEEP_STUBS);
+        InputStream inputStream = mock(InputStream.class);
+        when(dynamicMessage.toByteString().newInput()).thenReturn(inputStream);
+
+        String jsonParam = "{\"text\":\"hello world\"}";
+        DynamicMessage jsonMessage = JsonMessage.buildJsonMessage(jsonParam);
+        MethodDescriptor<DynamicMessage, DynamicMessage> echo = JsonMessage.createJsonMarshallerMethodDescriptor("echo.service",
+                "echo",
+                MethodDescriptor.MethodType.CLIENT_STREAMING,
+                jsonMessage,
+                jsonMessage);
+
+        MethodDescriptor.Marshaller<DynamicMessage> requestMarshaller = echo.getRequestMarshaller();
+
+        InputStream actualInputStream = requestMarshaller.stream(dynamicMessage);
+        Assert.assertEquals(inputStream, actualInputStream);
+    }
+

Review comment:
       remove this blank line..




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-shenyu] dengliming merged pull request #1643: [ISSUE #1642] add test case for shenyu-protocol-grpc module.

Posted by GitBox <gi...@apache.org>.
dengliming merged pull request #1643:
URL: https://github.com/apache/incubator-shenyu/pull/1643


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org