This is an automated email from the ASF dual-hosted git repository.
zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new c8fe281be3 Create MotanServiceEventListenerTest.java (#5402)
c8fe281be3 is described below
commit c8fe281be31263a1c605dbfc6d0f0e4c5f2a2724
Author: Divyansh200102 <[email protected]>
AuthorDate: Wed Jan 17 06:50:24 2024 +0530
Create MotanServiceEventListenerTest.java (#5402)
---
.../test/MotanServiceEventListenerTest.java | 256 +++++++++++++++++++++
1 file changed, 256 insertions(+)
diff --git
a/shenyu-client/shenyu-client-motan/test/MotanServiceEventListenerTest.java
b/shenyu-client/shenyu-client-motan/test/MotanServiceEventListenerTest.java
new file mode 100644
index 0000000000..ef744fd858
--- /dev/null
+++ b/shenyu-client/shenyu-client-motan/test/MotanServiceEventListenerTest.java
@@ -0,0 +1,256 @@
+/*
+ * 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.client.motan;
+
+import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
+import
org.apache.shenyu.client.core.register.ShenyuClientRegisterRepositoryFactory;
+import org.apache.shenyu.client.motan.common.annotation.ShenyuMotanClient;
+import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.register.common.config.PropertiesConfig;
+import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
+import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
+import org.apache.shenyu.register.common.dto.URIRegisterDTO;
+import org.apache.shenyu.register.common.enums.EventType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.ContextRefreshedEvent;
+
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.mockito.BDDMockito.given;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Test for {@link MotanServiceEventListener}.
+ */
+@ExtendWith(MockitoExtension.class)
+public class MotanServiceEventListenerTest {
+
+ private static final String CONTEXT_PATH = "/motan";
+
+ private static final String PORT = "8080";
+
+ private static final String HOST = "127.0.0.1";
+
+ private static final String USERNAME = "admin";
+
+ private static final String PASSWORD = "123456";
+
+ private static final String PATH = "path";
+
+ private static final String APP_NAME = "appName";
+
+ private static final String SUPER_PATH_CONTAINS_STAR = "/demo/**";
+
+ private static final String SUPER_PATH_NOT_CONTAINS_STAR =
"/findByIdsAndName";
+
+ private static final String METHOD_NAME = "buildURIRegisterDTO";
+
+ private static final String SERVICE_NAME = "java.lang.Comparable";
+
+ private static final String DESC = "desc";
+
+ private static final String CONFIG_RULE_NAME = "configRuleName";
+
+ private static final String LOAD_BALANCE = "loadBalance";
+
+ private static final int RETRY_TIME = 0;
+
+ private static final int TIME_OUT = 0;
+
+ private static final boolean ENABLED = true;
+
+ @InjectMocks
+ private MotanServiceEventListener motanServiceEventListener =
buildMotanServiceEventListener();
+
+ @Mock
+ private ApplicationContext applicationContext;
+
+ @Mock
+ private ShenyuMotanClient shenyuMotanClient;
+
+ @Mock
+ private Method method;
+
+ @Mock
+ private ServiceFactoryBean serviceFactoryBean;
+
+ @Mock
+ private ContextRefreshedEvent contextRefreshedEvent;
+
+ @Test
+ public void testGetBeans() {
+ motanServiceEventListener.getBeans(applicationContext);
+
+ verify(applicationContext,
times(1)).getBeansOfType(ShenyuMotanclient.class);
+ }
+
+ @Test
+ public void testBuildURIRegisterDTO() {
+ URIRegisterDTO expectedURIRegisterDTO = URIRegisterDTO.builder()
+ .contextPath(CONTEXT_PATH)
+ .appName(APP_NAME)
+ .rpcType(RpcTypeEnum.MOTAN.getName())
+ .eventType(EventType.REGISTER)
+ .host(HOST)
+ .port(Integer.parseInt(PORT))
+ .build();
+ Map<String,Object> beans = new HashMap<>();
+ URIRegisterDTO realURIRegisterDTO =
motanServiceEventListener.buildURIRegisterDTO(applicationContext, beans);
+
+ assertEquals(expectedURIRegisterDTO, realURIRegisterDTO);
+ }
+
+ @Test
+ public void testBuildApiSuperPathWhenShenyuMotanClientIsNull() {
+ Class<?> clazz = Class.class;
+ String realSuperPath =
motanServiceEventListener.buildApiSuperPath(clazz, null);
+
+ verify(shenyuMotanClient, times(0)).path();
+ assertEquals("", realSuperPath);
+ }
+
+ @Test
+ public void testBuildApiSuperPathWhenShenyuMotanClientPathIsEmpty() {
+ Class<?> clazz = Class.class;
+ given(shenyuMotanClient.path()).willReturn("");
+ String realSuperPath =
motanServiceEventListener.buildApiSuperPath(clazz, shenyuMotanClient);
+
+ verify(shenyuMotanClient, times(1)).path();
+ assertEquals("", realSuperPath);
+ }
+
+ @Test
+ public void testBuildApiSuperPath() {
+ Class<?> clazz = Class.class;
+ given(shenyuMotanClient.path()).willReturn(PATH);
+ String realSuperPath =
motanServiceEventListener.buildApiSuperPath(clazz, shenyuMotanClient);
+
+ verify(shenyuMotanClient, times(2)).path();
+ assertEquals(PATH, realSuperPath);
+ }
+
+ @Test
+ public void testGetAnnotationType() {
+ Class<?> clazz = motanServiceEventListener.getAnnotationType();
+
+ assertEquals(ShenyuMotanClient.class, clazz);
+ }
+
+ @Test
+ public void testBuildMetaDataDTOForMotan() throws NoSuchMethodException {
+
+ Method method =
MotanServiceEventListener.class.getDeclaredMethod(METHOD_NAME,
ApplicationContext.class, Map.class);
+
+
+ given(shenyuMotanClient.desc()).willReturn(DESC);
+ given(shenyuMotanClient.ruleName()).willReturn(CONFIG_RULE_NAME);
+ given(shenyuMotanClient.enabled()).willReturn(ENABLED);
+ given(shenyuMotanClient.loadBalance()).willReturn(LOAD_BALANCE);
+ given(shenyuMotanClient.retries()).willReturn(RETRY_TIME);
+ given(shenyuMotanClient.timeout()).willReturn(TIME_OUT);
+
+
+ BasicServiceConfigBean basicServiceConfigBean =
mock(BasicServiceConfigBean.class);
+ given(basicServiceConfigBean.getRequestTimeout()).willReturn(1000);
+
given(applicationContext.getBean(BASE_SERVICE_CONFIG)).willReturn(basicServiceConfigBean);
+
+
+ String expectedParameterTypes =
"org.springframework.context.ApplicationContext,java.util.Map,com.alipay.motan.runtime.spring.factory.Object";
+ String expectedPath = "/motan/findByIdsAndName/path";
+ String expectedRpcExt
="{\"loadbalance\":\"loadBalance\",\"retries\":0,\"timeout\":0}";
+
+
+ MetaDataRegisterDTO realMetaDataRegisterDTO =
motanServiceEventListener.buildMetaDataDTO(
+ Object,
+ shenyuMotanClient,
+ SUPER_PATH_NOT_CONTAINS_STAR,
+ MotanServiceEventListener.class,
+ method);
+
+ MetaDataRegisterDTO expectedMetaDataRegisterDTO =
MetaDataRegisterDTO.builder()
+ .appName(APP_NAME)
+ .serviceName(SERVICE_NAME)
+ .methodName(METHOD_NAME)
+ .contextPath(CONTEXT_PATH)
+ .path(expectedPath)
+ .port(Integer.parseInt(PORT))
+ .host(HOST)
+ .ruleName(CONFIG_RULE_NAME)
+ .pathDesc(DESC)
+ .parameterTypes(expectedParameterTypes)
+ .rpcType(RpcTypeEnum.MOTAN.getName())
+ .rpcExt(expectedRpcExt)
+ .enabled(ENABLED)
+ .build();
+
+ assertEquals(expectedMetaDataRegisterDTO, realMetaDataRegisterDTO);
+ }
+
+
+
+ @Test
+ public void testBuildApiPathSuperPathContainsStar() {
+ given(method.getName()).willReturn(METHOD_NAME);
+ String realApiPath = motanServiceEventListener.buildApiPath(method,
SUPER_PATH_CONTAINS_STAR, methodShenyuClient);
+ String expectedApiPath = "/motan/demo/buildURIRegisterDTO";
+
+ assertEquals(expectedApiPath, realApiPath);
+ }
+
+ @Test
+ public void testBuildApiPathSuperPathNotContainsStar() {
+ given( methodShenyuClient.path()).willReturn(PATH);
+ String realApiPath = motanServiceEventListener.buildApiPath(method,
SUPER_PATH_NOT_CONTAINS_STAR, methodShenyuClient);
+ String expectedApiPath = "/motan/findByIdsAndName/path";
+
+ assertEquals(expectedApiPath, realApiPath);
+ }
+
+
+
+ private MotanServiceEventListener buildMotanServiceEventListener() {
+ Properties properties = new Properties();
+ properties.setProperty("contextPath", CONTEXT_PATH);
+ properties.setProperty("port", PORT);
+ properties.setProperty("host", HOST);
+ properties.setProperty("username", USERNAME);
+ properties.setProperty("password", PASSWORD);
+ properties.setProperty("appName", APP_NAME);
+ PropertiesConfig config = new PropertiesConfig();
+ config.setProps(properties);
+
+ ShenyuRegisterCenterConfig mockRegisterCenter = new
ShenyuRegisterCenterConfig();
+ mockRegisterCenter.setServerLists("http://localhost:58080");
+ mockRegisterCenter.setRegisterType("http");
+ mockRegisterCenter.setProps(properties);
+
+ return new MotanServiceEventListener(config,
ShenyuClientRegisterRepositoryFactory.newInstance(mockRegisterCenter));
+ }
+
+}