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

test: Extend and refactor StepResourceTest #812

Merged
merged 2 commits into from
Aug 3, 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
3 changes: 3 additions & 0 deletions api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ crd:
default: "KameletBinding"

quarkus:
# explicitly set prod profile for native testing (on GitHub workflows, it used dev)
test:
native-image-profile: prod
index-dependency:
camelkcrd:
group-id: org.apache.camel.k
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,14 @@
package io.kaoto.backend.api.resource.v1;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.kaoto.backend.camel.KamelHelper;
import io.kaoto.backend.model.step.Step;
import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.resource.support.NativeHelper;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@QuarkusIntegrationTest
class StepResourceIT {
@Test
void checkCatalogLoaded() throws Exception {
ValidatableResponse response = RestAssured.given().get("/v1/steps").then().statusCode(200);

String json = response.extract().body().asString();
Step[] steps = KamelHelper.JSON_MAPPER.readValue(json, Step[].class);
assertThat(steps).isNotEmpty();
Condition<Step> weHaveCamelConnectors =
new Condition<>(value -> value.getKind().equalsIgnoreCase("Camel-Connector"),
"We have camel connectors");
Condition<Step> weHaveKamelets =
new Condition<>(value -> value.getKind().equalsIgnoreCase("Kamelet"),
"We have kamelets.");
Condition<Step> weHaveEIPs =
new Condition<>(value -> value.getKind().equalsIgnoreCase("EIP"),
"We have EIPs.");
assertThat(steps)
.haveAtLeast(10, weHaveCamelConnectors)
.haveAtLeast(10, weHaveKamelets)
.haveAtLeast(10, weHaveEIPs);
}

@Test
void checkAllRequiredPropertiesInitialized() throws Exception {
ValidatableResponse response = RestAssured.given().get("/v1/steps").then().statusCode(200);

String json = response.extract().body().asString();
Step[] steps = KamelHelper.JSON_MAPPER.readValue(json, Step[].class);
class StepResourceIT extends StepResourceTestAbstract {

assertThat(steps)
.allMatch(step -> step.getRequired() != null);
@Override
protected void waitForWarmUpCatalog() {
NativeHelper.waitForWarmUpCatalog(StepCatalog.class.getCanonicalName());
}
}
134 changes: 21 additions & 113 deletions api/src/test/java/io/kaoto/backend/api/resource/v1/StepResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,43 @@

import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.model.step.Step;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import jakarta.inject.Inject;
import java.util.Collection;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
@TestHTTPEndpoint(StepResource.class)
class StepResourceTest {
class StepResourceTest extends StepResourceTestAbstract {

public static final String INFINISPAN_SOURCE = "infinispan-source";
private StepResource stepResource;
private StepCatalog catalog;

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

@Inject
public void setStepResource(final StepResource stepResource) {
this.stepResource = stepResource;
}

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

@Test
void stepById() {
Step s = stepResource.stepById(INFINISPAN_SOURCE + "-START");
Assertions.assertNotNull(s);
Assertions.assertEquals(s.getName() + "-" + s.getType(), s.getId());
}

@Test
void stepsByName() {
Collection<Step> steps = stepResource.stepsByName(INFINISPAN_SOURCE);
for (Step s : steps) {
Assertions.assertNotNull(s);
Assertions.assertEquals(INFINISPAN_SOURCE, s.getName());
Assertions.assertEquals(s.getName() + "-" + s.getType(), s.getId());
}
}

@Test
@Timeout(100)
void speedKamelet() {
stepResource.all("KameletBinding", null, null, null, null, null, null);
}

@Test
@Timeout(100)
void speedKameletAndBinding() {
stepResource.all("KameletBinding,Kamelet", null, null, null, null, null, null);
}
@Test
@Timeout(100)
void speedKameletAndBindingEnd() {
stepResource.all("KameletBinding,Kamelet", Step.END, null, null, null, null, null);
}
@Test
@Timeout(100)
void speedKameletBindingMiddle() {
stepResource.all("KameletBinding", Step.MIDDLE, null, null, null, null, null);
}

@ParameterizedTest
@ValueSource(longs = {10l, 25l, 42l})
@Timeout(100)
void limitAndStart(long limitParameter) {
Assertions.assertTrue(stepResource.all(null, null, null, null, null, null, null).size() > limitParameter);
List<Step> limit = stepResource.all(null, null, null, limitParameter, null, null, null).stream().toList();
Assertions.assertEquals(limitParameter, limit.size());

Long[] startParameters = new Long[]{0l, 3l, 5l};

for (Long startParameter : startParameters) {
List<Step> start =
stepResource.all(null, null, null, limitParameter, startParameter, null, null).stream().toList();
Assertions.assertEquals(limitParameter, start.size());

for (int i = 0; i < startParameter; i++) {
Assertions.assertEquals(limit.get((int) (startParameter + i)), start.get(i));
}
}
}

@Test
void allSteps() {
Collection<Step> steps = stepResource.all(null, null, null, null, null, null, null);
Assertions.assertNotNull(steps);
Assertions.assertEquals(
catalog.getReadOnlyCatalog().getAll().size(),
steps.size());

var integrationType = "KameletBinding";
Assertions.assertNotNull(steps);
Assertions.assertTrue(
stepResource.all(null, integrationType, null, null, null, null, null)
.stream().allMatch(s ->
integrationType.equalsIgnoreCase(s.getType())));

var kind = "Kamelet";
Assertions.assertNotNull(steps);
Assertions.assertTrue(
stepResource.all(null, null, kind, null, null, null, null)
.stream().allMatch(s ->
kind.equalsIgnoreCase(s.getKind())));
var type = Step.START;
Assertions.assertNotNull(steps);
Assertions.assertTrue(
stepResource.all(type, null, null, null, null, null, null)
.stream().allMatch(s ->
type.equalsIgnoreCase(s.getType())));

Assertions.assertNotNull(steps);
Assertions.assertTrue(
stepResource.all(type, integrationType, kind, null, null, null, null)
.stream().allMatch(s ->
type.equalsIgnoreCase(s.getType())
&& kind.equalsIgnoreCase(s.getKind())));
void allStepsFromBackend() {
Step[] steps = given()
.when()
.get()
.then()
.statusCode(Response.Status.OK.getStatusCode())
.extract().body().as(Step[].class);

assertThat(steps).isNotNull().isNotEmpty()
.as("Check that API returns all steps from backend")
.hasSize(catalog.getReadOnlyCatalog().getAll().size());
}
}
Loading