|
| 1 | +package org.phoebus.channelfinder.processors; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 8 | +import org.junit.jupiter.api.Assertions; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.phoebus.channelfinder.entity.Channel; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.context.annotation.Configuration; |
| 16 | +import org.springframework.test.context.TestPropertySource; |
| 17 | + |
| 18 | +@SpringBootTest() |
| 19 | +@TestPropertySource(value = "classpath:application_test.properties") |
| 20 | +class ChannelProcessorServiceTest { |
| 21 | + |
| 22 | + private ChannelProcessorService channelProcessorService; |
| 23 | + |
| 24 | + @Autowired private DummyProcessor dummyProcessor; |
| 25 | + |
| 26 | + @Configuration |
| 27 | + static class TestConfig { |
| 28 | + @Bean |
| 29 | + public DummyProcessor dummyProcessor() { |
| 30 | + return new DummyProcessor(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + static class DummyProcessor implements ChannelProcessor { |
| 35 | + private final AtomicBoolean enabled = new AtomicBoolean(false); |
| 36 | + private final AtomicBoolean processed = new AtomicBoolean(false); |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean enabled() { |
| 40 | + return enabled.get(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void setEnabled(boolean enabled) { |
| 45 | + this.enabled.set(enabled); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public ChannelProcessorInfo processorInfo() { |
| 50 | + return new ChannelProcessorInfo("DummyProcessor", enabled.get(), Map.of()); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public long process(List<Channel> channels) throws JsonProcessingException { |
| 55 | + processed.set(true); |
| 56 | + return channels.size(); |
| 57 | + } |
| 58 | + |
| 59 | + public boolean hasBeenProcessed() { |
| 60 | + return processed.get(); |
| 61 | + } |
| 62 | + |
| 63 | + public void reset() { |
| 64 | + processed.set(false); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @BeforeEach |
| 69 | + void setUp() { |
| 70 | + channelProcessorService = new ChannelProcessorService(List.of(dummyProcessor), Runnable::run, 10); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testEnableAndDisableDummyProcessor() { |
| 75 | + // Initially, the dummy processor should be disabled |
| 76 | + Assertions.assertFalse( |
| 77 | + dummyProcessor.enabled(), "Dummy processor should be disabled initially"); |
| 78 | + |
| 79 | + // Enable the dummy processor |
| 80 | + channelProcessorService.setProcessorEnabled("DummyProcessor", true); |
| 81 | + Assertions.assertTrue(dummyProcessor.enabled(), "Dummy processor should be enabled"); |
| 82 | + |
| 83 | + // Disable the dummy processor |
| 84 | + channelProcessorService.setProcessorEnabled("DummyProcessor", false); |
| 85 | + Assertions.assertFalse(dummyProcessor.enabled(), "Dummy processor should be disabled"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testDummyProcessorProcessing() { |
| 90 | + // Disable the dummy processor |
| 91 | + dummyProcessor.reset(); |
| 92 | + channelProcessorService.setProcessorEnabled("DummyProcessor", false); |
| 93 | + Assertions.assertFalse(dummyProcessor.enabled(), "Dummy processor should be disabled"); |
| 94 | + |
| 95 | + // Process a channel - dummy processor should not be called |
| 96 | + channelProcessorService.sendToProcessors( |
| 97 | + Collections.singletonList(new Channel("test-channel"))); |
| 98 | + Assertions.assertFalse( |
| 99 | + dummyProcessor.hasBeenProcessed(), "Dummy processor should not have been called"); |
| 100 | + |
| 101 | + // Enable the dummy processor |
| 102 | + channelProcessorService.setProcessorEnabled("DummyProcessor", true); |
| 103 | + Assertions.assertTrue(dummyProcessor.enabled(), "Dummy processor should be enabled"); |
| 104 | + |
| 105 | + // Process a channel - dummy processor should be called |
| 106 | + channelProcessorService.sendToProcessors( |
| 107 | + Collections.singletonList(new Channel("test-channel"))); |
| 108 | + Assertions.assertTrue( |
| 109 | + dummyProcessor.hasBeenProcessed(), "Dummy processor should have been called"); |
| 110 | + } |
| 111 | +} |
0 commit comments