Skip to content

Commit d1d9803

Browse files
yuluo-yxAias00
andauthored
refactor: extract base data for plugin, selector and rule data class (#6215)
* refactor: extract base data for plugin, selector and rule data class Signed-off-by: yuluo-yx <[email protected]> * fix: fix ut Signed-off-by: yuluo-yx <[email protected]> * fix: fix checksyle error Signed-off-by: yuluo-yx <[email protected]> --------- Signed-off-by: yuluo-yx <[email protected]> Co-authored-by: aias00 <[email protected]>
1 parent 3c3a4a5 commit d1d9803

File tree

5 files changed

+256
-370
lines changed

5 files changed

+256
-370
lines changed

shenyu-admin/src/test/java/org/apache/shenyu/admin/listener/websocket/WebsocketDataChangedListenerTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void before() {
7272
*/
7373
@Test
7474
public void testOnPluginChanged() {
75-
String message = "{\"groupType\":\"PLUGIN\",\"eventType\":\"UPDATE\",\"data\":[{\"id\":\"2\",\"name\":\"waf\","
76-
+ "\"config\":\"{\\\\\\\"model\\\\\\\":\\\\\\\"black\\\\\\\"}\",\"role\":\"1\",\"enabled\":true,\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
75+
String message = "{\"groupType\":\"PLUGIN\",\"eventType\":\"UPDATE\",\"data\":[{\"config\":\"{\\\\\\\"model\\\\\\\":\\\\\\\"black\\\\\\\"}\","
76+
+ "\"role\":\"1\",\"id\":\"2\",\"name\":\"waf\",\"enabled\":true,\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
7777
MockedStatic.Verification verification = () -> WebsocketCollector.send(Constants.SYS_DEFAULT_NAMESPACE_ID, message, DataEventTypeEnum.UPDATE);
7878
try (MockedStatic<WebsocketCollector> mockedStatic = mockStatic(WebsocketCollector.class)) {
7979
mockedStatic.when(verification).thenAnswer((Answer<Void>) invocation -> null);
@@ -88,19 +88,25 @@ public void testOnPluginChanged() {
8888
@Test
8989
public void testOnSelectorChanged() {
9090
String message = "{\"groupType\":\"SELECTOR\",\"eventType\":\"UPDATE\",\"data\":"
91-
+ "[{\"id\":\"1336329408516136960\",\"pluginId\":\"5\",\"pluginName\":\"divide\",\"name\":"
92-
+ "\"/http\",\"matchMode\":0,\"type\":1,\"sort\":1,\"enabled\":true,\"logged\":true,"
91+
+ "[{\"pluginId\":\"5\",\"pluginName\":\"divide\","
92+
+ "\"matchMode\":0,\"type\":1,\"logged\":true,"
9393
+ "\"continued\":true,\"handle\":\"[{\\\\\\\"upstreamHost\\\\\\\":\\\\\\\"localhost\\\\\\\","
9494
+ "\\\\\\\"protocol\\\\\\\":\\\\\\\"http://\\\\\\\",\\\\\\\"upstreamUrl\\\\\\\":"
9595
+ "\\\\\\\"127.0.0.1:8187\\\\\\\",\\\\\\\"weight\\\\\\\":\\\\\\\"51\\\\\\\"},"
9696
+ "{\\\\\\\"upstreamHost\\\\\\\":\\\\\\\"localhost\\\\\\\",\\\\\\\"protocol\\\\\\\":"
9797
+ "\\\\\\\"http://\\\\\\\",\\\\\\\"upstreamUrl\\\\\\\":\\\\\\\"127.0.0.1:8188\\\\\\\","
9898
+ "\\\\\\\"weight\\\\\\\":\\\\\\\"49\\\\\\\"}]\",\"conditionList\":[{\"paramType\":\"uri\","
99-
+ "\"operator\":\"match\",\"paramName\":\"/\",\"paramValue\":\"/http/**\"}],\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
99+
+ "\"operator\":\"match\",\"paramName\":\"/\",\"paramValue\":\"/http/**\"}],\"id\":\"1336329408516136960\","
100+
+ "\"name\":\"/http\",\"enabled\":true,\"sort\":1,\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
100101
MockedStatic.Verification verification = () -> WebsocketCollector.send(Constants.SYS_DEFAULT_NAMESPACE_ID, message, DataEventTypeEnum.UPDATE);
102+
101103
try (MockedStatic<WebsocketCollector> mockedStatic = mockStatic(WebsocketCollector.class)) {
102104
mockedStatic.when(verification).thenAnswer((Answer<Void>) invocation -> null);
105+
106+
// 调用被测试的方法
103107
websocketDataChangedListener.onSelectorChanged(selectorDataList, DataEventTypeEnum.UPDATE);
108+
109+
// 验证
104110
mockedStatic.verify(verification);
105111
}
106112
}
@@ -110,12 +116,13 @@ public void testOnSelectorChanged() {
110116
*/
111117
@Test
112118
public void testOnRuleChanged() {
113-
String message = "{\"groupType\":\"RULE\",\"eventType\":\"UPDATE\",\"data\":[{\"id\":\"1336350040008105984\","
114-
+ "\"name\":\"test\",\"pluginName\":\"waf\",\"selectorId\":\"1336349806465064960\","
115-
+ "\"matchMode\":1,\"sort\":1,\"enabled\":true,\"loged\":true,\"handle\":"
119+
String message = "{\"groupType\":\"RULE\",\"eventType\":\"UPDATE\",\"data\":[{"
120+
+ "\"pluginName\":\"waf\",\"selectorId\":\"1336349806465064960\","
121+
+ "\"matchMode\":1,\"loged\":true,\"handle\":"
116122
+ "\"{\\\\\\\"permission\\\\\\\":\\\\\\\"reject\\\\\\\",\\\\\\\"statusCode\\\\\\\":"
117123
+ "\\\\\\\"503\\\\\\\"}\",\"conditionDataList\":[{\"paramType\":\"header\",\"operator\":"
118-
+ "\"\\u003d\",\"paramName\":\"test\",\"paramValue\":\"a\"}],\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
124+
+ "\"\\u003d\",\"paramName\":\"test\",\"paramValue\":\"a\"}],\"id\":\"1336350040008105984\",\"name\":\"test\","
125+
+ "\"enabled\":true,\"sort\":1,\"namespaceId\":\"649330b6-c2d7-4edc-be8e-8a54df9eb385\"}]}";
119126
MockedStatic.Verification verification = () -> WebsocketCollector.send(Constants.SYS_DEFAULT_NAMESPACE_ID, message, DataEventTypeEnum.UPDATE);
120127
try (MockedStatic<WebsocketCollector> mockedStatic = mockStatic(WebsocketCollector.class)) {
121128
mockedStatic.when(verification).thenAnswer((Answer<Void>) invocation -> null);
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shenyu.common.dto;
19+
20+
/**
21+
* Base class for common data transfer objects.
22+
* Contains common fields shared across SelectorData, RuleData, and PluginData.
23+
*/
24+
25+
public class BaseData {
26+
27+
/**
28+
* primary key id.
29+
*/
30+
private String id;
31+
32+
/**
33+
* name.
34+
*/
35+
private String name;
36+
37+
/**
38+
* whether enabled.
39+
*/
40+
private Boolean enabled;
41+
42+
/**
43+
* sort order.
44+
*/
45+
private Integer sort;
46+
47+
/**
48+
* namespace id.
49+
*/
50+
private String namespaceId;
51+
52+
/**
53+
* get id.
54+
*
55+
* @return id
56+
*/
57+
public String getId() {
58+
return id;
59+
}
60+
61+
/**
62+
* set id.
63+
*
64+
* @param id id
65+
* @return this
66+
*/
67+
public BaseData setId(final String id) {
68+
this.id = id;
69+
return this;
70+
}
71+
72+
/**
73+
* get name.
74+
*
75+
* @return name
76+
*/
77+
public String getName() {
78+
return name;
79+
}
80+
81+
/**
82+
* set name.
83+
*
84+
* @param name name
85+
* @return this
86+
*/
87+
public BaseData setName(final String name) {
88+
this.name = name;
89+
return this;
90+
}
91+
92+
/**
93+
* get enabled.
94+
*
95+
* @return enabled
96+
*/
97+
public Boolean getEnabled() {
98+
return enabled;
99+
}
100+
101+
/**
102+
* set enabled.
103+
*
104+
* @param enabled enabled
105+
* @return this
106+
*/
107+
public BaseData setEnabled(final Boolean enabled) {
108+
this.enabled = enabled;
109+
return this;
110+
}
111+
112+
/**
113+
* get sort.
114+
*
115+
* @return sort
116+
*/
117+
public Integer getSort() {
118+
return sort;
119+
}
120+
121+
/**
122+
* set sort.
123+
*
124+
* @param sort sort
125+
* @return this
126+
*/
127+
public BaseData setSort(final Integer sort) {
128+
this.sort = sort;
129+
return this;
130+
}
131+
132+
/**
133+
* get namespaceId.
134+
*
135+
* @return namespaceId
136+
*/
137+
public String getNamespaceId() {
138+
return namespaceId;
139+
}
140+
141+
/**
142+
* set namespaceId.
143+
*
144+
* @param namespaceId namespaceId
145+
* @return this
146+
*/
147+
public BaseData setNamespaceId(final String namespaceId) {
148+
this.namespaceId = namespaceId;
149+
return this;
150+
}
151+
}

0 commit comments

Comments
 (0)