Skip to content

Commit adc1350

Browse files
committed
Add basic unit and YAML REST tests
1 parent 5279783 commit adc1350

6 files changed

Lines changed: 152 additions & 14 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ src/main/java/org/easysearch/plugin/hello/
9494
}
9595
```
9696

97+
## 测试
98+
99+
模板现在包含最基础的两类测试:
100+
101+
- 单元测试:`src/test/java/org/easysearch/plugin/hello/rest/RestHelloActionTest.java`
102+
- YAML REST 套件入口:`src/yamlRestTest/java/org/easysearch/plugin/hello/HelloClientYamlTestSuiteIT.java`
103+
- YAML REST API 定义:`src/yamlRestTest/resources/rest-api-spec/api/hello.json`
104+
- YAML REST 测试:`src/yamlRestTest/resources/rest-api-spec/test/hello/10_basic.yml`
105+
106+
常用命令:
107+
108+
```bash
109+
./gradlew test
110+
./gradlew yamlRestTest
111+
./gradlew check
112+
```
113+
114+
说明:
115+
116+
- `test` 运行普通单元测试(类名需要匹配 `*Test`
117+
- 单元测试基于 `EasySearchTestCase`(推荐沿用 `testXxx` 方法命名)
118+
- `yamlRestTest` 会启动测试集群并执行 REST 用例(需要 `src/yamlRestTest/java` 下有 suite 类)
119+
- `check` 已配置为同时触发 `integTest``yamlRestTest`
120+
97121
## 版本兼容
98122

99123
| 组件 | 版本 |

build.gradle

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
apply plugin: 'easysearch.esplugin'
2-
3-
esplugin {
4-
name 'hello-plugin'
5-
description 'A simple hello plugin for Easysearch'
6-
classname "org.easysearch.plugin.hello.HelloPlugin"
7-
licenseFile rootProject.file('LICENSE.txt')
8-
noticeFile rootProject.file('NOTICE.txt')
9-
}
10-
11-
// Skip POM validation - this plugin is not published to maven.
12-
tasks.named('validateNebulaPom') {
13-
enabled = false
14-
}
1+
import org.easysearch.gradle.test.RestIntegTestTask
152

163
buildscript {
174
ext {
@@ -28,9 +15,35 @@ buildscript {
2815
}
2916
}
3017

18+
apply plugin: 'easysearch.esplugin'
19+
apply plugin: 'easysearch.yaml-rest-test'
20+
21+
esplugin {
22+
name 'hello-plugin'
23+
description 'A simple hello plugin for Easysearch'
24+
classname "org.easysearch.plugin.hello.HelloPlugin"
25+
licenseFile rootProject.file('LICENSE.txt')
26+
noticeFile rootProject.file('NOTICE.txt')
27+
}
28+
29+
// Skip POM validation - this plugin is not published to maven.
30+
tasks.named('validateNebulaPom') { enabled = false }
31+
3132
repositories {
3233
mavenCentral()
3334
gradlePluginPortal()
3435
maven { url "https://maven.aliyun.com/repository/central" }
3536
maven { url "https://maven.aliyun.com/repository/gradle-plugin" }
3637
}
38+
39+
test {
40+
include "**/*Test.class"
41+
}
42+
43+
task integTest(type: RestIntegTestTask) {
44+
description = "Run integration tests against an Easysearch test cluster"
45+
testClassesDirs = sourceSets.test.output.classesDirs
46+
classpath = sourceSets.test.runtimeClasspath
47+
}
48+
49+
tasks.named('check') { dependsOn integTest }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright © INFINI Ltd. All rights reserved.
2+
* Web: https://infinilabs.com
3+
* Email: hello#infini.ltd
4+
* SPDX-License-Identifier: Apache-2.0 */
5+
6+
package org.easysearch.plugin.hello.rest;
7+
8+
import org.easysearch.rest.RestRequest;
9+
import org.easysearch.test.EasySearchTestCase;
10+
11+
import java.util.List;
12+
13+
public class RestHelloActionTest extends EasySearchTestCase {
14+
15+
public void testGetName() {
16+
RestHelloAction action = new RestHelloAction();
17+
18+
assertEquals("hello_action", action.getName());
19+
}
20+
21+
public void testRoutes() {
22+
RestHelloAction action = new RestHelloAction();
23+
List<org.easysearch.rest.BaseRestHandler.Route> routes = action.routes();
24+
25+
assertEquals(1, routes.size());
26+
assertEquals(RestRequest.Method.GET, routes.get(0).getMethod());
27+
assertEquals("/_hello", routes.get(0).getPath());
28+
}
29+
}
30+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright © INFINI Ltd. All rights reserved.
2+
* Web: https://infinilabs.com
3+
* Email: hello#infini.ltd
4+
* SPDX-License-Identifier: Apache-2.0 */
5+
6+
package org.easysearch.plugin.hello;
7+
8+
import com.carrotsearch.randomizedtesting.annotations.Name;
9+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
10+
import org.easysearch.test.rest.yaml.ClientYamlTestCandidate;
11+
import org.easysearch.test.rest.yaml.ESClientYamlSuiteTestCase;
12+
13+
/**
14+
* Minimal YAML REST test suite entry for plugin REST tests.
15+
*/
16+
public class HelloClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
17+
18+
public HelloClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) {
19+
super(testCandidate);
20+
}
21+
22+
@ParametersFactory
23+
public static Iterable<Object[]> parameters() throws Exception {
24+
return createParameters();
25+
}
26+
}
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"hello": {
3+
"documentation": {
4+
"url": "https://github.com/infinilabs/easysearch-plugin-template",
5+
"description": "Returns a hello message from the example plugin"
6+
},
7+
"stability": "stable",
8+
"url": {
9+
"paths": [
10+
{
11+
"path": "/_hello",
12+
"methods": [
13+
"GET"
14+
]
15+
}
16+
]
17+
},
18+
"params": {
19+
"name": {
20+
"type": "string",
21+
"description": "Optional name to include in the greeting"
22+
}
23+
},
24+
"body": null
25+
}
26+
}
27+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"hello endpoint returns greeting with explicit name":
3+
- do:
4+
hello:
5+
name: "tom"
6+
7+
- match: { message: "Hello tom" }
8+
- is_true: cluster_name
9+
- is_true: node_name
10+
11+
---
12+
"hello endpoint uses default name":
13+
- do:
14+
hello: {}
15+
16+
- match: { message: "Hello World" }
17+

0 commit comments

Comments
 (0)