|
| 1 | +package org.phoebus.channelfinder.processors; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import okhttp3.mockwebserver.MockResponse; |
| 6 | +import okhttp3.mockwebserver.MockWebServer; |
| 7 | +import okhttp3.mockwebserver.RecordedRequest; |
| 8 | +import org.jetbrains.annotations.NotNull; |
| 9 | +import org.junit.jupiter.api.AfterEach; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.Arguments; |
| 14 | +import org.junit.jupiter.params.provider.MethodSource; |
| 15 | +import org.phoebus.channelfinder.entity.Channel; |
| 16 | +import org.phoebus.channelfinder.entity.Property; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 19 | +import org.springframework.test.context.TestPropertySource; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | + |
| 29 | +@WebMvcTest(AAChannelProcessor.class) |
| 30 | +@TestPropertySource(value = "classpath:application_test.properties") |
| 31 | +class AAChannelProcessorIT { |
| 32 | + |
| 33 | + protected static Property archiveProperty = new Property("archive", "owner", "default"); |
| 34 | + protected static Property activeProperty = new Property("pvStatus", "owner", "Active"); |
| 35 | + protected static Property inactiveProperty = new Property("pvStatus", "owner", "Inactive"); |
| 36 | + |
| 37 | + @Autowired |
| 38 | + AAChannelProcessor aaChannelProcessor; |
| 39 | + |
| 40 | + MockWebServer mockArchiverAppliance; |
| 41 | + ObjectMapper objectMapper; |
| 42 | + |
| 43 | + @NotNull |
| 44 | + private static Stream<Arguments> processSource() { |
| 45 | + return Stream.of( |
| 46 | + Arguments.of( |
| 47 | + new Channel("PVArchivedActive", "owner", List.of(archiveProperty, activeProperty), List.of()), |
| 48 | + "Being archived", |
| 49 | + "", |
| 50 | + ""), |
| 51 | + Arguments.of( |
| 52 | + new Channel("PVPausedActive", "owner", List.of(archiveProperty, activeProperty), List.of()), |
| 53 | + "Paused", |
| 54 | + "resumeArchivingPV", |
| 55 | + "[\"PVPausedActive\"]"), |
| 56 | + Arguments.of( |
| 57 | + new Channel("PVNoneActive", "owner", List.of(archiveProperty, activeProperty), List.of()), |
| 58 | + "Not being archived", |
| 59 | + "archivePV", |
| 60 | + "[{\"pv\":\"PVNoneActive\"}]"), |
| 61 | + Arguments.of( |
| 62 | + new Channel( |
| 63 | + "PVArchivedInactive", "owner", List.of(archiveProperty, inactiveProperty), List.of()), |
| 64 | + "Being archived", |
| 65 | + "pauseArchivingPV", |
| 66 | + "[\"PVArchivedInactive\"]"), |
| 67 | + Arguments.of( |
| 68 | + new Channel("PVPausedInactive", "owner", List.of(archiveProperty, inactiveProperty), List.of()), |
| 69 | + "Paused", |
| 70 | + "", |
| 71 | + ""), |
| 72 | + Arguments.of( |
| 73 | + new Channel("PVNoneInactive", "owner", List.of(archiveProperty, inactiveProperty), List.of()), |
| 74 | + "Not being archived", |
| 75 | + "", |
| 76 | + ""), |
| 77 | + Arguments.of( |
| 78 | + new Channel("PVArchivedNotag", "owner", List.of(), List.of()), |
| 79 | + "Being archived", |
| 80 | + "pauseArchivingPV", |
| 81 | + "[\"PVArchivedNotag\"]")); |
| 82 | + } |
| 83 | + |
| 84 | + public static void paramableAAChannelProcessorTest( |
| 85 | + MockWebServer mockArchiverAppliance, |
| 86 | + ObjectMapper objectMapper, |
| 87 | + ChannelProcessor aaChannelProcessor, |
| 88 | + Channel channel, |
| 89 | + String archiveStatus, |
| 90 | + String archiverEndpoint, |
| 91 | + String submissionBody) |
| 92 | + throws JsonProcessingException, InterruptedException { |
| 93 | + // Request to policies |
| 94 | + Map<String, String> policyList = Map.of("policy", "description"); |
| 95 | + mockArchiverAppliance.enqueue(new MockResponse() |
| 96 | + .setBody(objectMapper.writeValueAsString(policyList)) |
| 97 | + .addHeader("Content-Type", "application/json")); |
| 98 | + |
| 99 | + if (!archiveStatus.isEmpty()) { |
| 100 | + |
| 101 | + // Request to archiver status |
| 102 | + List<Map<String, String>> archivePVStatuses = |
| 103 | + List.of(Map.of("pvName", channel.getName(), "status", archiveStatus)); |
| 104 | + mockArchiverAppliance.enqueue(new MockResponse() |
| 105 | + .setBody(objectMapper.writeValueAsString(archivePVStatuses)) |
| 106 | + .addHeader("Content-Type", "application/json")); |
| 107 | + } |
| 108 | + if (!archiverEndpoint.isEmpty()) { |
| 109 | + // Request to archiver to archive |
| 110 | + List<Map<String, String>> archiverResponse = |
| 111 | + List.of(Map.of("pvName", channel.getName(), "status", "Archive request submitted")); |
| 112 | + mockArchiverAppliance.enqueue(new MockResponse() |
| 113 | + .setBody(objectMapper.writeValueAsString(archiverResponse)) |
| 114 | + .addHeader("Content-Type", "application/json")); |
| 115 | + } |
| 116 | + |
| 117 | + aaChannelProcessor.process(List.of(channel)); |
| 118 | + |
| 119 | + int expectedRequests = 1; |
| 120 | + RecordedRequest requestPolicy = mockArchiverAppliance.takeRequest(2, TimeUnit.SECONDS); |
| 121 | + assert requestPolicy != null; |
| 122 | + assertEquals("/mgmt/bpl/getPolicyList", requestPolicy.getPath()); |
| 123 | + |
| 124 | + if (!archiveStatus.isEmpty()) { |
| 125 | + expectedRequests += 1; |
| 126 | + RecordedRequest requestStatus = mockArchiverAppliance.takeRequest(2, TimeUnit.SECONDS); |
| 127 | + assert requestStatus != null; |
| 128 | + assert requestStatus.getRequestUrl() != null; |
| 129 | + assertEquals("/mgmt/bpl/getPVStatus", requestStatus.getRequestUrl().encodedPath()); |
| 130 | + } |
| 131 | + |
| 132 | + if (!archiverEndpoint.isEmpty()) { |
| 133 | + expectedRequests += 1; |
| 134 | + RecordedRequest requestAction = mockArchiverAppliance.takeRequest(2, TimeUnit.SECONDS); |
| 135 | + assert requestAction != null; |
| 136 | + assertEquals("/mgmt/bpl/" + archiverEndpoint, requestAction.getPath()); |
| 137 | + assertEquals(submissionBody, requestAction.getBody().readUtf8()); |
| 138 | + } |
| 139 | + |
| 140 | + assertEquals(mockArchiverAppliance.getRequestCount(), expectedRequests); |
| 141 | + } |
| 142 | + |
| 143 | + @BeforeEach |
| 144 | + void setUp() throws IOException { |
| 145 | + mockArchiverAppliance = new MockWebServer(); |
| 146 | + mockArchiverAppliance.start(17665); |
| 147 | + |
| 148 | + objectMapper = new ObjectMapper(); |
| 149 | + } |
| 150 | + |
| 151 | + @AfterEach |
| 152 | + void teardown() throws IOException { |
| 153 | + mockArchiverAppliance.shutdown(); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void testProcessNoPVs() throws JsonProcessingException { |
| 158 | + aaChannelProcessor.process(List.of()); |
| 159 | + |
| 160 | + assertEquals(mockArchiverAppliance.getRequestCount(), 0); |
| 161 | + } |
| 162 | + |
| 163 | + @ParameterizedTest |
| 164 | + @MethodSource("processSource") |
| 165 | + void testProcessNotArchivedActive( |
| 166 | + Channel channel, String archiveStatus, String archiverEndpoint, String submissionBody) |
| 167 | + throws JsonProcessingException, InterruptedException { |
| 168 | + paramableAAChannelProcessorTest( |
| 169 | + mockArchiverAppliance, |
| 170 | + objectMapper, |
| 171 | + aaChannelProcessor, |
| 172 | + channel, |
| 173 | + archiveStatus, |
| 174 | + archiverEndpoint, |
| 175 | + submissionBody); |
| 176 | + } |
| 177 | +} |
0 commit comments