Skip to content

Commit

Permalink
Added helper method for DecodedLog
Browse files Browse the repository at this point in the history
  • Loading branch information
wkennedy committed Jul 16, 2024
1 parent 826bf7d commit 49ed773
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/github/wkennedy/abi/models/DecodedLog.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.wkennedy.abi.models;

import java.util.List;
import java.util.Optional;

public class DecodedLog {
private String name;
Expand Down Expand Up @@ -40,6 +41,13 @@ public void setEvents(List<Param> events) {
this.events = events;
}

public Param getEvent(String name) {
return Optional.ofNullable(events)
.flatMap(eventList ->
eventList.stream().filter(event -> event.getName().equals(name)).findFirst())
.orElse(null);
}

@Override
public String toString() {
return "DecodedLog{" +
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/github/wkennedy/abi/models/DecodedLogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.wkennedy.abi.models;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

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

public class DecodedLogTest {

@Test
void getEventTestEventPresent() {
Param param1 = new Param("event1", "type1", "value1");
Param param2 = new Param("event2", "type2", "value2");
List<Param> events = Arrays.asList(param1, param2);

DecodedLog decodedLog = new DecodedLog("name", "address", events);

Param foundEvent = decodedLog.getEvent("event2");

assertEquals(param2, foundEvent);
}

@Test
void getEventTestEventNotPresent() {
Param param1 = new Param("event1", "type1", "value1");
List<Param> events = Arrays.asList(param1);

DecodedLog decodedLog = new DecodedLog("name", "address", events);

Param foundEvent = decodedLog.getEvent("event2");

assertNull(foundEvent);
}

@Test
void getEventTestNoEvents() {
DecodedLog decodedLog = new DecodedLog("name", "address", null);

Param foundEvent = decodedLog.getEvent("event1");

assertNull(foundEvent);
}
}

0 comments on commit 49ed773

Please sign in to comment.