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

Commit

Permalink
fix/test: null check for a type param in Deploment + test expansion
Browse files Browse the repository at this point in the history
* Add more tests for Deployments resources + native tests
  • Loading branch information
mkralik3 committed Jun 7, 2023
1 parent e260154 commit 89435bd
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 97 deletions.
5 changes: 5 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,15 @@ 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) @QueryParam("type")
String type,
final @Parameter(description = "Namespace of the cluster where the resource is running.")
@QueryParam("namespace") String namespace) {
if (type == null) {
throw new IllegalStateException("query parameter 'type' is required.");
}
CustomResource cr = clusterService.get(namespace, name, type);
if (cr == null) {
throw new NotFoundException("Resource with name " + name + " not "
Expand Down Expand Up @@ -192,10 +197,15 @@ 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) @QueryParam("type")
String type,
final @Parameter(description = "Namespace of the cluster where the resource is running.")
@QueryParam("namespace") String namespace) {
if (type == null) {
throw new IllegalStateException("query parameter 'type' is required.");
}
return clusterService.stop(name, namespace, type);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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! " +
"This can happen when integration tests are executing against a running application. " +
"Usage NativeHelper in remote native testing 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 DeploymentsResourcesTestAbstract {

@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 DeploymentsResourcesTestAbstract {

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

0 comments on commit 89435bd

Please sign in to comment.