Skip to content

Commit

Permalink
Added more CloudEvent test cases to appease Codecov
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Sieben <siebenluke@gmail.com>
  • Loading branch information
siebenluke committed Jan 5, 2024
1 parent 1321db3 commit 78e9575
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sdk/src/test/java/io/dapr/client/CloudEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

package io.dapr.client;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.client.domain.CloudEvent;
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

public class CloudEventTest {
Expand All @@ -30,6 +33,24 @@ public class CloudEventTest {
public static class MyClass {
public int id;
public String name;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

MyClass myClass = (MyClass) o;

if (id != myClass.id) return false;
return Objects.equals(name, myClass.name);
}

@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}

@Test
Expand Down Expand Up @@ -196,4 +217,40 @@ public void deserializeBinaryData() throws Exception {
assertNull(cloudEvent.getData());
assertArrayEquals(expected, cloudEvent.getBinaryData());
}

@Test
public void serializeObjectClass() throws Exception {
CloudEvent<MyClass> cloudEvent = new CloudEvent<>();
MyClass myClass = new MyClass();
myClass.id = 1;
myClass.name = "Hello World";
cloudEvent.setData(myClass);
OffsetDateTime now = OffsetDateTime.now();
cloudEvent.setTime(now);

String cloudEventAsString = OBJECT_MAPPER.writeValueAsString(cloudEvent);
CloudEvent<MyClass> cloudEventDeserialized = OBJECT_MAPPER.readValue(cloudEventAsString,
new TypeReference<CloudEvent<MyClass>>() {});
assertEquals(cloudEvent, cloudEventDeserialized);
assertEquals(now, cloudEventDeserialized.getTime());
MyClass myClassDeserialized = cloudEventDeserialized.getData();
assertEquals(myClass.id, myClassDeserialized.id);
assertEquals(myClass.name, myClassDeserialized.name);
}

@Test
public void equalsCodecovTest() {
CloudEvent<?> cloudEvent = new CloudEvent<>();
CloudEvent<?> cloudEventCopy = cloudEvent;
assertEquals(cloudEvent, cloudEventCopy);
assertFalse(cloudEventCopy.equals(null));
assertFalse(cloudEventCopy.equals(""));
}

@Test
public void hashCodeCodecovTest() {
CloudEvent<?> cloudEvent = new CloudEvent<>();
final int EXPECTED_EMPTY_HASH_CODE = -505558625;
assertEquals(EXPECTED_EMPTY_HASH_CODE, cloudEvent.hashCode());
}
}

0 comments on commit 78e9575

Please sign in to comment.