kezhenxu94 commented on code in PR #9969:
URL: https://github.com/apache/skywalking/pull/9969#discussion_r1024650841


##########
oap-server/server-storage-plugin/storage-shardingsphere-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/shardingsphere/ITShardingTest.java:
##########
@@ -0,0 +1,732 @@
+/*
+ * 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.skywalking.oap.server.storage.plugin.jdbc.shardingsphere;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.server.core.CoreModule;
+import org.apache.skywalking.oap.server.core.analysis.DownSampling;
+import org.apache.skywalking.oap.server.core.analysis.IDManager;
+import org.apache.skywalking.oap.server.core.analysis.Stream;
+import 
org.apache.skywalking.oap.server.core.analysis.manual.endpoint.EndpointTraffic;
+import 
org.apache.skywalking.oap.server.core.analysis.manual.relation.service.ServiceRelationClientSideMetrics;
+import 
org.apache.skywalking.oap.server.core.analysis.manual.relation.service.ServiceRelationServerSideMetrics;
+import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.Tag;
+import 
org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagAutocompleteData;
+import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagType;
+import 
org.apache.skywalking.oap.server.core.analysis.manual.segment.SegmentRecord;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import org.apache.skywalking.oap.server.core.analysis.record.Record;
+import org.apache.skywalking.oap.server.core.config.ConfigService;
+import org.apache.skywalking.oap.server.core.query.enumeration.Order;
+import org.apache.skywalking.oap.server.core.query.enumeration.Scope;
+import org.apache.skywalking.oap.server.core.query.enumeration.Step;
+import org.apache.skywalking.oap.server.core.query.input.Duration;
+import org.apache.skywalking.oap.server.core.query.input.Entity;
+import org.apache.skywalking.oap.server.core.query.input.MetricsCondition;
+import org.apache.skywalking.oap.server.core.query.input.TopNCondition;
+import org.apache.skywalking.oap.server.core.query.type.Call;
+import org.apache.skywalking.oap.server.core.query.type.Endpoint;
+import org.apache.skywalking.oap.server.core.query.type.MetricsValues;
+import org.apache.skywalking.oap.server.core.query.type.QueryOrder;
+import org.apache.skywalking.oap.server.core.query.type.SelectedRecord;
+import org.apache.skywalking.oap.server.core.query.type.TraceBrief;
+import org.apache.skywalking.oap.server.core.query.type.TraceState;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.storage.StorageData;
+import org.apache.skywalking.oap.server.core.storage.StorageException;
+import org.apache.skywalking.oap.server.core.storage.annotation.SQLDatabase;
+import org.apache.skywalking.oap.server.core.storage.annotation.Storage;
+import org.apache.skywalking.oap.server.core.storage.model.Model;
+import org.apache.skywalking.oap.server.core.storage.model.StorageModels;
+import 
org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient;
+import org.apache.skywalking.oap.server.library.module.ModuleManager;
+import org.apache.skywalking.oap.server.library.module.ModuleProviderHolder;
+import org.apache.skywalking.oap.server.library.module.ModuleServiceHolder;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLExecutor;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao.JDBCMetadataQueryDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao.JDBCMetricsDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao.JDBCRecordDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao.JDBCTagAutoCompleteQueryDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.mysql.MySQLTableInstaller;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.shardingsphere.dao.ShardingAggregationQueryDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.shardingsphere.dao.ShardingHistoryDeleteDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.shardingsphere.dao.ShardingMetricsQueryDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.shardingsphere.dao.ShardingTopologyQueryDAO;
+import 
org.apache.skywalking.oap.server.storage.plugin.jdbc.shardingsphere.dao.ShardingTraceQueryDAO;
+import org.joda.time.DateTime;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.modules.junit4.PowerMockRunnerDelegate;
+import org.powermock.reflect.Whitebox;
+import org.testcontainers.containers.DockerComposeContainer;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@Slf4j
+@RunWith(PowerMockRunner.class)
+@PowerMockRunnerDelegate(Parameterized.class)
+@PowerMockIgnore({
+    "javax.net.*",
+    "javax.management.*",
+    "com.sun.org.apache.xerces.*",
+    "javax.xml.*", "org.xml.*",
+    "javax.management.*",
+    "org.w3c.*"
+})
+@PrepareForTest({DefaultScopeDefine.class})
+public class ITShardingTest {
+    @BeforeClass
+    public static void setup() {
+        PowerMockito.mockStatic(DefaultScopeDefine.class);
+        PowerMockito.when(DefaultScopeDefine.nameOf(1)).thenReturn("any");
+    }
+
+    @Parameterized.Parameter
+    public String version;
+
+    @Parameterized.Parameter(1)
+    public DataSourceType dsType;
+
+    @Parameterized.Parameters(name = "version: {0}")
+    public static Collection<Object[]> versions() {
+        return Arrays.asList(new Object[][] {
+            {
+                "5.1.2",
+                DataSourceType.MYSQL
+            }
+        });
+    }
+
+    public static DockerComposeContainer<?> ENVIRONMENT;
+    private JDBCHikariCPClient ssClient;
+    private JDBCHikariCPClient dsClient0;
+    private JDBCHikariCPClient dsClient1;
+    private final Set<String> dataSources = new 
HashSet<>(Arrays.asList("ds_0", "ds_1"));
+    private final int ttlTestCreate = 3;
+    private final int ttlTestDrop = 2;
+    private final String searchableTag = "http.method";
+    private final long timeBucketSec = 
Long.parseLong(DateTime.now().toString("yyyyMMddHHmmss"));
+    private final long timeBucketMin = 
Long.parseLong(DateTime.now().toString("yyyyMMddHHmm"));
+    private final long timeBucketDay = 
Long.parseLong(DateTime.now().toString("yyyyMMdd"));
+    private Duration duration;
+    private Entity entityA;
+    private Entity entityB;
+    private String serviceIdA;
+    private String serviceIdB;
+
+    private ModuleManager moduleManager;
+    private MySQLTableInstaller mySQLTableInstaller;
+    private ShardingSphereTableInstaller installer;
+    private DurationWithinTTL durationWithinTTL = DurationWithinTTL.INSTANCE;
+    private final String countQuery = "SELECT COUNT(*) AS rc FROM ";
+
+    @Before
+    public void init() {
+        if (dsType.equals(DataSourceType.MYSQL)) {
+            startEnv("docker-compose-mysql.yml", 3306);
+            initConnection("mysql", "/swtest?rewriteBatchedStatements=true", 
3306, "root", "root@1234");
+        }
+        initTestData();
+    }
+
+    private void startEnv(String dockerComposeName, int dsServicePort) {
+        ENVIRONMENT = new DockerComposeContainer<>(new 
File(ITShardingTest.class
+                                                                
.getClassLoader()
+                                                                
.getResource(dockerComposeName).getPath()))
+            .withExposedService("sharding-proxy", 3307,
+                                
Wait.defaultWaitStrategy().withStartupTimeout(java.time.Duration.ofMinutes(10))
+            )
+            .withExposedService("data-source-0", dsServicePort,
+                                
Wait.defaultWaitStrategy().withStartupTimeout(java.time.Duration.ofMinutes(10))
+            )
+            .withExposedService("data-source-1", dsServicePort,
+                                
Wait.defaultWaitStrategy().withStartupTimeout(java.time.Duration.ofMinutes(10))
+            )
+            .withEnv("SS_VERSION", version);
+        ENVIRONMENT.start();
+    }
+
+    private void initConnection(String driverType,
+                                String urlSuffix,
+                                int dsServicePort,
+                                String dsUserName,
+                                String dsPassword) {
+        String ssUrl = "jdbc:" + driverType + "://" +
+            ENVIRONMENT.getServiceHost("sharding-proxy", 3307) + ":" +
+            ENVIRONMENT.getServicePort("sharding-proxy", 3307) +
+            urlSuffix;
+        Properties properties = new Properties();
+        properties.setProperty("jdbcUrl", ssUrl);
+        properties.setProperty("dataSource.user", "root");
+        properties.setProperty("dataSource.password", "root");
+
+        String dsUrl0 = "jdbc:" + driverType + "://" +
+            ENVIRONMENT.getServiceHost("data-source-0", dsServicePort) + ":" +
+            ENVIRONMENT.getServicePort("data-source-0", dsServicePort) +
+            urlSuffix;
+        Properties propertiesDs0 = new Properties();
+        propertiesDs0.setProperty("jdbcUrl", dsUrl0);
+        propertiesDs0.setProperty("dataSource.user", dsUserName);
+        propertiesDs0.setProperty("dataSource.password", "root@1234");
+
+        String dsUrl1 = "jdbc:" + driverType + "://" +
+            ENVIRONMENT.getServiceHost("data-source-1", dsServicePort) + ":" +
+            ENVIRONMENT.getServicePort("data-source-1", dsServicePort) +
+            urlSuffix;
+        Properties propertiesDs1 = new Properties();
+        propertiesDs1.setProperty("jdbcUrl", dsUrl1);
+        propertiesDs1.setProperty("dataSource.user", dsUserName);
+        propertiesDs1.setProperty("dataSource.password", dsPassword);
+
+        ssClient = new JDBCHikariCPClient(properties);
+        dsClient0 = new JDBCHikariCPClient(propertiesDs0);
+        dsClient1 = new JDBCHikariCPClient(propertiesDs1);
+
+        ssClient.connect();
+        dsClient0.connect();
+        dsClient1.connect();
+    }
+
+    private void initTestData() {
+        moduleManager = mock(ModuleManager.class);
+        ConfigService configService = mock(ConfigService.class);
+        Whitebox.setInternalState(moduleManager, "isInPrepareStage", false);
+        when(configService.getMetricsDataTTL()).thenReturn(ttlTestCreate);
+        when(configService.getRecordDataTTL()).thenReturn(ttlTestCreate);
+        
when(configService.getSearchableTracesTags()).thenReturn(searchableTag);
+        
when(moduleManager.find(anyString())).thenReturn(mock(ModuleProviderHolder.class));
+        
when(moduleManager.find(CoreModule.NAME).provider()).thenReturn(mock(ModuleServiceHolder.class));
+        
when(moduleManager.find(CoreModule.NAME).provider().getService(ConfigService.class))
+            .thenReturn(configService);
+        mySQLTableInstaller = new MySQLTableInstaller(ssClient, moduleManager);
+        installer = new ShardingSphereTableInstaller(ssClient, moduleManager,
+                                                     dataSources,
+                                                     mySQLTableInstaller
+        );
+
+        durationWithinTTL.setConfigService(configService);
+
+        duration = new Duration();
+        duration.setStart(DateTime.now().minusMinutes(15).toString("yyyy-MM-dd 
HHmm"));
+        duration.setEnd(DateTime.now().plusMinutes(1).toString("yyyy-MM-dd 
HHmm"));
+        duration.setStep(Step.MINUTE);
+
+        entityA = new Entity();
+        entityA.setServiceName("Service_A");
+        entityA.setNormal(true);
+        entityA.setScope(Scope.Service);
+
+        entityB = new Entity();
+        entityB.setServiceName("Service_B");
+        entityB.setNormal(true);
+        entityB.setScope(Scope.Service);
+
+        serviceIdA = entityA.buildId();
+        serviceIdB = entityB.buildId();
+    }
+
+    @SneakyThrows
+    @After
+    public void after() {
+        ENVIRONMENT.stop();
+    }
+
+    @SneakyThrows
+    @Test
+    public void test() {
+        trafficTest();
+        metricsTest();
+        tagsTest();
+        recordsTest();
+        topologyTest();

Review Comment:
   You can just annotate each of these methods with `@Test` and remove this 
`test()` method.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to