|
| 1 | +/* |
| 2 | + * Copyright 2024 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.observation.tck; |
| 17 | + |
| 18 | +import io.micrometer.observation.Observation; |
| 19 | +import io.micrometer.observation.Observation.Event; |
| 20 | +import io.micrometer.observation.Observation.Scope; |
| 21 | +import io.micrometer.observation.ObservationRegistry; |
| 22 | +import io.micrometer.observation.tck.ObservationValidator.ValidationResult; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.util.function.Consumer; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link ObservationValidator}. |
| 32 | + * |
| 33 | + * @author Jonatan Ivanov |
| 34 | + */ |
| 35 | +class ObservationValidatorTests { |
| 36 | + |
| 37 | + private TestConsumer testConsumer; |
| 38 | + |
| 39 | + private ObservationRegistry registry; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + testConsumer = new TestConsumer(); |
| 44 | + registry = ObservationRegistry.create(); |
| 45 | + registry.observationConfig().observationHandler(new ObservationValidator(testConsumer)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void doubleStartShouldBeInvalid() { |
| 50 | + Observation.start("test", registry).start(); |
| 51 | + assertThat(testConsumer.toString()).isEqualTo("Invalid start: Observation has already been started"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void stopBeforeStartShouldBeInvalid() { |
| 56 | + Observation.createNotStarted("test", registry).stop(); |
| 57 | + assertThat(testConsumer.toString()).isEqualTo("Invalid stop: Observation has not been started yet"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void errorBeforeStartShouldBeInvalid() { |
| 62 | + Observation.createNotStarted("test", registry).error(new RuntimeException()); |
| 63 | + assertThat(testConsumer.toString()).isEqualTo("Invalid error signal: Observation has not been started yet"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void eventBeforeStartShouldBeInvalid() { |
| 68 | + Observation.createNotStarted("test", registry).event(Event.of("test")); |
| 69 | + assertThat(testConsumer.toString()).isEqualTo("Invalid event signal: Observation has not been started yet"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void scopeBeforeStartShouldBeInvalid() { |
| 74 | + Scope scope = Observation.createNotStarted("test", registry).openScope(); |
| 75 | + scope.reset(); |
| 76 | + scope.close(); |
| 77 | + assertThat(testConsumer.toString()).isEqualTo("Invalid scope opening: Observation has not been started yet\n" |
| 78 | + + "Invalid scope resetting: Observation has not been started yet\n" |
| 79 | + + "Invalid scope closing: Observation has not been started yet"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void observeAfterStartShouldBeInvalid() { |
| 84 | + Observation.start("test", registry).observe(() -> ""); |
| 85 | + assertThat(testConsumer.toString()).isEqualTo("Invalid start: Observation has already been started"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void doubleStopShouldBeInvalid() { |
| 90 | + Observation observation = Observation.start("test", registry); |
| 91 | + observation.stop(); |
| 92 | + observation.stop(); |
| 93 | + assertThat(testConsumer.toString()).isEqualTo("Invalid stop: Observation has already been stopped"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void errorAfterStopShouldBeInvalid() { |
| 98 | + Observation observation = Observation.start("test", registry); |
| 99 | + observation.stop(); |
| 100 | + observation.error(new RuntimeException()); |
| 101 | + assertThat(testConsumer.toString()).isEqualTo("Invalid error signal: Observation has already been stopped"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void eventAfterStopShouldBeInvalid() { |
| 106 | + Observation observation = Observation.start("test", registry); |
| 107 | + observation.stop(); |
| 108 | + observation.event(Event.of("test")); |
| 109 | + assertThat(testConsumer.toString()).isEqualTo("Invalid event signal: Observation has already been stopped"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void scopeAfterStopShouldBeInvalid() { |
| 114 | + Observation observation = Observation.start("test", registry); |
| 115 | + observation.stop(); |
| 116 | + Scope scope = observation.openScope(); |
| 117 | + scope.reset(); |
| 118 | + scope.close(); |
| 119 | + assertThat(testConsumer.toString()).isEqualTo("Invalid scope opening: Observation has already been stopped\n" |
| 120 | + + "Invalid scope resetting: Observation has already been stopped\n" |
| 121 | + + "Invalid scope closing: Observation has already been stopped"); |
| 122 | + } |
| 123 | + |
| 124 | + static class TestConsumer implements Consumer<ValidationResult> { |
| 125 | + |
| 126 | + private final StringBuilder stringBuilder = new StringBuilder(); |
| 127 | + |
| 128 | + @Override |
| 129 | + public void accept(ValidationResult validationResult) { |
| 130 | + stringBuilder.append(validationResult.getMessage()).append("\n"); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public String toString() { |
| 135 | + return stringBuilder.toString().trim(); |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments