gmunozfe commented on code in PR #2216: URL: https://github.com/apache/incubator-kie-kogito-apps/pull/2216#discussion_r2068561539
########## apps-integration-tests/integration-tests-jobs-service/integration-tests-jobs-service-quarkus/integration-tests-jobs-service-quarkus-knative-eventing/src/test/java/org/kie/kogito/it/jobs/JobRecipientMock.java: ########## @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.kogito.it.jobs; + +import java.util.Collections; +import java.util.Map; + +import org.kie.kogito.test.utils.SocketUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.Testcontainers; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import com.github.tomakehurst.wiremock.matching.RegexPattern; +import com.github.tomakehurst.wiremock.matching.UrlPattern; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +import static com.github.tomakehurst.wiremock.client.WireMock.request; + +/** + * Mock an external JobRecipient to verify the proper execution of jobs service api over http. + */ +public class JobRecipientMock implements QuarkusTestResourceLifecycleManager { + + private static final Logger LOGGER = LoggerFactory.getLogger(JobRecipientMock.class); + private WireMockServer wireMockServer; + public static final String JOB_RECIPIENT_MOCK_URL_PROPERTY = "kogito.job-recipient-mock.url"; + public static final String JOB_RECIPIENT_MOCK = "job-recipient-mock"; + public static final String JOB_RECIPIENT_MOCK_REGEX = "\\/" + JOB_RECIPIENT_MOCK + "\\?limit\\=(\\d+)$"; + + public interface JobRecipientMockAware { + void setWireMockServer(WireMockServer jobRecipient); + } + + @Override + public Map<String, String> start() { + LOGGER.info("Start JobRecipientMock test resource"); + int httpPort = SocketUtils.findAvailablePort(); + Testcontainers.exposeHostPorts(httpPort); + wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(httpPort)); + wireMockServer.start(); + + wireMockServer.stubFor(request("POST", new UrlPattern(new RegexPattern(JOB_RECIPIENT_MOCK_REGEX), true))); + + LOGGER.info("JobRecipientMock test resource started"); + return Collections.singletonMap(JOB_RECIPIENT_MOCK_URL_PROPERTY, String.format("http://host.testcontainers.internal:%s", wireMockServer.port())); Review Comment: you can define this as a constant, but not important, up to you ########## apps-integration-tests/integration-tests-jobs-service/integration-tests-jobs-service-quarkus/integration-tests-jobs-service-quarkus-knative-eventing/src/test/java/org/kie/kogito/it/jobs/HttpJobExecutionIT.java: ########## @@ -0,0 +1,297 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.kogito.it.jobs; + +import java.time.OffsetDateTime; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +import org.junit.jupiter.api.Test; +import org.kie.kogito.jobs.service.api.event.CreateJobEvent; +import org.kie.kogito.jobs.service.api.event.DeleteJobEvent; +import org.kie.kogito.test.resources.JobServiceTestResource; +import org.kie.kogito.testcontainers.quarkus.KafkaQuarkusTestResource; +import org.testcontainers.shaded.org.awaitility.Awaitility; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.tomakehurst.wiremock.WireMockServer; + +import io.cloudevents.SpecVersion; +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusIntegrationTest; +import io.restassured.RestAssured; +import io.restassured.http.ContentType; + +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.awaitility.Awaitility.await; +import static org.kie.kogito.it.jobs.JobRecipientMock.JOB_RECIPIENT_MOCK; +import static org.kie.kogito.it.jobs.JobRecipientMock.JOB_RECIPIENT_MOCK_URL_PROPERTY; +import static org.kie.kogito.test.resources.JobServiceCompositeQuarkusTestResource.JOBS_SERVICE_URL; + +@QuarkusIntegrationTest +@QuarkusTestResource(KafkaQuarkusTestResource.class) +@QuarkusTestResource(JobRecipientMock.class) +@JobServiceTestResource(knativeEventingEnabled = true) +public class HttpJobExecutionIT implements JobRecipientMock.JobRecipientMockAware { + + private static final String APPLICATION_CLOUD_EVENTS = "application/cloudevents+json"; + private static final String SPECVERSION = "specversion"; + private static final String ID = "id"; + private static final String SOURCE = "source"; + private static final String TYPE = "type"; + private static final String TIME = "time"; + private static final String CE_SPECVERSION_HEADER = "ce-specversion"; + private static final String CE_ID_HEADER = "ce-id"; + private static final String CE_SOURCE_HEADER = "ce-source"; + private static final String CE_TYPE_HEADER = "ce-type"; + private static final String CE_TIME_HEADER = "ce-time"; + + private WireMockServer jobRecipient; + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public void setWireMockServer(WireMockServer jobRecipient) { + this.jobRecipient = jobRecipient; + } + + @Test + void createAndExecuteJobWithBinaryModeEventsApi() { + String eventsUrl = jobServiceEventsUrl(); + String jobRecipientUrl = jobRecipientMockUrl() + "/" + JOB_RECIPIENT_MOCK; + + String jobId = UUID.randomUUID().toString(); + String startTimeStr = getNowPlusSeconds(5); + + ObjectNode job = createJob(jobId, startTimeStr, jobRecipientUrl); + String body = job.toPrettyString(); + + RestAssured.given() + .accept(APPLICATION_JSON) + .contentType(APPLICATION_JSON) + .header(CE_ID_HEADER, UUID.randomUUID().toString()) + .header(CE_SPECVERSION_HEADER, SpecVersion.V1.toString()) + .header(CE_TYPE_HEADER, CreateJobEvent.TYPE) + .header(CE_SOURCE_HEADER, "http://binary.test.com") Review Comment: As this is repeated 3 times, you can convert it into a constant for better readability ########## apps-integration-tests/integration-tests-jobs-service/integration-tests-jobs-service-quarkus/integration-tests-jobs-service-quarkus-knative-eventing/src/test/java/org/kie/kogito/it/jobs/HttpJobExecutionIT.java: ########## @@ -0,0 +1,297 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.kogito.it.jobs; + +import java.time.OffsetDateTime; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +import org.junit.jupiter.api.Test; +import org.kie.kogito.jobs.service.api.event.CreateJobEvent; +import org.kie.kogito.jobs.service.api.event.DeleteJobEvent; +import org.kie.kogito.test.resources.JobServiceTestResource; +import org.kie.kogito.testcontainers.quarkus.KafkaQuarkusTestResource; +import org.testcontainers.shaded.org.awaitility.Awaitility; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.tomakehurst.wiremock.WireMockServer; + +import io.cloudevents.SpecVersion; +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusIntegrationTest; +import io.restassured.RestAssured; +import io.restassured.http.ContentType; + +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.awaitility.Awaitility.await; +import static org.kie.kogito.it.jobs.JobRecipientMock.JOB_RECIPIENT_MOCK; +import static org.kie.kogito.it.jobs.JobRecipientMock.JOB_RECIPIENT_MOCK_URL_PROPERTY; +import static org.kie.kogito.test.resources.JobServiceCompositeQuarkusTestResource.JOBS_SERVICE_URL; + +@QuarkusIntegrationTest +@QuarkusTestResource(KafkaQuarkusTestResource.class) +@QuarkusTestResource(JobRecipientMock.class) +@JobServiceTestResource(knativeEventingEnabled = true) +public class HttpJobExecutionIT implements JobRecipientMock.JobRecipientMockAware { + + private static final String APPLICATION_CLOUD_EVENTS = "application/cloudevents+json"; + private static final String SPECVERSION = "specversion"; + private static final String ID = "id"; + private static final String SOURCE = "source"; + private static final String TYPE = "type"; + private static final String TIME = "time"; + private static final String CE_SPECVERSION_HEADER = "ce-specversion"; + private static final String CE_ID_HEADER = "ce-id"; + private static final String CE_SOURCE_HEADER = "ce-source"; + private static final String CE_TYPE_HEADER = "ce-type"; + private static final String CE_TIME_HEADER = "ce-time"; + + private WireMockServer jobRecipient; + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public void setWireMockServer(WireMockServer jobRecipient) { + this.jobRecipient = jobRecipient; + } + + @Test + void createAndExecuteJobWithBinaryModeEventsApi() { + String eventsUrl = jobServiceEventsUrl(); + String jobRecipientUrl = jobRecipientMockUrl() + "/" + JOB_RECIPIENT_MOCK; + + String jobId = UUID.randomUUID().toString(); + String startTimeStr = getNowPlusSeconds(5); + + ObjectNode job = createJob(jobId, startTimeStr, jobRecipientUrl); + String body = job.toPrettyString(); + + RestAssured.given() + .accept(APPLICATION_JSON) + .contentType(APPLICATION_JSON) + .header(CE_ID_HEADER, UUID.randomUUID().toString()) + .header(CE_SPECVERSION_HEADER, SpecVersion.V1.toString()) + .header(CE_TYPE_HEADER, CreateJobEvent.TYPE) + .header(CE_SOURCE_HEADER, "http://binary.test.com") + .header(CE_TIME_HEADER, getNowPlusSeconds(0)) + .body(body) + .post(eventsUrl) + .then() + .statusCode(202); + + verifyJobWasExecuted(jobId, 0); + assertJobExists(jobId, false, 1, 60); + } + + @Test + void deleteJobWithBinaryModeEventsApi() { + String eventsUrl = jobServiceEventsUrl(); + String jobRecipientUrl = jobRecipientMockUrl() + "/" + JOB_RECIPIENT_MOCK; + + String jobId = UUID.randomUUID().toString(); + String startTimeStr = getNowPlusSeconds(60 * 60); + + ObjectNode job = createJob(jobId, startTimeStr, jobRecipientUrl); + String body = job.toPrettyString(); + + RestAssured.given() + .accept(APPLICATION_JSON) + .contentType(APPLICATION_JSON) + .header(CE_ID_HEADER, UUID.randomUUID().toString()) + .header(CE_SPECVERSION_HEADER, SpecVersion.V1.toString()) + .header(CE_TYPE_HEADER, CreateJobEvent.TYPE) + .header(CE_SOURCE_HEADER, "http://binary.test.com") + .header(CE_TIME_HEADER, getNowPlusSeconds(0)) + .body(body) + .post(eventsUrl) + .then() + .statusCode(202); + + assertJobExists(jobId, true, 1, 60); + + ObjectNode delete = objectMapper.createObjectNode() + .put("id", jobId); + String deleteBody = delete.toPrettyString(); + + RestAssured.given() + .accept(APPLICATION_JSON) + .contentType(APPLICATION_JSON) + .header(CE_ID_HEADER, UUID.randomUUID().toString()) + .header(CE_SPECVERSION_HEADER, SpecVersion.V1.toString()) + .header(CE_TYPE_HEADER, DeleteJobEvent.TYPE) + .header(CE_SOURCE_HEADER, "http://binary.test.com") + .header(CE_TIME_HEADER, getNowPlusSeconds(0)) + .body(deleteBody) + .post(eventsUrl) + .then() + .statusCode(202); + + assertJobExists(jobId, false, 1, 60); + } + + @Test + void executeJobWithStructuredModeEventsApi() { + String eventsUrl = jobServiceEventsUrl(); + String jobRecipientUrl = jobRecipientMockUrl() + "/" + JOB_RECIPIENT_MOCK; + + String jobId = UUID.randomUUID().toString(); + String startTimeStr = getNowPlusSeconds(5); + + ObjectNode job = createJob(jobId, startTimeStr, jobRecipientUrl); + + ObjectNode cloudEvent = objectMapper.createObjectNode() + .put(ID, UUID.randomUUID().toString()) + .put(SPECVERSION, SpecVersion.V1.toString()) + .put(TYPE, CreateJobEvent.TYPE) + .put(SOURCE, "http://structured.test.com") Review Comment: Same here -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
