You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by pe...@apache.org on 2022/03/09 09:16:40 UTC

[incubator-linkis] 02/06: add unit test for datasource metadata server

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

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

commit e9c770af0e5cce2b420d6d42d0aa2ffa91ed6dce
Author: xiaojie19852006 <xi...@163.com>
AuthorDate: Wed Mar 9 10:36:54 2022 +0800

    add unit test for datasource metadata server
---
 .../server/WebApplicationServer.java               |  11 ++
 .../server/restful/MetadataCoreRestfulTest.java    | 184 +++++++++++++++++++++
 .../metadatamanager/server/restful/MvcUtils.java   |  89 ++++++++++
 .../src/test/resources/application.properties      |   4 +
 4 files changed, 288 insertions(+)

diff --git a/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/WebApplicationServer.java b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/WebApplicationServer.java
new file mode 100644
index 0000000..2c39eba
--- /dev/null
+++ b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/WebApplicationServer.java
@@ -0,0 +1,11 @@
+package org.apache.linkis.metadatamanager.server;
+
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.web.servlet.ServletComponentScan;
+import org.springframework.context.annotation.ComponentScan;
+
+@EnableAutoConfiguration
+@ServletComponentScan
+@ComponentScan
+public class WebApplicationServer {
+}
diff --git a/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/restful/MetadataCoreRestfulTest.java b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/restful/MetadataCoreRestfulTest.java
new file mode 100644
index 0000000..48a1305
--- /dev/null
+++ b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/restful/MetadataCoreRestfulTest.java
@@ -0,0 +1,184 @@
+package org.apache.linkis.metadatamanager.server.restful;
+
+import org.apache.linkis.common.exception.ErrorException;
+import org.apache.linkis.metadatamanager.common.domain.MetaPartitionInfo;
+import org.apache.linkis.metadatamanager.server.WebApplicationServer;
+import org.apache.linkis.metadatamanager.server.service.MetadataAppService;
+import org.apache.linkis.server.Message;
+import org.apache.linkis.server.MessageStatus;
+import org.apache.linkis.server.security.SecurityFilter;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.mockito.stubbing.OngoingStubbing;
+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.boot.test.mock.mockito.MockBean;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import javax.servlet.http.HttpServletRequest;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import static org.mockito.ArgumentMatchers.isA;
+
+
+@ExtendWith({SpringExtension.class})
+@AutoConfigureMockMvc
+@SpringBootTest(classes = {WebApplicationServer.class})
+class MetadataCoreRestfulTest {
+
+    @Autowired
+    protected MockMvc mockMvc;
+
+    @MockBean
+    private MetadataAppService metadataAppService;
+
+    private static MockedStatic<SecurityFilter> securityFilter;
+
+    @BeforeAll
+    private static void init(){
+        securityFilter = Mockito.mockStatic(SecurityFilter.class);
+    }
+
+    @AfterAll
+    private static void close(){
+        securityFilter.close();
+    }
+
+
+    @Test
+    void testGetDatabases() {
+        try {
+            String dataSourceId = "1l";
+            String url = String.format("/metadatamanager/dbs/%s",dataSourceId);
+            MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
+            params.add("system","");
+            MvcUtils mvcUtils = new MvcUtils(mockMvc);
+            Message res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("'system' is missing"));
+            params.add("system","hive");
+            Mockito.when(metadataAppService.getDatabasesByDsId(dataSourceId,",hive",null)).thenReturn(new ArrayList<>());
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.SUCCESS() == res.getStatus());
+
+            Mockito.doThrow(new ErrorException(1,"")).when(metadataAppService).getDatabasesByDsId(dataSourceId,",hive",null);
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("Fail to get database list"));
+        }catch (Exception e){
+            //ignore
+        }
+
+    }
+
+    @Test
+    void testGetTables() throws Exception {
+        String dataSourceId = "1l";
+        String database = "hivedb";
+        String url = String.format("/metadatamanager/tables/%s/db/%s",dataSourceId,database);
+        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
+        params.add("system","");
+        MvcUtils mvcUtils = new MvcUtils(mockMvc);
+        Message res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+        Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("'system' is missing"));
+
+        params.add("system","hive");
+        Mockito.when(metadataAppService.getTablesByDsId(dataSourceId,database,",hive",null)).thenReturn(new ArrayList<>());
+        res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+        Assertions.assertTrue(MessageStatus.SUCCESS() == res.getStatus());
+
+        Mockito.doThrow(new ErrorException(1,"")).when(metadataAppService).getTablesByDsId(dataSourceId,database,",hive",null);
+        res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+        Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("Fail to get table list"));
+    }
+
+    @Test
+    void testGetTableProps() {
+        try {
+            String dataSourceId = "1l";
+            String database = "hivedb";
+            String table = "testtab";
+            String url = String.format("/metadatamanager/props/%s/db/%s/table/%s",dataSourceId,database,table);
+            MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
+            params.add("system","");
+            MvcUtils mvcUtils = new MvcUtils(mockMvc);
+            Message res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("'system' is missing"));
+
+            params.add("system","hive");
+            Mockito.when(metadataAppService.getTablePropsByDsId(dataSourceId,database,table,",hive",null)).thenReturn(new HashMap<>());
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.SUCCESS() == res.getStatus());
+
+            Mockito.doThrow(new ErrorException(1,"")).when(metadataAppService).getTablePropsByDsId(dataSourceId,database,table,",hive",null);
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("Fail to get table properties"));
+        }catch (Exception e) {
+            //ignore
+        }
+
+    }
+
+    @Test
+    void testGetPartitions() {
+        try {
+            String dataSourceId = "1l";
+            String database = "hivedb";
+            String table = "testtab";
+            String url = String.format("/metadatamanager/partitions/%s/db/%s/table/%s",dataSourceId,database,table);
+            MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
+            params.add("system","");
+            MvcUtils mvcUtils = new MvcUtils(mockMvc);
+            Message res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("'system' is missing"));
+
+            params.add("system","hive");
+            Mockito.when(metadataAppService.getPartitionsByDsId(dataSourceId,database,table,",hive",null)).thenReturn(new MetaPartitionInfo());
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.SUCCESS() == res.getStatus());
+
+            Mockito.doThrow(new ErrorException(1,"")).when(metadataAppService).getPartitionsByDsId(dataSourceId,database,table,",hive",null);
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("Fail to get partitions"));
+        }catch (Exception e){
+            //ignore
+        }
+
+    }
+
+    @Test
+    void testGetColumns() {
+        try {
+            String dataSourceId = "1l";
+            String database = "hivedb";
+            String table = "testtab";
+            String url = String.format("/metadatamanager/columns/%s/db/%s/table/%s",dataSourceId,database,table);
+            MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
+            params.add("system","");
+            MvcUtils mvcUtils = new MvcUtils(mockMvc);
+            Message res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("'system' is missing"));
+
+            params.add("system","hive");
+            Mockito.when(metadataAppService.getColumns(dataSourceId,database,table,",hive",null)).thenReturn(new ArrayList<>());
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.SUCCESS() == res.getStatus());
+
+            Mockito.doThrow(new ErrorException(1,"")).when(metadataAppService).getColumns(dataSourceId,database,table,",hive",null);
+            res = mvcUtils.getMessage(mvcUtils.buildMvcResultGet(url,params));
+            Assertions.assertTrue(MessageStatus.ERROR() == res.getStatus() && res.getMessage().contains("Fail to get column list"));
+        }catch (Exception e){
+            //ignore
+        }
+
+    }
+}
\ No newline at end of file
diff --git a/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/restful/MvcUtils.java b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/restful/MvcUtils.java
new file mode 100644
index 0000000..36463a6
--- /dev/null
+++ b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/java/org/apache/linkis/metadatamanager/server/restful/MvcUtils.java
@@ -0,0 +1,89 @@
+package org.apache.linkis.metadatamanager.server.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-datasource/linkis-metadata-manager/server/src/test/resources/application.properties b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/resources/application.properties
new file mode 100644
index 0000000..2215c66
--- /dev/null
+++ b/linkis-public-enhancements/linkis-datasource/linkis-metadata-manager/server/src/test/resources/application.properties
@@ -0,0 +1,4 @@
+#disable eureka discovery client
+spring.cloud.service-registry.auto-registration.enabled=false
+eureka.client.enabled=false
+eureka.client.serviceUrl.registerWithEureka=false
\ 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