From 0909bed1013c3e358f65cf95720fc1e188e60d0f Mon Sep 17 00:00:00 2001 From: Jens Kaiser Date: Sat, 30 Nov 2024 12:41:04 +0100 Subject: [PATCH] use @Spy instead of @Mock to test AssaultProperties too. --- .../ChaosMonkeyRequestScopeTest.java | 41 +++++++------------ ...haosMonkeyRequestScopeJmxEndpointTest.java | 12 +++++- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/component/ChaosMonkeyRequestScopeTest.java b/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/component/ChaosMonkeyRequestScopeTest.java index 4531e8e5..3fede7a3 100644 --- a/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/component/ChaosMonkeyRequestScopeTest.java +++ b/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/component/ChaosMonkeyRequestScopeTest.java @@ -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; @@ -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 */ @@ -44,7 +46,7 @@ class ChaosMonkeyRequestScopeTest { ChaosMonkeyRequestScope chaosMonkeyRequestScope; - @Mock + @Spy AssaultProperties assaultProperties; @Mock @@ -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); @@ -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); @@ -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"); @@ -211,15 +210,13 @@ 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(); @@ -227,16 +224,13 @@ void chaosMonkeyIsCalledWhenServiceIsWatched() { @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(); @@ -244,16 +238,12 @@ void chaosMonkeyIsCalledWhenServiceIsWatchedWhenSimpleNameIsMethodReference() { @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(); @@ -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())); diff --git a/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/endpoints/ChaosMonkeyRequestScopeJmxEndpointTest.java b/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/endpoints/ChaosMonkeyRequestScopeJmxEndpointTest.java index 6fdd5120..76b2b5bd 100644 --- a/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/endpoints/ChaosMonkeyRequestScopeJmxEndpointTest.java +++ b/chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/endpoints/ChaosMonkeyRequestScopeJmxEndpointTest.java @@ -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. @@ -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(); + } }