Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

fix/test: NotNull check for a type param in DeplomentsResource + test expansion #688

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -150,8 +151,10 @@ private String securityCheck(final String crd) {
@Operation(summary = "Get CRD",
description = "Returns the custom resource identified by name.")
public String resource(
final @Parameter(description = "Name of the resource to get.") @PathParam("name") String name,
final @Parameter(description = "Type of the resource to get") @QueryParam("type") String type,
final @Parameter(description = "Name of the resource to get.") @PathParam("name")
String name,
final @Parameter(description = "Type of the resource to get", required = true) @NotNull @QueryParam("type")
String type,
final @Parameter(description = "Namespace of the cluster where the resource is running.")
@QueryParam("namespace") String namespace) {
CustomResource cr = clusterService.get(namespace, name, type);
Expand Down Expand Up @@ -192,8 +195,10 @@ public String resource(
@Operation(summary = "Stop/Remove",
description = "Remove the resource identified by name.")
public boolean stop(
final @Parameter(description = "Name of the resource to get.") @PathParam("name") String name,
final @Parameter(description = "Type of the resource to get") @QueryParam("type") String type,
final @Parameter(description = "Name of the resource to get.") @PathParam("name")
String name,
final @Parameter(description = "Type of the resource to get", required = true) @NotNull @QueryParam("type")
String type,
final @Parameter(description = "Namespace of the cluster where the resource is running.")
@QueryParam("namespace") String namespace) {
return clusterService.stop(name, namespace, type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.kaoto.backend.api.resource.support;

import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.jboss.logging.Logger;

import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;

import static io.quarkus.bootstrap.util.IoUtils.readFile;

/**
* Functions that help native testing to use the same functionality as JVM testing does with injection
*/
public class NativeHelper {

private static final Logger log = Logger.getLogger(NativeHelper.class);


public static void waitForWarmUpCatalog(String catalogClassName) {
// when quarkus run native image before native tests, they set path to log to api/target/quarkus.log
// and it cannot be overridden
Path quarkusNativeLog = Paths.get("target", "quarkus.log");
log.info("Using Quarkus log file from " + quarkusNativeLog);
Awaitility.await()
.timeout(Duration.ofSeconds(20))
.pollDelay(Durations.ONE_SECOND)
.dontCatchUncaughtExceptions()
.until(() -> {
try {
return readFile(quarkusNativeLog).contains(
String.format("Catalog class %s_Subclass warmed up in", catalogClassName));
} catch (NoSuchFileException e) {
throw new IllegalStateException("The quarkus.log file doesn't exist in the api/target" +
" folder! This can happen when integration tests are executing against" +
" a running application. Usage NativeHelper in remote native testing mode" +
" is not implemented yet!");
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.kaoto.backend.api.resource.v1;

import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.resource.support.NativeHelper;
import io.quarkus.test.junit.QuarkusIntegrationTest;


@QuarkusIntegrationTest
public class DeploymentsResourceIT extends DeploymentsResourceTestAbstract {

@Override
protected void waitForWarmUpCatalog() {
NativeHelper.waitForWarmUpCatalog(StepCatalog.class.getCanonicalName());
}
}
Original file line number Diff line number Diff line change
@@ -1,110 +1,23 @@
package io.kaoto.backend.api.resource.v1;

import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;


@QuarkusTest
@WithKubernetesTestServer
@TestHTTPEndpoint(DeploymentsResource.class)
class DeploymentsResourceTest {
class DeploymentsResourceTest extends DeploymentsResourceTestAbstract {

private StepCatalog catalog;

@Test
void test() throws URISyntaxException, IOException {

catalog.waitForWarmUp().join();

var res = given()
.when()
.contentType("application/json")
.get()
.then()
.statusCode(Response.Status.OK.getStatusCode());
assertEquals("[]", res.extract().response().asString());


String yamlBinding = Files.readString(Path.of(
DeploymentsResourceTest.class.getResource(
"../twitter-search-source-binding.yaml")
.toURI()));
final var name = "integration-4";

given()
.when()
.get("/{name}", name)
.then()
.statusCode(Response.Status.NOT_FOUND.getStatusCode());

given()
.when()
.contentType("application/json")
.delete("/{name}", name)
.then()
.statusCode(Response.Status.NOT_FOUND.getStatusCode());

given()
.when().body(yamlBinding)
.contentType("text/yaml")
.post("/{name}", "Updated integration")
.then()
.statusCode(Response.Status.OK.getStatusCode());

res = given()
.when()
.contentType("application/json")
.get()
.then()
.statusCode(Response.Status.OK.getStatusCode());
assertNotEquals("[]", res.extract().response().asString());

res = given()
.when()
.get("/{name}", name)
.then()
.statusCode(Response.Status.OK.getStatusCode());
var yaml = res.extract().response().asString();
assertNotNull(yaml);
assertTrue(yaml.contains("kind: KameletBinding"));
assertTrue(yaml.contains("name: twitter-search-source"));
assertTrue(yaml.contains("name: aws-translate-action"));

res = given()
.when()
.contentType("application/json")
.delete("/{name}", name)
.then()
.statusCode(Response.Status.OK.getStatusCode());
assertTrue(Boolean.valueOf(res.extract().response().asString()));

res = given()
.when()
.contentType("application/json")
.get()
.then()
.statusCode(Response.Status.OK.getStatusCode());
assertEquals("[]", res.extract().response().asString());
}

@Inject
public void setCatalog(final StepCatalog catalog) {
this.catalog = catalog;
}

@Override
protected void waitForWarmUpCatalog() {
catalog.waitForWarmUp().join();
}
}
Loading