Search in sources :

Example 1 with ActivatedJob

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob in project zeebe by camunda.

the class ActivateJobsTest method shouldMapRequestAndResponse.

@Test
public void shouldMapRequestAndResponse() {
    // given
    final ActivateJobsStub stub = new ActivateJobsStub();
    stub.registerWith(brokerClient);
    final String jobType = "testJob";
    final String worker = "testWorker";
    final int maxJobsToActivate = 13;
    final Duration timeout = Duration.ofMinutes(12);
    final List<String> fetchVariables = Arrays.asList("foo", "bar", "baz");
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(jobType).setWorker(worker).setMaxJobsToActivate(maxJobsToActivate).setTimeout(timeout.toMillis()).addAllFetchVariable(fetchVariables).build();
    stub.addAvailableJobs(jobType, maxJobsToActivate);
    // when
    final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
    // then
    assertThat(responses.hasNext()).isTrue();
    final ActivateJobsResponse response = responses.next();
    assertThat(response.getJobsCount()).isEqualTo(maxJobsToActivate);
    for (int i = 0; i < maxJobsToActivate; i++) {
        final ActivatedJob job = response.getJobs(i);
        assertThat(job.getKey()).isEqualTo(Protocol.encodePartitionId(Protocol.START_PARTITION_ID, i));
        assertThat(job.getType()).isEqualTo(jobType);
        assertThat(job.getWorker()).isEqualTo(worker);
        assertThat(job.getRetries()).isEqualTo(stub.getRetries());
        assertThat(job.getDeadline()).isEqualTo(stub.getDeadline());
        assertThat(job.getProcessInstanceKey()).isEqualTo(stub.getProcessInstanceKey());
        assertThat(job.getBpmnProcessId()).isEqualTo(stub.getBpmnProcessId());
        assertThat(job.getProcessDefinitionVersion()).isEqualTo(stub.getProcessDefinitionVersion());
        assertThat(job.getProcessDefinitionKey()).isEqualTo(stub.getProcessDefinitionKey());
        assertThat(job.getElementId()).isEqualTo(stub.getElementId());
        assertThat(job.getElementInstanceKey()).isEqualTo(stub.getElementInstanceKey());
        JsonUtil.assertEquality(job.getCustomHeaders(), stub.getCustomHeaders());
        JsonUtil.assertEquality(job.getVariables(), stub.getVariables());
    }
    final BrokerActivateJobsRequest brokerRequest = brokerClient.getSingleBrokerRequest();
    final JobBatchRecord brokerRequestValue = brokerRequest.getRequestWriter();
    assertThat(brokerRequestValue.getMaxJobsToActivate()).isEqualTo(maxJobsToActivate);
    assertThat(brokerRequestValue.getTypeBuffer()).isEqualTo(wrapString(jobType));
    assertThat(brokerRequestValue.getTimeout()).isEqualTo(timeout.toMillis());
    assertThat(brokerRequestValue.getWorkerBuffer()).isEqualTo(wrapString(worker));
    assertThat(brokerRequestValue.variables()).extracting(v -> BufferUtil.bufferAsString(v.getValue())).containsExactlyInAnyOrderElementsOf(fetchVariables);
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) Protocol(io.camunda.zeebe.protocol.Protocol) Arrays(java.util.Arrays) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Status(io.grpc.Status) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Parameterized(org.junit.runners.Parameterized) Intent(io.camunda.zeebe.protocol.record.intent.Intent) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Iterator(java.util.Iterator) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) JsonUtil(io.camunda.zeebe.test.util.JsonUtil) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) List(java.util.List) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) GatewayCfg(io.camunda.zeebe.gateway.impl.configuration.GatewayCfg) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) Duration(java.time.Duration) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Example 2 with ActivatedJob

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob in project zeebe by camunda.

the class ActivateJobsTest method shouldActivateJobsRoundRobin.

@Test
public void shouldActivateJobsRoundRobin() {
    // given
    final ActivateJobsStub stub = new ActivateJobsStub();
    stub.registerWith(brokerClient);
    final String type = "test";
    final int maxJobsToActivate = 2;
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(type).setMaxJobsToActivate(maxJobsToActivate).build();
    for (int partitionOffset = 0; partitionOffset < 3; partitionOffset++) {
        stub.addAvailableJobs(type, maxJobsToActivate);
        // when
        final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
        // then
        assertThat(responses.hasNext()).isTrue();
        final ActivateJobsResponse response = responses.next();
        for (final ActivatedJob activatedJob : response.getJobsList()) {
            assertThat(Protocol.decodePartitionId(activatedJob.getKey())).isEqualTo(Protocol.START_PARTITION_ID + partitionOffset);
        }
    }
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Example 3 with ActivatedJob

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob in project zeebe by camunda.

the class ActivateJobsTest method shouldActivateJobs.

@Test
public void shouldActivateJobs() {
    // given
    final ActivatedJob activatedJob1 = ActivatedJob.newBuilder().setKey(12).setType("foo").setProcessInstanceKey(123).setBpmnProcessId("test1").setProcessDefinitionVersion(2).setProcessDefinitionKey(23).setElementId("foo").setElementInstanceKey(23213).setCustomHeaders("{\"version\": \"1\"}").setWorker("worker1").setRetries(34).setDeadline(1231).setVariables("{\"key\": \"val\"}").build();
    final ActivatedJob activatedJob2 = ActivatedJob.newBuilder().setKey(42).setType("foo").setProcessInstanceKey(333).setBpmnProcessId("test3").setProcessDefinitionVersion(23).setProcessDefinitionKey(11).setElementId("bar").setElementInstanceKey(111).setCustomHeaders("{\"key\": \"value\"}").setWorker("worker1").setRetries(334).setDeadline(3131).setVariables("{\"bar\": 3}").build();
    gatewayService.onActivateJobsRequest(activatedJob1, activatedJob2);
    // when
    final ActivateJobsResponse response = client.newActivateJobsCommand().jobType("foo").maxJobsToActivate(3).timeout(Duration.ofMillis(1000)).workerName("worker1").send().join();
    // then
    assertThat(response.getJobs()).hasSize(2);
    io.camunda.zeebe.client.api.response.ActivatedJob job = response.getJobs().get(0);
    assertThat(job.getKey()).isEqualTo(activatedJob1.getKey());
    assertThat(job.getType()).isEqualTo(activatedJob1.getType());
    assertThat(job.getBpmnProcessId()).isEqualTo(activatedJob1.getBpmnProcessId());
    assertThat(job.getElementId()).isEqualTo(activatedJob1.getElementId());
    assertThat(job.getElementInstanceKey()).isEqualTo(activatedJob1.getElementInstanceKey());
    assertThat(job.getProcessDefinitionVersion()).isEqualTo(activatedJob1.getProcessDefinitionVersion());
    assertThat(job.getProcessDefinitionKey()).isEqualTo(activatedJob1.getProcessDefinitionKey());
    assertThat(job.getProcessInstanceKey()).isEqualTo(activatedJob1.getProcessInstanceKey());
    assertThat(job.getCustomHeaders()).isEqualTo(fromJsonAsMap(activatedJob1.getCustomHeaders()));
    assertThat(job.getWorker()).isEqualTo(activatedJob1.getWorker());
    assertThat(job.getRetries()).isEqualTo(activatedJob1.getRetries());
    assertThat(job.getDeadline()).isEqualTo(activatedJob1.getDeadline());
    assertThat(job.getVariables()).isEqualTo(activatedJob1.getVariables());
    job = response.getJobs().get(1);
    assertThat(job.getKey()).isEqualTo(activatedJob2.getKey());
    assertThat(job.getType()).isEqualTo(activatedJob2.getType());
    assertThat(job.getBpmnProcessId()).isEqualTo(activatedJob2.getBpmnProcessId());
    assertThat(job.getElementId()).isEqualTo(activatedJob2.getElementId());
    assertThat(job.getElementInstanceKey()).isEqualTo(activatedJob2.getElementInstanceKey());
    assertThat(job.getProcessDefinitionVersion()).isEqualTo(activatedJob2.getProcessDefinitionVersion());
    assertThat(job.getProcessDefinitionKey()).isEqualTo(activatedJob2.getProcessDefinitionKey());
    assertThat(job.getProcessInstanceKey()).isEqualTo(activatedJob2.getProcessInstanceKey());
    assertThat(job.getCustomHeaders()).isEqualTo(fromJsonAsMap(activatedJob2.getCustomHeaders()));
    assertThat(job.getWorker()).isEqualTo(activatedJob2.getWorker());
    assertThat(job.getRetries()).isEqualTo(activatedJob2.getRetries());
    assertThat(job.getDeadline()).isEqualTo(activatedJob2.getDeadline());
    assertThat(job.getVariables()).isEqualTo(activatedJob2.getVariables());
    final ActivateJobsRequest request = gatewayService.getLastRequest();
    assertThat(request.getType()).isEqualTo("foo");
    assertThat(request.getMaxJobsToActivate()).isEqualTo(3);
    assertThat(request.getTimeout()).isEqualTo(1000);
    assertThat(request.getWorker()).isEqualTo("worker1");
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.client.api.response.ActivateJobsResponse) ClientTest(io.camunda.zeebe.client.util.ClientTest) Test(org.junit.Test)

Example 4 with ActivatedJob

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob in project zeebe by camunda.

the class ResponseMapper method toActivateJobsResponse.

public static ActivateJobsResponse toActivateJobsResponse(final long key, final JobBatchRecord brokerResponse) {
    final ActivateJobsResponse.Builder responseBuilder = ActivateJobsResponse.newBuilder();
    final Iterator<LongValue> jobKeys = brokerResponse.jobKeys().iterator();
    final Iterator<JobRecord> jobs = brokerResponse.jobs().iterator();
    while (jobKeys.hasNext() && jobs.hasNext()) {
        final LongValue jobKey = jobKeys.next();
        final JobRecord job = jobs.next();
        final ActivatedJob activatedJob = ActivatedJob.newBuilder().setKey(jobKey.getValue()).setType(bufferAsString(job.getTypeBuffer())).setBpmnProcessId(job.getBpmnProcessId()).setElementId(job.getElementId()).setProcessInstanceKey(job.getProcessInstanceKey()).setProcessDefinitionVersion(job.getProcessDefinitionVersion()).setProcessDefinitionKey(job.getProcessDefinitionKey()).setElementInstanceKey(job.getElementInstanceKey()).setCustomHeaders(bufferAsJson(job.getCustomHeadersBuffer())).setWorker(bufferAsString(job.getWorkerBuffer())).setRetries(job.getRetries()).setDeadline(job.getDeadline()).setVariables(bufferAsJson(job.getVariablesBuffer())).build();
        responseBuilder.addJobs(activatedJob);
    }
    return responseBuilder.build();
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) LongValue(io.camunda.zeebe.msgpack.value.LongValue)

Example 5 with ActivatedJob

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob in project zeebe by zeebe-io.

the class ActivateJobsTest method shouldMapRequestAndResponse.

@Test
public void shouldMapRequestAndResponse() {
    // given
    final ActivateJobsStub stub = new ActivateJobsStub();
    stub.registerWith(brokerClient);
    final String jobType = "testJob";
    final String worker = "testWorker";
    final int maxJobsToActivate = 13;
    final Duration timeout = Duration.ofMinutes(12);
    final List<String> fetchVariables = Arrays.asList("foo", "bar", "baz");
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(jobType).setWorker(worker).setMaxJobsToActivate(maxJobsToActivate).setTimeout(timeout.toMillis()).addAllFetchVariable(fetchVariables).build();
    stub.addAvailableJobs(jobType, maxJobsToActivate);
    // when
    final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
    // then
    assertThat(responses.hasNext()).isTrue();
    final ActivateJobsResponse response = responses.next();
    assertThat(response.getJobsCount()).isEqualTo(maxJobsToActivate);
    for (int i = 0; i < maxJobsToActivate; i++) {
        final ActivatedJob job = response.getJobs(i);
        assertThat(job.getKey()).isEqualTo(Protocol.encodePartitionId(Protocol.START_PARTITION_ID, i));
        assertThat(job.getType()).isEqualTo(jobType);
        assertThat(job.getWorker()).isEqualTo(worker);
        assertThat(job.getRetries()).isEqualTo(stub.getRetries());
        assertThat(job.getDeadline()).isEqualTo(stub.getDeadline());
        assertThat(job.getProcessInstanceKey()).isEqualTo(stub.getProcessInstanceKey());
        assertThat(job.getBpmnProcessId()).isEqualTo(stub.getBpmnProcessId());
        assertThat(job.getProcessDefinitionVersion()).isEqualTo(stub.getProcessDefinitionVersion());
        assertThat(job.getProcessDefinitionKey()).isEqualTo(stub.getProcessDefinitionKey());
        assertThat(job.getElementId()).isEqualTo(stub.getElementId());
        assertThat(job.getElementInstanceKey()).isEqualTo(stub.getElementInstanceKey());
        JsonUtil.assertEquality(job.getCustomHeaders(), stub.getCustomHeaders());
        JsonUtil.assertEquality(job.getVariables(), stub.getVariables());
    }
    final BrokerActivateJobsRequest brokerRequest = brokerClient.getSingleBrokerRequest();
    final JobBatchRecord brokerRequestValue = brokerRequest.getRequestWriter();
    assertThat(brokerRequestValue.getMaxJobsToActivate()).isEqualTo(maxJobsToActivate);
    assertThat(brokerRequestValue.getTypeBuffer()).isEqualTo(wrapString(jobType));
    assertThat(brokerRequestValue.getTimeout()).isEqualTo(timeout.toMillis());
    assertThat(brokerRequestValue.getWorkerBuffer()).isEqualTo(wrapString(worker));
    assertThat(brokerRequestValue.variables()).extracting(v -> BufferUtil.bufferAsString(v.getValue())).containsExactlyInAnyOrderElementsOf(fetchVariables);
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) Protocol(io.camunda.zeebe.protocol.Protocol) Arrays(java.util.Arrays) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Status(io.grpc.Status) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Parameterized(org.junit.runners.Parameterized) Intent(io.camunda.zeebe.protocol.record.intent.Intent) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Iterator(java.util.Iterator) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) JsonUtil(io.camunda.zeebe.test.util.JsonUtil) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) List(java.util.List) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) GatewayCfg(io.camunda.zeebe.gateway.impl.configuration.GatewayCfg) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) Duration(java.time.Duration) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Aggregations

ActivatedJob (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob)12 ActivateJobsRequest (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest)9 ActivateJobsResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse)9 Test (org.junit.Test)9 GatewayTest (io.camunda.zeebe.gateway.api.util.GatewayTest)6 BrokerActivateJobsRequest (io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest)6 BufferUtil.wrapString (io.camunda.zeebe.util.buffer.BufferUtil.wrapString)6 ActivateJobsResponse (io.camunda.zeebe.client.api.response.ActivateJobsResponse)3 ClientTest (io.camunda.zeebe.client.util.ClientTest)3 RequestHandler (io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler)3 BrokerRequest (io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest)3 BrokerRejection (io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection)3 BrokerRejectionResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse)3 BrokerResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse)3 GatewayCfg (io.camunda.zeebe.gateway.impl.configuration.GatewayCfg)3 LongValue (io.camunda.zeebe.msgpack.value.LongValue)3 Protocol (io.camunda.zeebe.protocol.Protocol)3 JobBatchRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)3 JobRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)3 RejectionType (io.camunda.zeebe.protocol.record.RejectionType)3