Skip to content

Commit

Permalink
Provide native tests for Integrations resource
Browse files Browse the repository at this point in the history
- it allows to have a non-regression test for KaotoIO#617 issue with
expressions
- extracted a single test from IntegrationsResourceTest which cannot be
launch in Native mode because it is using @Inject to access internal
services directly. it allows to inherit the native test class from
IntegrationsResourceTest and play all of the existing tests in native

part of KaotoIO#617
part of KaotoIO#437

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Apr 7, 2023
1 parent a5bdc81 commit 1d601ef
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.kaoto.backend.api.resource.v1;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
class IntegrationsResourceIT extends IntegrationsResourceTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,140 +6,31 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.inject.Inject;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import io.kaoto.backend.model.deployment.kamelet.expression.Expression;
import io.kaoto.backend.model.step.Step;
import org.junit.jupiter.api.BeforeEach;
import javax.ws.rs.core.Response;

import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.resource.v1.model.Integration;
import io.kaoto.backend.api.service.deployment.generator.kamelet.KameletRepresenter;
import io.kaoto.backend.api.service.language.LanguageService;
import io.kaoto.backend.model.deployment.kamelet.KameletBinding;
import io.kaoto.backend.model.step.Step;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@TestHTTPEndpoint(IntegrationsResource.class)
class IntegrationsResourceTest {

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

@Inject
public void setLanguageService(final LanguageService languageService) {
this.languageService = languageService;
}

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

@Test
void thereAndBackAgain() throws URISyntaxException, IOException {

final var alldsl = languageService.getAll();
ObjectMapper mapper = new ObjectMapper();

String yaml1 = Files.readString(Path.of(
DeploymentsResourceTest.class.getResource(
"../twitter-search-source-binding.yaml")
.toURI()));

var res = given()
.when()
.contentType("application/json")
.body(Collections.emptyList())
.post("/dsls")
.then()
.statusCode(Response.Status.OK.getStatusCode());
final var dsllist = mapper.readValue(res.extract().body().asString(), List.class);
assertEquals(alldsl.size(), dsllist.size());
assertTrue(alldsl.stream().allMatch(l -> dsllist.contains(l.get("name"))));

//It will return a valid value even if we are useless users with the DSL
given()
.when()
.contentType("text/yaml")
.body(yaml1)
.post("?dsl=SomethingWrong")
.then()
.statusCode(Response.Status.OK.getStatusCode());

res = given()
.when()
.contentType("text/yaml")
.body(yaml1)
.post("?dsl=KameletBinding")
.then()
.statusCode(Response.Status.OK.getStatusCode());

String json = res.extract().body().asString();
Integration integration = mapper.readValue(json, Integration.class);

res = given()
.when()
.contentType("application/json")
.body(Collections.emptyList())
.post("/dsls")
.then()
.statusCode(Response.Status.OK.getStatusCode());
final var dsllist2 = mapper.readValue(res.extract().body().asString(), List.class);
assertEquals(alldsl.size(), dsllist2.size());
assertTrue(alldsl.stream().allMatch(l -> dsllist2.contains(l.get("name"))));

res = given()
.when()
.contentType("application/json")
.body(integration.getSteps())
.post("/dsls")
.then()
.statusCode(Response.Status.OK.getStatusCode());
var dsls = mapper.readValue(res.extract().body().asString(), List.class);
assertEquals(1, dsls.size());
assertTrue(dsls.contains("KameletBinding"));

res = given()
.when()
.contentType("application/json")
.body(json)
.post("?dsl=KameletBinding")
.then()
.statusCode(Response.Status.OK.getStatusCode());

String yaml2 = res.extract().body().asString();

Yaml yaml = new Yaml(
new Constructor(KameletBinding.class),
new KameletRepresenter());
KameletBinding res1 = yaml.load(yaml1);
KameletBinding res2 = yaml.load(yaml2);

assertEquals(res1, res2);

}


@Test
void complexEIP() throws URISyntaxException, IOException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package io.kaoto.backend.api.resource.v1;

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

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.core.Response;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.resource.v1.model.Integration;
import io.kaoto.backend.api.service.deployment.generator.kamelet.KameletRepresenter;
import io.kaoto.backend.api.service.language.LanguageService;
import io.kaoto.backend.model.deployment.kamelet.KameletBinding;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@TestHTTPEndpoint(IntegrationsResource.class)
class IntegrationsResourceWithServiceAccessTest {

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

@Inject
public void setLanguageService(final LanguageService languageService) {
this.languageService = languageService;
}

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

@Test
void thereAndBackAgain() throws URISyntaxException, IOException {

final var alldsl = languageService.getAll();
ObjectMapper mapper = new ObjectMapper();

String yaml1 = Files.readString(Path.of(
DeploymentsResourceTest.class.getResource(
"../twitter-search-source-binding.yaml")
.toURI()));

var res = given()
.when()
.contentType("application/json")
.body(Collections.emptyList())
.post("/dsls")
.then()
.statusCode(Response.Status.OK.getStatusCode());
final var dsllist = mapper.readValue(res.extract().body().asString(), List.class);
assertEquals(alldsl.size(), dsllist.size());
assertTrue(alldsl.stream().allMatch(l -> dsllist.contains(l.get("name"))));

//It will return a valid value even if we are useless users with the DSL
given()
.when()
.contentType("text/yaml")
.body(yaml1)
.post("?dsl=SomethingWrong")
.then()
.statusCode(Response.Status.OK.getStatusCode());

res = given()
.when()
.contentType("text/yaml")
.body(yaml1)
.post("?dsl=KameletBinding")
.then()
.statusCode(Response.Status.OK.getStatusCode());

String json = res.extract().body().asString();
Integration integration = mapper.readValue(json, Integration.class);

res = given()
.when()
.contentType("application/json")
.body(Collections.emptyList())
.post("/dsls")
.then()
.statusCode(Response.Status.OK.getStatusCode());
final var dsllist2 = mapper.readValue(res.extract().body().asString(), List.class);
assertEquals(alldsl.size(), dsllist2.size());
assertTrue(alldsl.stream().allMatch(l -> dsllist2.contains(l.get("name"))));

res = given()
.when()
.contentType("application/json")
.body(integration.getSteps())
.post("/dsls")
.then()
.statusCode(Response.Status.OK.getStatusCode());
var dsls = mapper.readValue(res.extract().body().asString(), List.class);
assertEquals(1, dsls.size());
assertTrue(dsls.contains("KameletBinding"));

res = given()
.when()
.contentType("application/json")
.body(json)
.post("?dsl=KameletBinding")
.then()
.statusCode(Response.Status.OK.getStatusCode());

String yaml2 = res.extract().body().asString();

Yaml yaml = new Yaml(
new Constructor(KameletBinding.class),
new KameletRepresenter());
KameletBinding res1 = yaml.load(yaml1);
KameletBinding res2 = yaml.load(yaml2);

assertEquals(res1, res2);

}

}

0 comments on commit 1d601ef

Please sign in to comment.