|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.nifi.connectable; |
| 18 | + |
| 19 | +import org.apache.nifi.connectable.ConnectionUtils.FlowFileCloneResult; |
| 20 | +import org.apache.nifi.controller.queue.FlowFileQueue; |
| 21 | +import org.apache.nifi.controller.repository.ContentRepository; |
| 22 | +import org.apache.nifi.controller.repository.FlowFileRecord; |
| 23 | +import org.apache.nifi.controller.repository.FlowFileRepository; |
| 24 | +import org.apache.nifi.controller.repository.RepositoryRecord; |
| 25 | +import org.apache.nifi.controller.repository.RepositoryRecordType; |
| 26 | +import org.apache.nifi.controller.repository.StandardFlowFileRecord; |
| 27 | +import org.apache.nifi.controller.repository.claim.ContentClaim; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.mockito.Mockito; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.atomic.AtomicLong; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +public class TestConnectionUtils { |
| 42 | + |
| 43 | + private FlowFileRepository flowFileRepository; |
| 44 | + private ContentRepository contentRepository; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + public void setup() { |
| 48 | + flowFileRepository = Mockito.mock(FlowFileRepository.class); |
| 49 | + contentRepository = Mockito.mock(ContentRepository.class); |
| 50 | + |
| 51 | + final AtomicLong nextId = new AtomicLong(1000L); |
| 52 | + when(flowFileRepository.getNextFlowFileSequence()).thenAnswer(invocation -> nextId.getAndIncrement()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testCloneSingleDestinationProducesCreateRecord() { |
| 57 | + final FlowFileRecord flowFile = new StandardFlowFileRecord.Builder() |
| 58 | + .id(1L) |
| 59 | + .addAttribute("uuid", "test-uuid-1") |
| 60 | + .build(); |
| 61 | + |
| 62 | + final FlowFileQueue destinationQueue = Mockito.mock(FlowFileQueue.class); |
| 63 | + final Connection destination = mockConnection(destinationQueue); |
| 64 | + |
| 65 | + final FlowFileCloneResult result = ConnectionUtils.clone(flowFile, List.of(destination), flowFileRepository, contentRepository); |
| 66 | + |
| 67 | + final List<RepositoryRecord> records = result.getRepositoryRecords(); |
| 68 | + assertEquals(1, records.size()); |
| 69 | + final RepositoryRecord record = records.get(0); |
| 70 | + assertEquals(RepositoryRecordType.CREATE, record.getType()); |
| 71 | + assertEquals(destinationQueue, record.getDestination()); |
| 72 | + assertNotNull(record.getCurrent()); |
| 73 | + assertEquals(flowFile.getId(), record.getCurrent().getId()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testCloneMultipleDestinationsAllProduceCreateRecords() { |
| 78 | + final ContentClaim contentClaim = Mockito.mock(ContentClaim.class); |
| 79 | + final FlowFileRecord flowFile = new StandardFlowFileRecord.Builder() |
| 80 | + .id(1L) |
| 81 | + .addAttribute("uuid", "test-uuid-1") |
| 82 | + .contentClaim(contentClaim) |
| 83 | + .size(1024L) |
| 84 | + .build(); |
| 85 | + |
| 86 | + final FlowFileQueue queueOne = Mockito.mock(FlowFileQueue.class); |
| 87 | + final FlowFileQueue queueTwo = Mockito.mock(FlowFileQueue.class); |
| 88 | + final FlowFileQueue queueThree = Mockito.mock(FlowFileQueue.class); |
| 89 | + final List<Connection> destinations = List.of( |
| 90 | + mockConnection(queueOne), |
| 91 | + mockConnection(queueTwo), |
| 92 | + mockConnection(queueThree)); |
| 93 | + |
| 94 | + final FlowFileCloneResult result = ConnectionUtils.clone(flowFile, destinations, flowFileRepository, contentRepository); |
| 95 | + |
| 96 | + final List<RepositoryRecord> records = result.getRepositoryRecords(); |
| 97 | + assertEquals(destinations.size(), records.size()); |
| 98 | + |
| 99 | + // Each record produced by clone() represents a FlowFile being introduced to the repository, |
| 100 | + // whether it is the original routed FlowFile or a sibling clone. |
| 101 | + assertTrue(records.stream().allMatch(record -> record.getType() == RepositoryRecordType.CREATE)); |
| 102 | + |
| 103 | + // The clones (one per additional destination beyond the first) should each have triggered a |
| 104 | + // claimant count increment on the shared Content Claim. |
| 105 | + Mockito.verify(contentRepository, Mockito.times(destinations.size() - 1)).incrementClaimaintCount(contentClaim); |
| 106 | + |
| 107 | + final List<FlowFileQueue> destinationQueues = new ArrayList<>(); |
| 108 | + for (final RepositoryRecord record : records) { |
| 109 | + destinationQueues.add(record.getDestination()); |
| 110 | + } |
| 111 | + assertTrue(destinationQueues.contains(queueOne)); |
| 112 | + assertTrue(destinationQueues.contains(queueTwo)); |
| 113 | + assertTrue(destinationQueues.contains(queueThree)); |
| 114 | + } |
| 115 | + |
| 116 | + private Connection mockConnection(final FlowFileQueue queue) { |
| 117 | + final Connection connection = Mockito.mock(Connection.class); |
| 118 | + when(connection.getFlowFileQueue()).thenReturn(queue); |
| 119 | + return connection; |
| 120 | + } |
| 121 | +} |
0 commit comments