Skip to content
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
1 change: 1 addition & 0 deletions temporal-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-reflect"

implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8"
implementation ("com.fasterxml.jackson.module:jackson-module-kotlin") {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}
Expand Down
1 change: 1 addition & 0 deletions temporal-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
}
api "com.fasterxml.jackson.core:jackson-databind"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8"
if (!JavaVersion.current().isJava8()) {
implementation 'javax.annotation:javax.annotation-api:1.3.2'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.protobuf.ByteString;
import io.temporal.api.common.v1.Payload;
Expand All @@ -41,6 +42,7 @@ public JacksonJsonPayloadConverter() {
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.registerModule(new JavaTimeModule());
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.registerModule(new Jdk8Module());
}

public JacksonJsonPayloadConverter(ObjectMapper mapper) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 io.temporal.common.converter;

import static org.junit.Assert.assertEquals;

import io.temporal.api.common.v1.Payloads;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import org.junit.Test;

public class JacksonJsonPayloadConverterTest {
@Test
public void testJson() {
DataConverter converter = DefaultDataConverter.newDefaultInstance();
ProtoPayloadConverterTest.TestPayload payload =
new ProtoPayloadConverterTest.TestPayload(1L, Instant.now(), "myPayload");
Optional<Payloads> data = converter.toPayloads(payload);
ProtoPayloadConverterTest.TestPayload converted =
converter.fromPayloads(
0,
data,
ProtoPayloadConverterTest.TestPayload.class,
ProtoPayloadConverterTest.TestPayload.class);
assertEquals(payload, converted);
}

@Test
public void testJsonWithOptional() {
DataConverter converter = DefaultDataConverter.newDefaultInstance();
TestOptionalPayload payload =
new TestOptionalPayload(
Optional.of(1L), Optional.of(Instant.now()), Optional.of("myPayload"));
Optional<Payloads> data = converter.toPayloads(payload);
TestOptionalPayload converted =
converter.fromPayloads(0, data, TestOptionalPayload.class, TestOptionalPayload.class);
assertEquals(payload, converted);

assertEquals(Long.valueOf(1L), converted.getId().get());
assertEquals("myPayload", converted.getName().get());
}

static class TestOptionalPayload {
private Optional<Long> id;
private Optional<Instant> timestamp;
private Optional<String> name;

public TestOptionalPayload() {}

TestOptionalPayload(Optional<Long> id, Optional<Instant> timestamp, Optional<String> name) {
this.id = id;
this.timestamp = timestamp;
this.name = name;
}

public Optional<Long> getId() {
return id;
}

public void setId(Optional<Long> id) {
this.id = id;
}

public Optional<Instant> getTimestamp() {
return timestamp;
}

public void setTimestamp(Optional<Instant> timestamp) {
this.timestamp = timestamp;
}

public Optional<String> getName() {
return name;
}

public void setName(Optional<String> name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestOptionalPayload that = (TestOptionalPayload) o;
return getId().get().equals(that.getId().get())
&& Objects.equals(getTimestamp().get(), that.getTimestamp().get())
&& Objects.equals(getName().get(), that.getName().get());
}

@Override
public int hashCode() {
return Objects.hash(id, timestamp, name);
}

@Override
public String toString() {
return "TestPayload{"
+ "id="
+ id
+ ", timestamp="
+ timestamp
+ ", name='"
+ name
+ '\''
+ '}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.protobuf.ByteString;
import com.google.protobuf.MapEntry;
import com.google.protobuf.util.JsonFormat;
Expand Down Expand Up @@ -54,21 +51,6 @@ public void testProtoJson() {
assertEquals(execution, converted);
}

@Test
public void testCustomJson() {
ObjectMapper objectMapper =
new ObjectMapper()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
.registerModule(new JavaTimeModule());
DataConverter converter =
DefaultDataConverter.newDefaultInstance()
.withPayloadConverterOverrides(new JacksonJsonPayloadConverter(objectMapper));
TestPayload payload = new TestPayload(1L, Instant.now(), "myPayload");
Optional<Payloads> data = converter.toPayloads(payload);
TestPayload converted = converter.fromPayloads(0, data, TestPayload.class, TestPayload.class);
assertEquals(payload, converted);
}

@Test
public void testProto() {
DataConverter converter = new DefaultDataConverter(new ProtobufPayloadConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 io.temporal.workflow;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.client.WorkflowStub;
import io.temporal.testing.internal.SDKTestWorkflowRule;
import java.time.Duration;
import java.util.Optional;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

public class WorkflowOptionalTest {

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(CustomerWorkflowWaitForSignalImpl.class, CustomerWorkflowImpl.class)
.build();

@Test
public void testOptionalArgumentsWorkflowTypedStub() {
Optional<Customer> customer =
Optional.of(new Customer("john", "smith", 33, Optional.of("555-55-5555")));
WorkflowClient client = testWorkflowRule.getWorkflowClient();

CustomerWorkflow workflow =
client.newWorkflowStub(
CustomerWorkflow.class,
WorkflowOptions.newBuilder()
.setTaskQueue(testWorkflowRule.getTaskQueue())
.validateBuildWithDefaults());

Optional<Customer> result = workflow.execute(customer);
Assert.assertEquals(customer.get().getFirstName(), result.get().getFirstName());

// query after completion
Optional<Customer> queryResult = workflow.getCustomer();
assertTrue(queryResult.isPresent());
assertEquals(queryResult.get().getFirstName(), customer.get().getFirstName());
}

@Test
public void testOptionalArgumentsWorkflowUntypedStub() throws InterruptedException {
CustomerWorkflowWaitForSignal workflow =
testWorkflowRule.newWorkflowStubTimeoutOptions(CustomerWorkflowWaitForSignal.class);
Optional<Customer> customer1 =
Optional.of(new Customer("john", "smith", 33, Optional.of("555-55-5555")));
Optional<Customer> customer2 =
Optional.of(new Customer("merry", "smith", 29, Optional.of("111-11-1111")));
WorkflowClient.start(workflow::execute, customer1);
testWorkflowRule.sleep(Duration.ofSeconds(1));

Optional<Customer> result = workflow.getCustomer();
assertTrue(result.isPresent());
assertEquals(result.get().getFirstName(), customer1.get().getFirstName());
if (testWorkflowRule.isUseExternalService()) {
// Sleep a little bit to avoid server throttling error.
Thread.sleep(50);
}

// send query to update customer
workflow.setCustomer(customer2);

Customer finalResult = WorkflowStub.fromTyped(workflow).getResult(Customer.class);
assertEquals(finalResult.getFirstName(), customer2.get().getFirstName());
}

public static class CustomerWorkflowImpl implements CustomerWorkflow {
private Optional<Customer> customer;

@Override
public Optional<Customer> execute(Optional<Customer> customer) {
this.customer = customer;
return this.customer;
}

@Override
public Optional<Customer> getCustomer() {
return this.customer;
}
}

public static class CustomerWorkflowWaitForSignalImpl implements CustomerWorkflowWaitForSignal {
private Optional<Customer> customer;
CompletablePromise<Optional<Customer>> promise = Workflow.newPromise();

@Override
public Optional<Customer> execute(Optional<Customer> customer) {
this.customer = customer;

promise.get();

return this.customer;
}

@Override
public Optional<Customer> getCustomer() {
return customer;
}

@Override
public void setCustomer(Optional<Customer> customer) {
this.customer = customer;
promise.complete(null);
}
}

@WorkflowInterface
public interface CustomerWorkflow {

@WorkflowMethod
Optional<Customer> execute(Optional<Customer> customer);

@QueryMethod
Optional<Customer> getCustomer();
}

@WorkflowInterface
public interface CustomerWorkflowWaitForSignal {

@WorkflowMethod
Optional<Customer> execute(Optional<Customer> customer);

@QueryMethod
Optional<Customer> getCustomer();

@SignalMethod(name = "setCustomer")
void setCustomer(Optional<Customer> customer);
}

public static class Customer {
private String firstName;
private String lastName;
private int age;
private Optional<String> ssn;

public Customer() {}

public Customer(String firstName, String lastName, int age, Optional<String> ssn) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.ssn = ssn;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Optional<String> getSsn() {
return ssn;
}

public void setSsn(Optional<String> ssn) {
this.ssn = ssn;
}
}
}