|
| 1 | +/** |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * <p>Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file |
| 7 | + * except in compliance with the License. A copy of the License is located at |
| 8 | + * |
| 9 | + * <p>http://aws.amazon.com/apache2.0 |
| 10 | + * |
| 11 | + * <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package com.uber.cadence.internal.compatibility.proto.mappers; |
| 16 | + |
| 17 | +import static com.uber.cadence.internal.compatibility.MapperTestUtil.assertNoMissingFields; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import com.google.common.collect.Sets; |
| 21 | +import com.uber.cadence.entities.Decision; |
| 22 | +import com.uber.cadence.entities.DecisionType; |
| 23 | +import com.uber.cadence.internal.compatibility.ClientObjects; |
| 24 | +import com.uber.cadence.internal.compatibility.ProtoObjects; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.EnumSet; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.stream.Collectors; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class DecisionMapperTest { |
| 34 | + private static final Map<Decision, com.uber.cadence.api.v1.Decision> DECISIONS = |
| 35 | + ImmutableMap.<Decision, com.uber.cadence.api.v1.Decision>builder() |
| 36 | + .put( |
| 37 | + ClientObjects.DECISION_SCHEDULE_ACTIVITY_TASK, |
| 38 | + ProtoObjects.DECISION_SCHEDULE_ACTIVITY_TASK) |
| 39 | + .put( |
| 40 | + ClientObjects.DECISION_REQUEST_CANCEL_ACTIVITY_TASK, |
| 41 | + ProtoObjects.DECISION_REQUEST_CANCEL_ACTIVITY_TASK) |
| 42 | + .put(ClientObjects.DECISION_START_TIMER, ProtoObjects.DECISION_START_TIMER) |
| 43 | + .put( |
| 44 | + ClientObjects.DECISION_COMPLETE_WORKFLOW_EXECUTION, |
| 45 | + ProtoObjects.DECISION_COMPLETE_WORKFLOW_EXECUTION) |
| 46 | + .put( |
| 47 | + ClientObjects.DECISION_FAIL_WORKFLOW_EXECUTION, |
| 48 | + ProtoObjects.DECISION_FAIL_WORKFLOW_EXECUTION) |
| 49 | + .put(ClientObjects.DECISION_CANCEL_TIMER, ProtoObjects.DECISION_CANCEL_TIMER) |
| 50 | + .put(ClientObjects.DECISION_CANCEL_WORKFLOW, ProtoObjects.DECISION_CANCEL_WORKFLOW) |
| 51 | + .put( |
| 52 | + ClientObjects.DECISION_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION, |
| 53 | + ProtoObjects.DECISION_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION) |
| 54 | + .put( |
| 55 | + ClientObjects.DECISION_CONTINUE_AS_NEW_WORKFLOW_EXECUTION, |
| 56 | + ProtoObjects.DECISION_CONTINUE_AS_NEW_WORKFLOW_EXECUTION) |
| 57 | + .put( |
| 58 | + ClientObjects.DECISION_START_CHILD_WORKFLOW_EXECUTION, |
| 59 | + ProtoObjects.DECISION_START_CHILD_WORKFLOW_EXECUTION) |
| 60 | + .put( |
| 61 | + ClientObjects.DECISION_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION, |
| 62 | + ProtoObjects.DECISION_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION) |
| 63 | + .put( |
| 64 | + ClientObjects.DECISION_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES, |
| 65 | + ProtoObjects.DECISION_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES) |
| 66 | + .put(ClientObjects.DECISION_RECORD_MARKER, ProtoObjects.DECISION_RECORD_MARKER) |
| 67 | + .build(); |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testMapDecision() { |
| 71 | + for (Map.Entry<Decision, com.uber.cadence.api.v1.Decision> entry : DECISIONS.entrySet()) { |
| 72 | + Assert.assertEquals( |
| 73 | + "Failed to convert decision of type: " + entry.getKey().getDecisionType(), |
| 74 | + entry.getValue(), |
| 75 | + DecisionMapper.decision(entry.getKey())); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testAllDecisionTypesCovered() { |
| 81 | + // If IDL changes add a new decision type, this should fail |
| 82 | + Set<DecisionType> expected = EnumSet.allOf(DecisionType.class); |
| 83 | + Set<DecisionType> actual = |
| 84 | + DECISIONS.keySet().stream().map(Decision::getDecisionType).collect(Collectors.toSet()); |
| 85 | + |
| 86 | + Assert.assertEquals( |
| 87 | + "Missing conversion for some DecisionTypes", |
| 88 | + Collections.emptySet(), |
| 89 | + Sets.difference(expected, actual)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testAllAttributesSet() { |
| 94 | + // If IDL changes add a new field to decision attributes, this should fail |
| 95 | + for (Map.Entry<Decision, com.uber.cadence.api.v1.Decision> entry : DECISIONS.entrySet()) { |
| 96 | + Decision decision = entry.getKey(); |
| 97 | + switch (decision.getDecisionType()) { |
| 98 | + case ScheduleActivityTask: |
| 99 | + assertNoMissingFields(decision.getScheduleActivityTaskDecisionAttributes()); |
| 100 | + break; |
| 101 | + case RequestCancelActivityTask: |
| 102 | + assertNoMissingFields(decision.getRequestCancelActivityTaskDecisionAttributes()); |
| 103 | + break; |
| 104 | + case StartTimer: |
| 105 | + assertNoMissingFields(decision.getStartTimerDecisionAttributes()); |
| 106 | + break; |
| 107 | + case CompleteWorkflowExecution: |
| 108 | + assertNoMissingFields(decision.getCompleteWorkflowExecutionDecisionAttributes()); |
| 109 | + break; |
| 110 | + case FailWorkflowExecution: |
| 111 | + assertNoMissingFields(decision.getFailWorkflowExecutionDecisionAttributes()); |
| 112 | + break; |
| 113 | + case CancelTimer: |
| 114 | + assertNoMissingFields(decision.getCancelTimerDecisionAttributes()); |
| 115 | + break; |
| 116 | + case CancelWorkflowExecution: |
| 117 | + assertNoMissingFields(decision.getCancelWorkflowExecutionDecisionAttributes()); |
| 118 | + break; |
| 119 | + case RequestCancelExternalWorkflowExecution: |
| 120 | + assertNoMissingFields( |
| 121 | + decision.getRequestCancelExternalWorkflowExecutionDecisionAttributes()); |
| 122 | + break; |
| 123 | + case RecordMarker: |
| 124 | + assertNoMissingFields(decision.getRecordMarkerDecisionAttributes()); |
| 125 | + break; |
| 126 | + case ContinueAsNewWorkflowExecution: |
| 127 | + assertNoMissingFields(decision.getContinueAsNewWorkflowExecutionDecisionAttributes()); |
| 128 | + break; |
| 129 | + case StartChildWorkflowExecution: |
| 130 | + assertNoMissingFields(decision.getStartChildWorkflowExecutionDecisionAttributes()); |
| 131 | + break; |
| 132 | + case SignalExternalWorkflowExecution: |
| 133 | + assertNoMissingFields(decision.getSignalExternalWorkflowExecutionDecisionAttributes()); |
| 134 | + break; |
| 135 | + case UpsertWorkflowSearchAttributes: |
| 136 | + assertNoMissingFields(decision.getUpsertWorkflowSearchAttributesDecisionAttributes()); |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments