You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/05/03 14:04:13 UTC
[incubator-shenyu] branch master updated: Add shenyu-register-instance-consul Test (#3376)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new b4857520d Add shenyu-register-instance-consul Test (#3376)
b4857520d is described below
commit b4857520d4dc2aadde761991048d3b80a1095ee8
Author: renzhuyan <40...@qq.com>
AuthorDate: Tue May 3 22:04:04 2022 +0800
Add shenyu-register-instance-consul Test (#3376)
---
.../ConsulInstanceRegisterRepositoryTest.java | 91 ++++++++++++++++++++++
1 file changed, 91 insertions(+)
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/test/java/org/apache/shenyu/register/instance/consul/ConsulInstanceRegisterRepositoryTest.java b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/test/java/org/apache/shenyu/register/instance/consul/ConsulInstanceRegisterRepositoryTest.java
new file mode 100644
index 000000000..7a467ba6b
--- /dev/null
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/test/java/org/apache/shenyu/register/instance/consul/ConsulInstanceRegisterRepositoryTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.shenyu.register.instance.consul;
+
+import com.ecwid.consul.v1.ConsulClient;
+import com.ecwid.consul.v1.agent.model.NewCheck;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+class ConsulInstanceRegisterRepositoryTest {
+
+ private ConsulInstanceRegisterRepository repository;
+
+ private final Map<String, String> consulBroker = new HashMap<>();
+
+ @BeforeEach
+ public void setUp() throws NoSuchFieldException, IllegalAccessException {
+ this.repository = new ConsulInstanceRegisterRepository();
+ Class<? extends ConsulInstanceRegisterRepository> clazz = this.repository.getClass();
+
+ Field consulClientField = clazz.getDeclaredField("consulClient");
+ consulClientField.setAccessible(true);
+ consulClientField.set(repository, mockConsulClient());
+
+ Field checkField = clazz.getDeclaredField("check");
+ checkField.setAccessible(true);
+ checkField.set(repository, mockNewCheck());
+
+ consulBroker.clear();
+ }
+
+ private ConsulClient mockConsulClient() {
+ ConsulClient consulClient = mock(ConsulClient.class);
+
+ doAnswer(invocationOnMock -> {
+ String key = invocationOnMock.getArgument(0);
+ String value = invocationOnMock.getArgument(1);
+ consulBroker.put(key, value);
+ return null;
+ }).when(consulClient).setKVValue(anyString(), anyString());
+
+ return consulClient;
+ }
+
+ private NewCheck mockNewCheck() {
+ return mock(NewCheck.class);
+ }
+
+ @Test
+ public void testPersistInstance() {
+ InstanceRegisterDTO data = InstanceRegisterDTO.builder()
+ .appName("shenyu-test")
+ .host("shenyu-host")
+ .port(9195)
+ .build();
+
+ final String realNode = "/shenyu/register/instance/shenyu-host:9195";
+ repository.persistInstance(data);
+ assertTrue(consulBroker.containsKey(realNode));
+ assertEquals(GsonUtils.getInstance().toJson(data), consulBroker.get(realNode));
+ repository.close();
+ }
+
+}