Skip to content

Commit

Permalink
Fixed integration tests.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jan 11, 2024
1 parent 65af048 commit 48221f7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 52 deletions.
4 changes: 3 additions & 1 deletion plugins/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ A plugin that returns the OpenSearch API in the OpenAPI format.
### Build and Test

```sh
./gradlew plugins:api:test
./gradlew :plugins:api:test # unit tests
./gradlew :plugins:api:integTest # integration tests
./gradlew :plugins:api:yamlRestTest # YAML REST tests
```

### Run
Expand Down
10 changes: 10 additions & 0 deletions plugins/api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import org.opensearch.gradle.info.BuildParams

apply plugin: 'opensearch.build'
apply plugin: 'opensearch.publish'
apply plugin: 'opensearch.testclusters'
apply plugin: 'opensearch.yaml-rest-test'
apply plugin: 'opensearch.rest-test'

opensearchplugin {
description 'The API plugin returns an OpenAPI spec.'
Expand All @@ -38,3 +40,11 @@ restResources {

thirdPartyAudit.enabled = false
testingConventions.enabled = false

tasks.named("integTest").configure {
it.dependsOn(project.tasks.named("bundlePlugin"))
}

testClusters.integTest {
plugin(project.tasks.bundlePlugin.archiveFile)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@

import org.opensearch.Build;
import org.opensearch.Version;
import org.opensearch.core.ParseField;
import org.opensearch.action.main.MainResponse;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ObjectParser;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;

import java.io.IOException;
import java.util.Objects;

import static org.opensearch.action.main.MainResponse.TAGLINE;

public class APIResponse extends ActionResponse implements ToXContentObject {

private Version version;
Expand Down Expand Up @@ -73,7 +69,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

builder.startObject("info")
.field("title", build.getDistribution())
.field("description", TAGLINE)
.field("description", MainResponse.TAGLINE)
.field("version", build.getQualifiedVersion())
.endObject();

Expand All @@ -83,46 +79,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

private static final ObjectParser<APIResponse, Void> PARSER = new ObjectParser<>(APIResponse.class.getName(), true, APIResponse::new);

static {
PARSER.declareObject((response, value) -> {
final String buildType = (String) value.get("build_type");
response.build = new Build(
buildType == null ? Build.Type.UNKNOWN : Build.Type.fromDisplayName(buildType, false),
(String) value.get("build_hash"),
(String) value.get("build_date"),
(boolean) value.get("build_snapshot"),
(String) value.get("number"),
(String) value.get("distribution")
);
response.version = Version.fromString(
((String) value.get("number")).replace("-SNAPSHOT", "").replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "")
);
}, (parser, context) -> parser.map(), new ParseField("version"));
}

public static APIResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
APIResponse other = (APIResponse) o;
return Objects.equals(version, other.version) && Objects.equals(build, other.build);
}

@Override
public int hashCode() {
return Objects.hash(version, build);
}

@Override
public String toString() {
return "APIResponse{" + '\'' + ", version=" + version + '\'' + ", build=" + build + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ public void testPluginInstalled() throws IOException, ParseException {
Response response = createRestClient().performRequest(new Request("GET", "/_cat/plugins"));
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);

logger.info("response body: {}", body);
logger.info("response body: {}", body.toString());
assertTrue(body.contains("api"));
}

public void testPluginGetAPI() throws IOException, ParseException {
Response response = createRestClient().performRequest(new Request("GET", "/_plugins/api"));
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
logger.info("response body: {}", body);

APIResponse apiResponse = APIResponse.fromXContent(createParser(JsonXContent.jsonXContent, response.getEntity().getContent()));

assertEquals(apiResponse.getVersion(), Version.CURRENT);
assertTrue(body.startsWith("{\"openapi\":\"3.0.1\""));
}
}

0 comments on commit 48221f7

Please sign in to comment.