You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by ca...@apache.org on 2022/08/12 10:23:11 UTC

[incubator-linkis] branch dev-1.2.1 updated: [ISSUES-2716]Add VariableRestfulApi unit test (#2717)

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

casion pushed a commit to branch dev-1.2.1
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git


The following commit(s) were added to refs/heads/dev-1.2.1 by this push:
     new 51d5544b1 [ISSUES-2716]Add VariableRestfulApi unit  test (#2717)
51d5544b1 is described below

commit 51d5544b1fbc2c6fb44a742af8809ffe51a750f9
Author: 成彬彬 <10...@users.noreply.github.com>
AuthorDate: Fri Aug 12 18:23:06 2022 +0800

    [ISSUES-2716]Add VariableRestfulApi unit  test (#2717)
    
    * Add VariableRestfulApi test
---
 .../apache/linkis/variable/restful/MvcUtils.java   | 109 +++++++++++++++++++++
 .../restful/api/VariableRestfulApiTest.java        |  90 ++++++++++++++---
 .../src/test/resources/application.properties      |   2 +-
 .../src/test/resources/linkis.properties           |  21 ++++
 4 files changed, 208 insertions(+), 14 deletions(-)

diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/MvcUtils.java b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/MvcUtils.java
new file mode 100644
index 000000000..078660943
--- /dev/null
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/MvcUtils.java
@@ -0,0 +1,109 @@
+/*
+ * 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.linkis.variable.restful;
+
+import org.apache.linkis.common.utils.JsonUtils;
+import org.apache.linkis.server.Message;
+
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.util.MultiValueMap;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+public class MvcUtils {
+    private MockMvc mockMvc;
+
+    public MvcUtils(MockMvc mockMvc) {
+        this.mockMvc = mockMvc;
+    }
+
+    public MvcResult buildMvcResultGet(String url) throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(get(url))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public MvcResult buildMvcResultGet(String url, MultiValueMap<String, String> params)
+            throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(get(url).params(params))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public MvcResult buildMvcResultPost(String url, String json) throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(post(url).contentType(MediaType.APPLICATION_JSON).content(json))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public MvcResult buildMvcResultPost(String url) throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(post(url))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public MvcResult buildMvcResultPut(String url, String json) throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(put(url).contentType(MediaType.APPLICATION_JSON).content(json))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public MvcResult buildMvcResultPut(String url) throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(put(url))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public MvcResult buildMvcResultDelete(String url) throws Exception {
+        MvcResult mvcResult =
+                mockMvc.perform(delete(url))
+                        .andExpect(status().isOk())
+                        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+                        .andReturn();
+        return mvcResult;
+    }
+
+    public Message getMessage(MvcResult mvcResult) throws Exception {
+        Message res =
+                JsonUtils.jackson()
+                        .readValue(mvcResult.getResponse().getContentAsString(), Message.class);
+        return res;
+    }
+}
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/api/VariableRestfulApiTest.java b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/api/VariableRestfulApiTest.java
index 52c7dd0fd..d8d9ab966 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/api/VariableRestfulApiTest.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/java/org/apache/linkis/variable/restful/api/VariableRestfulApiTest.java
@@ -17,33 +17,97 @@
 
 package org.apache.linkis.variable.restful.api;
 
+import org.apache.linkis.server.Message;
+import org.apache.linkis.server.MessageStatus;
+import org.apache.linkis.server.security.SecurityFilter;
+import org.apache.linkis.variable.Scan;
+import org.apache.linkis.variable.WebApplicationServer;
+import org.apache.linkis.variable.restful.MvcUtils;
+import org.apache.linkis.variable.service.VariableService;
+
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.util.MultiValueMap;
 
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /** VariableRestfulApi Tester */
+@ExtendWith({SpringExtension.class})
+@AutoConfigureMockMvc
+@SpringBootTest(classes = {WebApplicationServer.class, Scan.class})
 public class VariableRestfulApiTest {
+    private Logger logger = LoggerFactory.getLogger(VariableRestfulApiTest.class);
 
-    @Autowired private VariableRestfulApi variableRestfulApi;
+    @Autowired protected MockMvc mockMvc;
 
-    @BeforeEach
-    @DisplayName("Each unit test method is executed once before execution")
-    public void before() throws Exception {}
+    @Mock VariableService variableService;
 
-    @AfterEach
-    @DisplayName("Each unit test method is executed once after execution")
-    public void after() throws Exception {}
+    private static MockedStatic<SecurityFilter> securityFilter;
+
+    @BeforeAll
+    private static void init() {
+        securityFilter = Mockito.mockStatic(SecurityFilter.class);
+    }
+
+    @AfterAll
+    private static void close() {
+        securityFilter.close();
+    }
 
     @Test
     public void testListGlobalVariable() throws Exception {
-        // TODO: Test goes here...
+        // h2数据库执行生成的sql语句报错
+        /*String url = "/variable/listGlobalVariable";
+        securityFilter
+                .when(() -> SecurityFilter.getLoginUsername(isA(HttpServletRequest.class)))
+                .thenReturn("hadoop");
+        List<VarKeyValueVO> list=new ArrayList<>();
+        VarKeyValueVO varKeyValueVO=new VarKeyValueVO();
+        varKeyValueVO.setValue("testV");
+        varKeyValueVO.setKey("testK");
+        varKeyValueVO.setKeyID(1l);
+        varKeyValueVO.setValueID(2l);
+        list.add(varKeyValueVO);
+        Mockito.when(variableService.listGlobalVariable("hadoop")).thenReturn(list);
+        sendUrl(url, null, "get", null);*/
     }
 
     @Test
-    public void testSaveGlobalVariable() throws Exception {
-        // TODO: Test goes here...
+    public void testSaveGlobalVariable() throws Exception {}
+
+    public void sendUrl(
+            String url, MultiValueMap<String, String> paramsMap, String type, String msg)
+            throws Exception {
+        MvcUtils mvcUtils = new MvcUtils(mockMvc);
+        Message mvcResult = null;
+        if (type.equals("get")) {
+            if (paramsMap != null) {
+                mvcResult = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url, paramsMap));
+            } else {
+                mvcResult = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url));
+            }
+        }
+        if (type.equals("post")) {
+            if (msg != null) {
+                mvcResult = mvcUtils.getMessage(mvcUtils.buildMvcResultPost(url, msg));
+            } else {
+                mvcResult = mvcUtils.getMessage(mvcUtils.buildMvcResultPost(url));
+            }
+        }
+        assertEquals(MessageStatus.SUCCESS(), mvcResult.getStatus());
+        logger.info(String.valueOf(mvcResult));
     }
 }
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/application.properties b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/application.properties
index e2b6ed6c7..df58ee9f8 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/application.properties
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/application.properties
@@ -57,5 +57,5 @@ eureka.client.enabled=false
 eureka.client.serviceUrl.registerWithEureka=false
 
 mybatis-plus.mapper-locations=classpath:org/apache/linkis/variable/dao/impl/*.xml
-mybatis-plus.type-aliases-package=org.apache.linkis.configuration.entity
+mybatis-plus.type-aliases-package=org.apache.linkis.variable.entity
 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
\ No newline at end of file
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/linkis.properties b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/linkis.properties
new file mode 100644
index 000000000..1c575edc5
--- /dev/null
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-variable/src/test/resources/linkis.properties
@@ -0,0 +1,21 @@
+# 
+# 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.
+#
+
+#wds.linkis.test.mode=true
+wds.linkis.server.version=v1
+
+#test
+wds.linkis.test.mode=true
+wds.linkis.test.user=hadoop
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org