Skip to content

Commit

Permalink
use @SPY instead of @mock to test AssaultProperties too.
Browse files Browse the repository at this point in the history
  • Loading branch information
schegge42 committed Nov 30, 2024
1 parent 833242d commit 0909bed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import de.codecentric.spring.boot.chaos.monkey.assaults.ChaosMonkeyAssault;
Expand All @@ -31,11 +30,14 @@
import de.codecentric.spring.boot.chaos.monkey.configuration.toggles.DefaultChaosToggles;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

/** @author Benjamin Wilms */
Expand All @@ -44,7 +46,7 @@ class ChaosMonkeyRequestScopeTest {

ChaosMonkeyRequestScope chaosMonkeyRequestScope;

@Mock
@Spy
AssaultProperties assaultProperties;

@Mock
Expand Down Expand Up @@ -86,7 +88,7 @@ class GivenChaosMonekyExecutionIsEnabled {

@BeforeEach
void setUpForChaosMonkeyExecutionEnabled() {
given(assaultProperties.getLevel()).willReturn(1);
assaultProperties.setLevel(1);
given(assaultProperties.getTroubleRandom()).willReturn(1);
given(chaosMonkeyProperties.isEnabled()).willReturn(true);
given(chaosMonkeySettings.getAssaultProperties()).willReturn(assaultProperties);
Expand Down Expand Up @@ -187,7 +189,7 @@ void givenNoAssaultsActiveExpectNoAttack() {

@Test
void givenAssaultLevelTooHighExpectNoLogging() {
given(assaultProperties.getLevel()).willReturn(1000);
assaultProperties.setLevel(1000);
given(assaultProperties.getTroubleRandom()).willReturn(9);

chaosMonkeyRequestScope.callChaosMonkey(null, null);
Expand All @@ -198,10 +200,7 @@ void givenAssaultLevelTooHighExpectNoLogging() {

@Test
void chaosMonkeyIsNotCalledWhenServiceNotWatched() {
String customService = "CustomService";

given(assaultProperties.getWatchedCustomServices()).willReturn(Collections.singletonList(customService));
given(chaosMonkeySettings.getAssaultProperties().isWatchedCustomServicesActive()).willReturn(true);
assaultProperties.setWatchedCustomServices(List.of("CustomService"));

chaosMonkeyRequestScope.callChaosMonkey(null, "notInListService");

Expand All @@ -211,49 +210,40 @@ void chaosMonkeyIsNotCalledWhenServiceNotWatched() {

@Test
void chaosMonkeyIsCalledWhenServiceIsWatched() {
String customService = "CustomService";

assaultProperties.setWatchedCustomServices(List.of("CustomService"));
given(exceptionAssault.isActive()).willReturn(true);
given(assaultProperties.getWatchedCustomServices()).willReturn(Collections.singletonList(customService));
given(chaosMonkeySettings.getAssaultProperties().isWatchedCustomServicesActive()).willReturn(true);
given(latencyAssault.isActive()).willReturn(true);
given(assaultProperties.chooseAssault(2)).willReturn(0);

chaosMonkeyRequestScope.callChaosMonkey(null, customService);
chaosMonkeyRequestScope.callChaosMonkey(null, "CustomService");

verify(latencyAssault).attack();
verify(exceptionAssault, never()).attack();
}

@Test
void chaosMonkeyIsCalledWhenServiceIsWatchedWhenSimpleNameIsMethodReference() {
String customRepository = "org.springframework.data.repository.CrudRepository";

assaultProperties.setWatchedCustomServices(List.of("org.springframework.data.repository.CrudRepository"));
given(exceptionAssault.isActive()).willReturn(true);
given(assaultProperties.getWatchedCustomServices()).willReturn(Collections.singletonList(customRepository));
given(chaosMonkeySettings.getAssaultProperties().isWatchedCustomServicesActive()).willReturn(true);
given(latencyAssault.isActive()).willReturn(true);
given(assaultProperties.chooseAssault(2)).willReturn(0);

String simpleName = customRepository + ".findAll";
chaosMonkeyRequestScope.callChaosMonkey(null, simpleName);
chaosMonkeyRequestScope.callChaosMonkey(null, "org.springframework.data.repository.CrudRepository.findAll");

verify(latencyAssault).attack();
verify(exceptionAssault, never()).attack();
}

@Test
void chaosMonkeyIsCalledWhenServiceIsWatchedWhenSimpleNameIsPackageReference() {
String packageName = "org.springframework.data.repository";

assaultProperties.setWatchedCustomServices(List.of("org.springframework.data.repository"));
given(exceptionAssault.isActive()).willReturn(true);
given(assaultProperties.getWatchedCustomServices()).willReturn(Collections.singletonList(packageName));
given(chaosMonkeySettings.getAssaultProperties().isWatchedCustomServicesActive()).willReturn(true);
given(latencyAssault.isActive()).willReturn(true);
given(assaultProperties.chooseAssault(2)).willReturn(0);

String simpleName = packageName + "CrudRepository.findAll";
chaosMonkeyRequestScope.callChaosMonkey(null, simpleName);
chaosMonkeyRequestScope.callChaosMonkey(null, "org.springframework.data.repository.CrudRepository.findAll");

verify(latencyAssault).attack();
verify(exceptionAssault, never()).attack();
Expand All @@ -275,14 +265,13 @@ void shouldMakeUncategorizedCustomAssaultsRequestScopeByDefault() {

@Test
void assaultShouldBeDeterministicIfConfigured() {
assaultProperties.setLevel(3);
assaultProperties.setDeterministic(true);
ChaosMonkeyAssault customAssault = mock(ChaosMonkeyAssault.class);
given(customAssault.isActive()).willReturn(true);
given(chaosMonkeyProperties.isEnabled()).willReturn(true);
given(chaosMonkeySettings.getAssaultProperties()).willReturn(assaultProperties);

given(assaultProperties.isDeterministic()).willReturn(true);
given(assaultProperties.getLevel()).willReturn(3);

ChaosMonkeyRequestScope customScope = new ChaosMonkeyRequestScope(chaosMonkeySettings, Collections.emptyList(),
Collections.singletonList(customAssault), metricEventPublisherMock, new DefaultChaosToggles(),
new DefaultChaosToggleNameMapper(chaosMonkeyProperties.getTogglePrefix()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -113,4 +113,14 @@ void disableChaosMonkey() {
void getWatcherProperties() {
assertThat(chaosMonkeyJmxEndpoint.getWatcherProperties()).isEqualTo(chaosMonkeySettings.getWatcherProperties());
}

@Test
void getStatus() {
OffsetDateTime enabledAt = OffsetDateTime.now().withNano(0);
chaosMonkeyJmxEndpoint.enableChaosMonkey();
ChaosMonkeyStatusResponseDto enabledDto = chaosMonkeyJmxEndpoint.getStatus();
assertThat(enabledDto.isEnabled()).isEqualTo(true);
assertThat(enabledDto.getEnabledAt()).isAfterOrEqualTo(enabledAt);
assertThat(chaosMonkeySettings.getChaosMonkeyProperties().isEnabled()).isTrue();
}
}

0 comments on commit 0909bed

Please sign in to comment.