Skip to content

Commit

Permalink
Merge branch 'plugin' of https://github.com/jakiuncle/shenyu into plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jakiuncle committed Jan 19, 2025
2 parents af6ee61 + 81ad3da commit 0e01725
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.apache.shenyu.registry.api.config;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
Expand Down Expand Up @@ -127,7 +130,7 @@ public void setEnabled(final boolean enabled) {

@Override
public boolean equals(final Object obj) {
if (obj == null) {
if (Objects.isNull(obj)) {
return false;
}
RegisterConfig registerConfig = (RegisterConfig) obj;
Expand All @@ -137,18 +140,20 @@ public boolean equals(final Object obj) {
if (!this.getServerLists().equals(registerConfig.getServerLists())) {
return false;
}
if (this.getProps() == null && registerConfig.getProps() == null) {
Properties properties = this.getProps();
Properties registerConfigProps = registerConfig.getProps();
if (Objects.isNull(properties) && Objects.isNull(registerConfigProps)) {
return true;
}
if (this.getProps() == null || registerConfig.getProps() == null) {
if (Objects.isNull(properties) || Objects.isNull(registerConfigProps)) {
return false;
}
if (this.getProps().entrySet().size() != registerConfig.getProps().entrySet().size()) {
if (properties.entrySet().size() != registerConfigProps.entrySet().size()) {
return false;
}
for (Map.Entry<Object, Object> entry : this.getProps().entrySet()) {
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object newValue = entry.getValue();
Object oldValue = registerConfig.getProps().get(entry.getKey());
Object oldValue = registerConfigProps.get(entry.getKey());
if (!newValue.equals(oldValue)) {
return false;
}
Expand All @@ -158,13 +163,18 @@ public boolean equals(final Object obj) {

@Override
public int hashCode() {
int result = getRegisterType() != null ? getRegisterType().hashCode() : 0;
result = 31 * result + (getServerLists() != null ? getServerLists().hashCode() : 0);
String registerTypeStr = getRegisterType();
int result = StringUtils.isNotEmpty(registerTypeStr) ? registerTypeStr.hashCode() : 0;
String serverListsStr = getServerLists();
result = 31 * result + (StringUtils.isNotEmpty(serverListsStr) ? serverListsStr.hashCode() : 0);

if (getProps() != null) {
for (Map.Entry<Object, Object> entry : getProps().entrySet()) {
result = 31 * result + (entry.getKey() != null ? entry.getKey().hashCode() : 0);
result = 31 * result + (entry.getValue() != null ? entry.getValue().hashCode() : 0);
Properties properties = getProps();
if (Objects.nonNull(properties)) {
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object entryKey = entry.getKey();
result = 31 * result + (Objects.nonNull(entryKey) ? entryKey.hashCode() : 0);
Object entryValue = entry.getValue();
result = 31 * result + (Objects.nonNull(entryValue) ? entryValue.hashCode() : 0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public boolean equals(final Object o) {
return Boolean.TRUE;
}

if (o == null || getClass() != o.getClass()) {
if (Objects.isNull(o) || getClass() != o.getClass()) {
return Boolean.FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -46,10 +47,10 @@ public KubernetesClient(final KubernetesConfig kubernetesConfig) {
*/
public List<KubernetesInstance> selectInstances(final String serviceId) {
List<KubernetesInstance> response = Collections.emptyList();
KubernetesInstance[] responseBody = (KubernetesInstance[]) this.rest.getForEntity(this.kubernetesConfig.getDiscoveryServerUrl() + "/apps/" + serviceId,
KubernetesInstance[] responseBody = this.rest.getForEntity(this.kubernetesConfig.getDiscoveryServerUrl() + "/apps/" + serviceId,
KubernetesInstance[].class, new Object[0]).getBody();
if (responseBody != null && responseBody.length > 0) {
response = (List) Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
if (Objects.nonNull(responseBody) && responseBody.length > 0) {
response = Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
}

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (Objects.isNull(o) || getClass() != o.getClass()) {
return false;
}
HttpConfig that = (HttpConfig) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (Objects.isNull(o) || getClass() != o.getClass()) {
return false;
}
NacosACMConfig that = (NacosACMConfig) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (Objects.isNull(o) || getClass() != o.getClass()) {
return false;
}
NacosConfig that = (NacosConfig) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (Objects.isNull(o) || getClass() != o.getClass()) {
return false;
}
WebsocketConfig that = (WebsocketConfig) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* RestTemplate config.
*/
@Configuration
public class RestTemplateConfig {
public class RestTemplateConfiguration {

/**
* ClientHttpRequestFactory bean.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.web.configuration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test case for {@link RestTemplateConfiguration}.
*/
@Configuration
@EnableConfigurationProperties
public class RestTemplateConfigurationTest {

private ApplicationContextRunner applicationContextRunner;

@BeforeEach
public void setUp() {
applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RestTemplateConfiguration.class))
.withBean(RestTemplateConfigurationTest.class)
.withPropertyValues("debug=true");
}

@Test
public void testSimpleClientHttpRequestFactory() {
applicationContextRunner.run(context -> {
ClientHttpRequestFactory clientHttpRequestFactory = context.getBean("simpleClientHttpRequestFactory", SimpleClientHttpRequestFactory.class);
assertNotNull(clientHttpRequestFactory);
});
}

@Test
public void testRestTemplate() {
applicationContextRunner.run(context -> {
RestTemplate restTemplate = context.getBean("restTemplate", RestTemplate.class);
assertNotNull(restTemplate);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.web.endpoint;

import org.apache.shenyu.common.enums.TrieCacheTypeEnum;
import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
import org.apache.shenyu.plugin.base.trie.ShenyuTrie;
import org.apache.shenyu.web.handler.ShenyuWebHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Test cases for {@link ShenyuControllerEndpoint}.
*/
public final class ShenyuControllerEndpointTest {

private MockMvc mockMvc;

@BeforeEach
public void setUp() {
ShenyuControllerEndpoint shenyuControllerEndpoint = new ShenyuControllerEndpoint(mock(ShenyuWebHandler.class));
this.mockMvc = MockMvcBuilders.standaloneSetup(shenyuControllerEndpoint).build();
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
SpringBeanUtils.getInstance().setApplicationContext(context);
when(context.getBean(TrieCacheTypeEnum.SELECTOR.getTrieType())).thenReturn(mock(ShenyuTrie.class));
when(context.getBean(TrieCacheTypeEnum.RULE.getTrieType())).thenReturn(mock(ShenyuTrie.class));
}

@Test
public void plugins() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/plugins"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void pluginDatas() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/pluginData"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void selectorData() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/selectorData"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void ruleData() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/ruleData"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void getSelectorMatchCache() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/selectorMatchCache"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void getRuleMatchCache() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/ruleMatchCache"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void getMetadata() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/metadata"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void getMetaDataCache() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/metadataCache"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void getSelectorTrieKeys() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/selectorTrie"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}

@Test
public void getRuleTrieKeys() throws Exception {
final MockHttpServletResponse response = this.mockMvc.perform(MockMvcRequestBuilders.get("/actuator/ruleTrie"))
.andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}
}

0 comments on commit 0e01725

Please sign in to comment.