Skip to content

Commit

Permalink
refactor(watcher): test naming and log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
apancorb committed Nov 12, 2024
1 parent 302f8f3 commit 713d55c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void onRetiringFederationChange(Federation newRetiringFederation) {
public void onProposedFederationChange(Federation newProposedFederation) {
if (newProposedFederation == null) {
logger.info(
"[onProposedFederationChange] New proposed federation changed to none");
"[onProposedFederationChange] Proposed federation was cleared");
return;
}

Expand Down
32 changes: 16 additions & 16 deletions src/test/java/co/rsk/federate/watcher/FederationWatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ void onBestBlock_whenNoActiveFederationAndProposedFederationExists_shouldTrigger
verify(federationProvider).getActiveFederation();
verify(federationWatcherListener).onActiveFederationChange(FIRST_FEDERATION);

verify(federationProvider).getActiveFederation();
verify(federationProvider).getRetiringFederationAddress();
verify(federationProvider, never()).getRetiringFederation();
verify(federationWatcherListener, never()).onRetiringFederationChange(any(Federation.class));
}

@Test
void onBestBlock_whenProposedFederationChangesFromProposedToOtherProposed_shouldTriggerProposedFederationChange() throws Exception {
void onBestBlock_whenProposedFederationChanged_shouldTriggerProposedFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(FIRST_FEDERATION, null, SECOND_FEDERATION);
var rskListener = setupAndGetRskListener(SECOND_FEDERATION, FIRST_FEDERATION, null);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.of(THIRD_FEDERATION.getAddress()));
when(federationProvider.getProposedFederation()).thenReturn(Optional.of(THIRD_FEDERATION));
when(federationProvider.getActiveFederationAddress()).thenReturn(FIRST_FEDERATION.getAddress());
Expand All @@ -133,9 +133,9 @@ void onBestBlock_whenProposedFederationChangesFromProposedToOtherProposed_should
}

@Test
void onBestBlock_whenProposedFederationChangesFromProposedToNone_shouldTriggerProposedFederationChange() throws Exception {
void onBestBlock_whenProposedFederationIsCleared_shouldTriggerProposedFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(FIRST_FEDERATION, null, SECOND_FEDERATION);
var rskListener = setupAndGetRskListener(SECOND_FEDERATION, FIRST_FEDERATION, null);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getProposedFederation()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(FIRST_FEDERATION.getAddress());
Expand Down Expand Up @@ -164,7 +164,7 @@ void onBestBlock_whenProposedFederationChangesFromProposedToNone_shouldTriggerPr
@Test
void onBestBlock_whenProposedFederationChangesToActive_shouldTriggerActiveAndProposedFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(FIRST_FEDERATION, null, SECOND_FEDERATION);
var rskListener = setupAndGetRskListener(SECOND_FEDERATION, FIRST_FEDERATION, null);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getProposedFederation()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(SECOND_FEDERATION.getAddress());
Expand Down Expand Up @@ -222,7 +222,7 @@ void onBestBlock_whenNoActiveFederation_shouldTriggerActiveFederationChange() th
@Test
void onBestBlock_whenActiveFederationChangesFromActiveToOtherActive_shouldTriggerActiveFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(FIRST_FEDERATION, null, null);
var rskListener = setupAndGetRskListener(null, FIRST_FEDERATION, null);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(SECOND_FEDERATION.getAddress());
when(federationProvider.getActiveFederation()).thenReturn(SECOND_FEDERATION);
Expand All @@ -248,9 +248,9 @@ void onBestBlock_whenActiveFederationChangesFromActiveToOtherActive_shouldTrigge
}

@Test
void onBestBlock_whenNoActiveFederationChange_shouldNotTriggerActiveOrRetiringFederationChange() throws Exception {
void onBestBlock_whenNoActiveFederationChange_shouldNotTriggerActiveOrRetiringOrProposedFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(FIRST_FEDERATION, null, null);
var rskListener = setupAndGetRskListener(null, FIRST_FEDERATION, null);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(FIRST_FEDERATION.getAddress());
when(federationProvider.getRetiringFederationAddress()).thenReturn(Optional.empty());
Expand All @@ -275,9 +275,9 @@ void onBestBlock_whenNoActiveFederationChange_shouldNotTriggerActiveOrRetiringFe
}

@Test
void onBestBlock_whenNoActiveAndRetiringAndProposedChangeInFederation_shouldNotTriggerActiveOrRetiringFederationChange() throws Exception {
void onBestBlock_whenNoActiveAndRetiringAndProposedChangeInFederation_shouldNotTriggerActiveOrRetiringOrProposedFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(FIRST_FEDERATION, SECOND_FEDERATION, THIRD_FEDERATION);
var rskListener = setupAndGetRskListener(THIRD_FEDERATION, FIRST_FEDERATION, SECOND_FEDERATION);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.of(THIRD_FEDERATION.getAddress()));
when(federationProvider.getActiveFederationAddress()).thenReturn(FIRST_FEDERATION.getAddress());
when(federationProvider.getRetiringFederationAddress()).thenReturn(Optional.of(SECOND_FEDERATION.getAddress()));
Expand All @@ -304,7 +304,7 @@ void onBestBlock_whenNoActiveAndRetiringAndProposedChangeInFederation_shouldNotT
@Test
void onBestBlock_whenActiveFederationChangesToRetiring_shouldTriggerRetiringFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(SECOND_FEDERATION, null, null);
var rskListener = setupAndGetRskListener(null, SECOND_FEDERATION, null);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(SECOND_FEDERATION.getAddress());
when(federationProvider.getRetiringFederationAddress()).thenReturn(Optional.of(FIRST_FEDERATION.getAddress()));
Expand Down Expand Up @@ -332,7 +332,7 @@ void onBestBlock_whenActiveFederationChangesToRetiring_shouldTriggerRetiringFede
@Test
void onBestBlock_whenRetiringFederationChangesToNone_shouldTriggerRetiringFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(SECOND_FEDERATION, FIRST_FEDERATION, null);
var rskListener = setupAndGetRskListener(null, SECOND_FEDERATION, FIRST_FEDERATION);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(SECOND_FEDERATION.getAddress());
when(federationProvider.getRetiringFederationAddress()).thenReturn(Optional.empty());
Expand Down Expand Up @@ -360,7 +360,7 @@ void onBestBlock_whenRetiringFederationChangesToNone_shouldTriggerRetiringFedera
@Test
void onBestBlock_whenRetiringFederationChangesToOtherRetiring_shouldTriggerRetiringFederationChange() throws Exception {
// Arrange
var rskListener = setupAndGetRskListener(THIRD_FEDERATION, FIRST_FEDERATION, null);
var rskListener = setupAndGetRskListener(null, THIRD_FEDERATION, FIRST_FEDERATION);
when(federationProvider.getProposedFederationAddress()).thenReturn(Optional.empty());
when(federationProvider.getActiveFederationAddress()).thenReturn(THIRD_FEDERATION.getAddress());
when(federationProvider.getRetiringFederationAddress()).thenReturn(Optional.of(SECOND_FEDERATION.getAddress()));
Expand All @@ -386,7 +386,7 @@ void onBestBlock_whenRetiringFederationChangesToOtherRetiring_shouldTriggerRetir
}

private EthereumListenerAdapter setupAndGetRskListener(
Federation activeFederation, Federation retiringFederation, Federation proposedFederation) throws Exception {
Federation proposedFederation, Federation activeFederation, Federation retiringFederation) throws Exception {
// Mock the behavior of adding a listener
AtomicReference<EthereumListenerAdapter> listenerRef = new AtomicReference<>();
doAnswer((InvocationOnMock m) -> {
Expand All @@ -397,9 +397,9 @@ private EthereumListenerAdapter setupAndGetRskListener(
federationWatcher.start(federationProvider, null);

// Set up federationWatcher and internal states
TestUtils.setInternalState(federationWatcher, "proposedFederation", proposedFederation);
TestUtils.setInternalState(federationWatcher, "activeFederation", activeFederation);
TestUtils.setInternalState(federationWatcher, "retiringFederation", retiringFederation);
TestUtils.setInternalState(federationWatcher, "proposedFederation", proposedFederation);

// Retrieve and return the listener
EthereumListenerAdapter listener = listenerRef.get();
Expand Down

0 comments on commit 713d55c

Please sign in to comment.