Skip to content

Commit

Permalink
adds coverage to a minor internal struct file (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidporter-id-au authored Nov 6, 2024
1 parent 6d76d41 commit b08b8d7
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,20 @@ public SignalExternalWorkflowParameters withWorkflowId(String workflowId) {

@Override
public String toString() {

String inputStr;
if (input == null) {
inputStr = "";
} else if (input.length > 512) {
inputStr = new String(input, 0, 512, StandardCharsets.UTF_8);
} else {
inputStr = new String(input, StandardCharsets.UTF_8);
}

StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append("SignalName: " + signalName + ", ");
sb.append("Input: " + new String(input, 0, 512, StandardCharsets.UTF_8) + ", ");
sb.append("Input: " + inputStr + ", ");
sb.append("WorkflowId: " + workflowId + ", ");
sb.append("RunId: " + runId + ", ");
sb.append("}");
Expand All @@ -108,6 +118,7 @@ public SignalExternalWorkflowParameters copy() {
SignalExternalWorkflowParameters result = new SignalExternalWorkflowParameters();
result.setInput(input);
result.setRunId(runId);
result.setDomain(domain);
result.setSignalName(signalName);
result.setWorkflowId(workflowId);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/**
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* <p>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
*
* <p>http://aws.amazon.com/apache2.0
*
* <p>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 com.uber.cadence.internal.replay;

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import org.junit.Test;

public class SignalExternalWorkflowParametersTest {

@Test
public void testSetAndGetDomain() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
params.setDomain("TestDomain");
assertEquals("TestDomain", params.getDomain());
}

@Test
public void testSetAndGetInput() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
byte[] input = "TestInput".getBytes(StandardCharsets.UTF_8);
params.setInput(input);
assertArrayEquals(input, params.getInput());
}

@Test
public void testWithInputChain() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();

byte[] input = "ChainInput".getBytes(StandardCharsets.UTF_8);
SignalExternalWorkflowParameters result = params.withInput(input);
assertArrayEquals(input, params.getInput());
assertSame(params, result);
}

@Test
public void testSetAndGetRunId() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
params.setRunId("TestRunId");
assertEquals("TestRunId", params.getRunId());
}

@Test
public void testWithRunIdChain() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();

SignalExternalWorkflowParameters result = params.withRunId("ChainRunId");
assertEquals("ChainRunId", params.getRunId());
assertSame(params, result);
}

@Test
public void testSetAndGetSignalName() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();

params.setSignalName("TestSignalName");
assertEquals("TestSignalName", params.getSignalName());
}

@Test
public void testWithSignalNameChain() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
SignalExternalWorkflowParameters result = params.withSignalName("ChainSignalName");
assertEquals("ChainSignalName", params.getSignalName());
assertSame(params, result);
}

@Test
public void testSetAndGetWorkflowId() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
params.setWorkflowId("TestWorkflowId");
assertEquals("TestWorkflowId", params.getWorkflowId());
}

@Test
public void testWithWorkflowIdChain() {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
SignalExternalWorkflowParameters result = params.withWorkflowId("ChainWorkflowId");
assertEquals("ChainWorkflowId", params.getWorkflowId());
assertSame(params, result);
}

@Test
public void testStringification() {

String longStr =
"1000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000001";

SignalExternalWorkflowParameters longParams =
new SignalExternalWorkflowParameters()
.withWorkflowId("ChainWorkflowId")
.withSignalName("signalName")
.withInput(longStr.getBytes())
.withRunId("some-run-id");

SignalExternalWorkflowParameters shortParams =
new SignalExternalWorkflowParameters()
.withWorkflowId("ChainWorkflowId")
.withSignalName("signalName")
.withInput("smol".getBytes())
.withRunId("some-run-id");

assertEquals(
"{SignalName: signalName, Input: "
+ longStr.substring(0, 512)
+ ", WorkflowId: ChainWorkflowId, RunId: some-run-id, }",
longParams.toString());

assertEquals(
"{SignalName: signalName, Input: "
+ "smol"
+ ", WorkflowId: ChainWorkflowId, RunId: some-run-id, }",
shortParams.toString());

assertEquals(
"{SignalName: null, Input: , WorkflowId: null, RunId: null, }",
new SignalExternalWorkflowParameters().toString());
}

@Test
public void testClone() throws CloneNotSupportedException {
SignalExternalWorkflowParameters params = new SignalExternalWorkflowParameters();
params.setDomain("Domain");
params.setRunId("RunId");
params.setSignalName("SignalName");
params.setWorkflowId("WorkflowId");
params.setInput("Input".getBytes(StandardCharsets.UTF_8));

SignalExternalWorkflowParameters clonedParams = params.copy();

assertNotSame(params, clonedParams);

assertEquals(params.getDomain(), clonedParams.getDomain());
assertArrayEquals(params.getInput(), clonedParams.getInput());
assertEquals(params.getRunId(), clonedParams.getRunId());
assertEquals(params.getSignalName(), clonedParams.getSignalName());
assertEquals(params.getWorkflowId(), clonedParams.getWorkflowId());

// there's 5 assertions here
// update if there's any additional properties
assertEquals(5, getPropertyCount(params));
}

private static int getPropertyCount(Object obj) {
int count = 0;
Class<?> clazz = obj.getClass();

while (clazz != null) {
Field[] fields = clazz.getDeclaredFields();

for (Field field : fields) {
if (!field.getName().equals("$jacocoData")) {
count++;
}
}
clazz = clazz.getSuperclass();
}

return count;
}
}

0 comments on commit b08b8d7

Please sign in to comment.