Search in sources :

Example 1 with FlowFileAttributesSerializer

use of org.apache.nifi.processors.standard.util.FlowFileAttributesSerializer in project nifi by apache.

the class TestWaitNotifyProtocol method testNiFiVersionUpgrade.

/**
 * Test migration across NiFi version upgrade.
 * Old version of Wait/Notify processors use FlowFileAttributesSerializer for cache entries.
 * New version uses StringSerializer. WaitNotifyProtocol should be able to migrate old cache entries.
 */
@Test
public void testNiFiVersionUpgrade() throws Exception {
    doAnswer(successfulReplace).when(cache).replace(any(), any(), any());
    // Simulate old cache entry.
    final FlowFileAttributesSerializer attributesSerializer = new FlowFileAttributesSerializer();
    final Map<String, String> cachedAttributes = new HashMap<>();
    cachedAttributes.put("key1", "value1");
    cachedAttributes.put("key2", "value2");
    cachedAttributes.put("key3", "value3");
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    attributesSerializer.serialize(cachedAttributes, bos);
    final String signalId = "old-entry";
    cacheEntries.put(signalId, new AtomicCacheEntry<>(signalId, new String(bos.toByteArray(), StandardCharsets.UTF_8), 0L));
    final WaitNotifyProtocol protocol = new WaitNotifyProtocol(cache);
    final Signal signal = protocol.getSignal(signalId);
    assertEquals(1, signal.getCount(DEFAULT_COUNT_NAME));
    assertEquals("value1", signal.getAttributes().get("key1"));
    assertEquals("value2", signal.getAttributes().get("key2"));
    assertEquals("value3", signal.getAttributes().get("key3"));
    cacheEntries.put(signalId, new AtomicCacheEntry<>(signalId, "UNSUPPORTED_FORMAT", 0L));
    try {
        protocol.getSignal(signalId);
        fail("Should fail since cached value was not in expected format.");
    } catch (DeserializationException e) {
    }
}
Also used : Signal(org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal) HashMap(java.util.HashMap) ByteArrayOutputStream(org.apache.activemq.util.ByteArrayOutputStream) DeserializationException(org.apache.nifi.distributed.cache.client.exception.DeserializationException) FlowFileAttributesSerializer(org.apache.nifi.processors.standard.util.FlowFileAttributesSerializer) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 ByteArrayOutputStream (org.apache.activemq.util.ByteArrayOutputStream)1 DeserializationException (org.apache.nifi.distributed.cache.client.exception.DeserializationException)1 Signal (org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal)1 FlowFileAttributesSerializer (org.apache.nifi.processors.standard.util.FlowFileAttributesSerializer)1 Test (org.junit.Test)1