You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/12/20 10:16:46 UTC

[shardingsphere] branch master updated: Add YamlAdvisorsConfigurationLoaderTest (#22993)

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

sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 06363b09398 Add YamlAdvisorsConfigurationLoaderTest (#22993)
06363b09398 is described below

commit 06363b0939894ce8ac9baacff39b3053099b8cc1
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Dec 20 18:16:37 2022 +0800

    Add YamlAdvisorsConfigurationLoaderTest (#22993)
---
 .../loader/YamlAdvisorsConfigurationLoader.java    |  3 +-
 .../YamlAdvisorsConfigurationLoaderTest.java       | 90 ++++++++++++++++++++++
 .../src/test/resources/conf/empty-advisors.yaml    | 16 ++++
 3 files changed, 108 insertions(+), 1 deletion(-)

diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java
index 9ab26dfd64d..334d98f8b50 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java
@@ -34,6 +34,7 @@ public final class YamlAdvisorsConfigurationLoader {
      * @return loaded advisors configuration
      */
     public YamlAdvisorsConfiguration load(final InputStream inputStream) {
-        return new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
+        YamlAdvisorsConfiguration result = new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
+        return null == result ? new YamlAdvisorsConfiguration() : result;
     }
 }
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoaderTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoaderTest.java
new file mode 100644
index 00000000000..95c1a5829ce
--- /dev/null
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoaderTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.shardingsphere.agent.core.plugin.yaml.loader;
+
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorsConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlPointcutConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlPointcutParameterConfiguration;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class YamlAdvisorsConfigurationLoaderTest {
+    
+    @Test
+    public void assertLoad() {
+        YamlAdvisorsConfiguration actual = new YamlAdvisorsConfigurationLoader().load(getClass().getResourceAsStream("/conf/advisors.yaml"));
+        assertThat(actual.getAdvisors().size(), is(1));
+        assertYamlAdvisorConfiguration(actual.getAdvisors().iterator().next());
+    }
+    
+    private void assertYamlAdvisorConfiguration(final YamlAdvisorConfiguration actual) {
+        assertThat(actual.getTarget(), is("org.apache.shardingsphere.agent.core.plugin.yaml.fixture.YamlTargetObjectFixture"));
+        assertThat(actual.getAdvice(), is("org.apache.shardingsphere.agent.core.plugin.yaml.fixture.YamlAdviceFixture"));
+        assertThat(actual.getPointcuts().size(), is(8));
+        List<YamlPointcutConfiguration> actualYamlPointcutConfigs = new ArrayList<>(actual.getPointcuts());
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(0), null, "constructor", Collections.emptyList());
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(1), null, "constructor", Collections.singletonList(createYamlPointcutParameterConfiguration(0, "value")));
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(2), "call", "method", Collections.emptyList());
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(3), "call", "method", Collections.singletonList(createYamlPointcutParameterConfiguration(0, "value")));
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(4), "call", "method",
+                Arrays.asList(createYamlPointcutParameterConfiguration(0, "value1"), createYamlPointcutParameterConfiguration(1, "value2")));
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(5), "staticCall", "method", Collections.emptyList());
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(6), "staticCall", "method", Collections.singletonList(createYamlPointcutParameterConfiguration(0, "value")));
+        assertYamlPointcutConfiguration(actualYamlPointcutConfigs.get(7), "staticCall", "method",
+                Arrays.asList(createYamlPointcutParameterConfiguration(0, "value1"), createYamlPointcutParameterConfiguration(1, "value2")));
+    }
+    
+    private void assertYamlPointcutConfiguration(final YamlPointcutConfiguration actual,
+                                                 final String expectedName, final String expectedType, final List<YamlPointcutParameterConfiguration> expectedParams) {
+        assertThat(actual.getName(), is(expectedName));
+        assertThat(actual.getType(), is(expectedType));
+        assertThat(actual.getParams().size(), is(expectedParams.size()));
+        int count = 0;
+        for (YamlPointcutParameterConfiguration each : actual.getParams()) {
+            assertYamlPointcutParameterConfiguration(each, expectedParams.get(count));
+            count++;
+        }
+    }
+    
+    private void assertYamlPointcutParameterConfiguration(final YamlPointcutParameterConfiguration actual, final YamlPointcutParameterConfiguration expected) {
+        assertThat(actual.getIndex(), is(expected.getIndex()));
+        assertThat(actual.getName(), is(expected.getName()));
+    }
+    
+    private YamlPointcutParameterConfiguration createYamlPointcutParameterConfiguration(final int index, final String name) {
+        YamlPointcutParameterConfiguration result = new YamlPointcutParameterConfiguration();
+        result.setIndex(index);
+        result.setName(name);
+        return result;
+    }
+    
+    @Test
+    public void assertLoadEmptyFile() {
+        YamlAdvisorsConfiguration actual = new YamlAdvisorsConfigurationLoader().load(getClass().getResourceAsStream("/conf/empty-advisors.yaml"));
+        assertTrue(actual.getAdvisors().isEmpty());
+    }
+}
diff --git a/agent/core/src/test/resources/conf/empty-advisors.yaml b/agent/core/src/test/resources/conf/empty-advisors.yaml
new file mode 100644
index 00000000000..b1312a0905c
--- /dev/null
+++ b/agent/core/src/test/resources/conf/empty-advisors.yaml
@@ -0,0 +1,16 @@
+#
+# 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.
+#