diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAOTest.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAOTest.java new file mode 100644 index 000000000000..381d30123887 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAOTest.java @@ -0,0 +1,134 @@ +/* + * 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.common.dao; + +import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; +import org.apache.skywalking.oap.server.core.query.enumeration.Order; +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.TopNCondition; +import org.apache.skywalking.oap.server.core.query.type.KeyValue; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class JDBCAggregationQueryDAOTest { + + private static final String TABLE = "metrics_table"; + + @Mock + private JDBCClient jdbcClient; + @Mock + private TableHelper tableHelper; + + private JDBCAggregationQueryDAO dao; + + @BeforeEach + void setUp() { + dao = new JDBCAggregationQueryDAO(jdbcClient, tableHelper); + } + + private TopNCondition buildCondition(String name, int topN, Order order) { + final TopNCondition condition = new TopNCondition(); + condition.setName(name); + condition.setTopN(topN); + condition.setOrder(order); + return condition; + } + + private Duration buildDuration() { + final Duration duration = new Duration(); + duration.setStart("2023-01-01 0000"); + duration.setEnd("2023-01-02 0000"); + duration.setStep(Step.MINUTE); + return duration; + } + + @Test + void buildSQL_shouldContainTableColumnAndTimeBucketRange() { + final TopNCondition condition = buildCondition("service_resp_time", 10, Order.DES); + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE); + + assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?"); + assertThat(result.sql()).contains(Metrics.TIME_BUCKET + " >= ?"); + assertThat(result.sql()).contains(Metrics.TIME_BUCKET + " <= ?"); + } + + @Test + void buildSQL_shouldContainSubqueryWithGroupBy() { + final TopNCondition condition = buildCondition("service_resp_time", 10, Order.DES); + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE); + + assertThat(result.sql()).contains("avg(value) as result"); + assertThat(result.sql()).contains("group by " + Metrics.ENTITY_ID); + assertThat(result.sql()).contains("as T order by result"); + } + + @Test + void buildSQL_withOrderDES_shouldContainDesc() { + final TopNCondition condition = buildCondition("service_resp_time", 5, Order.DES); + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE); + + assertThat(result.sql()).contains("order by result desc"); + assertThat(result.sql()).contains("limit 5"); + } + + @Test + void buildSQL_withOrderASC_shouldContainAsc() { + final TopNCondition condition = buildCondition("service_resp_time", 5, Order.ASC); + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE); + + assertThat(result.sql()).contains("order by result asc"); + } + + @Test + void buildSQL_withAdditionalConditions_shouldAppendConditions() { + final TopNCondition condition = buildCondition("service_resp_time", 10, Order.DES); + final Duration duration = buildDuration(); + final KeyValue kv = new KeyValue("service_id", "svc-1"); + + final SQLAndParameters result = dao.buildSQL( + condition, "value", duration, Collections.singletonList(kv), TABLE); + + assertThat(result.sql()).contains("service_id = ?"); + assertThat(result.parameters()).contains("svc-1"); + } +} diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAOTest.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAOTest.java new file mode 100644 index 000000000000..9488ab417226 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAOTest.java @@ -0,0 +1,83 @@ +/* + * 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.common.dao; + +import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingDataRecord; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class JDBCEBPFProfilingDataDAOTest { + + private static final String TABLE = "ebpf_profiling_data"; + + @Mock + private JDBCClient jdbcClient; + @Mock + private TableHelper tableHelper; + + private JDBCEBPFProfilingDataDAO dao; + + @BeforeEach + void setUp() { + dao = new JDBCEBPFProfilingDataDAO(jdbcClient, tableHelper); + } + + @Test + void buildSQL_shouldContainTableColumnCondition() { + final SQLAndParameters result = dao.buildSQL( + Arrays.asList("schedule-1"), 1000L, 2000L, TABLE); + + assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?"); + assertThat(result.parameters()).contains(EBPFProfilingDataRecord.INDEX_NAME); + } + + @Test + void buildSQL_shouldContainScheduleIdInClause() { + final SQLAndParameters result = dao.buildSQL( + Arrays.asList("s1", "s2", "s3"), 1000L, 2000L, TABLE); + + assertThat(result.sql()).contains(EBPFProfilingDataRecord.SCHEDULE_ID + " in (?, ?, ?)"); + } + + @Test + void buildSQL_shouldContainUploadTimeRange() { + final SQLAndParameters result = dao.buildSQL( + Arrays.asList("schedule-1"), 1000L, 2000L, TABLE); + + assertThat(result.sql()).contains(EBPFProfilingDataRecord.UPLOAD_TIME + ">=?"); + assertThat(result.sql()).contains(EBPFProfilingDataRecord.UPLOAD_TIME + "= ?"); + assertThat(result.sql()).contains(TopN.TIME_BUCKET + " <= ?"); + } + + @Test + void buildSQL_withOrderDES_shouldContainDesc() { + final RecordCondition condition = buildCondition("top_n_db", 5, Order.DES); + final Duration duration = buildDuration(); + + final SQLAndParameters result = JDBCRecordsQueryDAO.buildSQL(condition, "latency", duration, TABLE); + + assertThat(result.sql()).contains("order by latency desc"); + assertThat(result.sql()).contains("limit 5"); + } + + @Test + void buildSQL_withOrderASC_shouldContainAsc() { + final RecordCondition condition = buildCondition("top_n_db", 5, Order.ASC); + final Duration duration = buildDuration(); + + final SQLAndParameters result = JDBCRecordsQueryDAO.buildSQL(condition, "latency", duration, TABLE); + + assertThat(result.sql()).contains("order by latency asc"); + } +} diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAOTest.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAOTest.java new file mode 100644 index 000000000000..d7b724d0860b --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAOTest.java @@ -0,0 +1,105 @@ +/* + * 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.common.dao; + +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.query.enumeration.Step; +import org.apache.skywalking.oap.server.core.query.input.Duration; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class JDBCTagAutoCompleteQueryDAOTest { + + private static final String TABLE = "tag_autocomplete_table"; + + @Mock + private JDBCClient jdbcClient; + @Mock + private TableHelper tableHelper; + + private JDBCTagAutoCompleteQueryDAO dao; + + @BeforeEach + void setUp() { + dao = new JDBCTagAutoCompleteQueryDAO(jdbcClient, tableHelper); + } + + private Duration buildDuration() { + final Duration duration = new Duration(); + duration.setStart("2023-01-01 0000"); + duration.setEnd("2023-01-02 0000"); + duration.setStep(Step.MINUTE); + return duration; + } + + @Test + void buildSQLForQueryKeys_shouldContainDistinctAndTagType() { + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQLForQueryKeys(TagType.TRACE, 100, duration, TABLE); + + assertThat(result.sql()).contains("select distinct " + TagAutocompleteData.TAG_KEY); + assertThat(result.sql()).contains(TagAutocompleteData.TAG_TYPE + " = ?"); + assertThat(result.parameters()).contains(TagType.TRACE.name()); + } + + @Test + void buildSQLForQueryKeys_shouldContainLimit() { + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQLForQueryKeys(TagType.TRACE, 50, duration, TABLE); + + assertThat(result.sql()).contains("limit 50"); + } + + @Test + void buildSQLForQueryValues_shouldContainTagKeyCondition() { + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQLForQueryValues( + TagType.TRACE, "http.method", 100, duration, TABLE); + + assertThat(result.sql()).contains(TagAutocompleteData.TAG_KEY + " = ?"); + assertThat(result.parameters()).contains("http.method"); + } + + @Test + void buildSQLForQueryValues_shouldContainTagTypeCondition() { + final Duration duration = buildDuration(); + + final SQLAndParameters result = dao.buildSQLForQueryValues( + TagType.LOG, "level", 100, duration, TABLE); + + assertThat(result.sql()).contains(TagAutocompleteData.TAG_TYPE + " = ?"); + assertThat(result.parameters()).contains(TagType.LOG.name()); + } +}