|
| 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.replay; |
| 16 | + |
| 17 | +import static org.junit.Assert.*; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +public class SignalExternalWorkflowParametersTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testSetAndGetDomain() { |
| 27 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 28 | + params.setDomain("TestDomain"); |
| 29 | + assertEquals("TestDomain", params.getDomain()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testSetAndGetInput() { |
| 34 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 35 | + byte[] input = "TestInput".getBytes(StandardCharsets.UTF_8); |
| 36 | + params.setInput(input); |
| 37 | + assertArrayEquals(input, params.getInput()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testWithInputChain() { |
| 42 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 43 | + |
| 44 | + byte[] input = "ChainInput".getBytes(StandardCharsets.UTF_8); |
| 45 | + SignalExternalWorkflowParameters result = params.withInput(input); |
| 46 | + assertArrayEquals(input, params.getInput()); |
| 47 | + assertSame(params, result); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSetAndGetRunId() { |
| 52 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 53 | + params.setRunId("TestRunId"); |
| 54 | + assertEquals("TestRunId", params.getRunId()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testWithRunIdChain() { |
| 59 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 60 | + |
| 61 | + SignalExternalWorkflowParameters result = params.withRunId("ChainRunId"); |
| 62 | + assertEquals("ChainRunId", params.getRunId()); |
| 63 | + assertSame(params, result); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testSetAndGetSignalName() { |
| 68 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 69 | + |
| 70 | + params.setSignalName("TestSignalName"); |
| 71 | + assertEquals("TestSignalName", params.getSignalName()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testWithSignalNameChain() { |
| 76 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 77 | + SignalExternalWorkflowParameters result = params.withSignalName("ChainSignalName"); |
| 78 | + assertEquals("ChainSignalName", params.getSignalName()); |
| 79 | + assertSame(params, result); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testSetAndGetWorkflowId() { |
| 84 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 85 | + params.setWorkflowId("TestWorkflowId"); |
| 86 | + assertEquals("TestWorkflowId", params.getWorkflowId()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testWithWorkflowIdChain() { |
| 91 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 92 | + SignalExternalWorkflowParameters result = params.withWorkflowId("ChainWorkflowId"); |
| 93 | + assertEquals("ChainWorkflowId", params.getWorkflowId()); |
| 94 | + assertSame(params, result); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testStringification() { |
| 99 | + |
| 100 | + String longStr = |
| 101 | + "1000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 102 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 103 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 104 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 105 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 106 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 107 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 108 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 109 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 110 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 111 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 112 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 113 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 114 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 115 | + + "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 116 | + + "0000000000000000000000000000000000000000000000000000000000000000000000001"; |
| 117 | + |
| 118 | + SignalExternalWorkflowParameters longParams = |
| 119 | + new SignalExternalWorkflowParameters() |
| 120 | + .withWorkflowId("ChainWorkflowId") |
| 121 | + .withSignalName("signalName") |
| 122 | + .withInput(longStr.getBytes()) |
| 123 | + .withRunId("some-run-id"); |
| 124 | + |
| 125 | + SignalExternalWorkflowParameters shortParams = |
| 126 | + new SignalExternalWorkflowParameters() |
| 127 | + .withWorkflowId("ChainWorkflowId") |
| 128 | + .withSignalName("signalName") |
| 129 | + .withInput("smol".getBytes()) |
| 130 | + .withRunId("some-run-id"); |
| 131 | + |
| 132 | + assertEquals( |
| 133 | + "{SignalName: signalName, Input: " |
| 134 | + + longStr.substring(0, 512) |
| 135 | + + ", WorkflowId: ChainWorkflowId, RunId: some-run-id, }", |
| 136 | + longParams.toString()); |
| 137 | + |
| 138 | + assertEquals( |
| 139 | + "{SignalName: signalName, Input: " |
| 140 | + + "smol" |
| 141 | + + ", WorkflowId: ChainWorkflowId, RunId: some-run-id, }", |
| 142 | + shortParams.toString()); |
| 143 | + |
| 144 | + assertEquals( |
| 145 | + "{SignalName: null, Input: , WorkflowId: null, RunId: null, }", |
| 146 | + new SignalExternalWorkflowParameters().toString()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testClone() throws CloneNotSupportedException { |
| 151 | + SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters(); |
| 152 | + params.setDomain("Domain"); |
| 153 | + params.setRunId("RunId"); |
| 154 | + params.setSignalName("SignalName"); |
| 155 | + params.setWorkflowId("WorkflowId"); |
| 156 | + params.setInput("Input".getBytes(StandardCharsets.UTF_8)); |
| 157 | + |
| 158 | + SignalExternalWorkflowParameters clonedParams = params.copy(); |
| 159 | + |
| 160 | + assertNotSame(params, clonedParams); |
| 161 | + |
| 162 | + assertEquals(params.getDomain(), clonedParams.getDomain()); |
| 163 | + assertArrayEquals(params.getInput(), clonedParams.getInput()); |
| 164 | + assertEquals(params.getRunId(), clonedParams.getRunId()); |
| 165 | + assertEquals(params.getSignalName(), clonedParams.getSignalName()); |
| 166 | + assertEquals(params.getWorkflowId(), clonedParams.getWorkflowId()); |
| 167 | + |
| 168 | + // there's 5 assertions here |
| 169 | + // update if there's any additional properties |
| 170 | + assertEquals(5, getPropertyCount(params)); |
| 171 | + } |
| 172 | + |
| 173 | + private static int getPropertyCount(Object obj) { |
| 174 | + int count = 0; |
| 175 | + Class<?> clazz = obj.getClass(); |
| 176 | + |
| 177 | + while (clazz != null) { |
| 178 | + Field[] fields = clazz.getDeclaredFields(); |
| 179 | + |
| 180 | + for (Field field : fields) { |
| 181 | + if (!field.getName().equals("$jacocoData")) { |
| 182 | + count++; |
| 183 | + } |
| 184 | + } |
| 185 | + clazz = clazz.getSuperclass(); |
| 186 | + } |
| 187 | + |
| 188 | + return count; |
| 189 | + } |
| 190 | +} |
0 commit comments