QueueingStrategyFactory { public static QueueingStrategy buildQueueStrategy(JsonObject hookConfiguration) { QueueingStrategy queueingStrategy = new DefaultQueueingStrategy(); if(hookConfiguration == null || !hookConfiguration.containsKey(QUEUEING_STRATEGY_PROPERTY)){ return queueingStrategy; } Object queueingStrategyConfigObj = hookConfiguration.getValue(QUEUEING_STRATEGY_PROPERTY); if(!(queueingStrategyConfigObj instanceof JsonObject)){ LOG.warn("Invalid 'queueingStrategy' configuration found: " + queueingStrategyConfigObj + ". Using DefaultQueueingStrategy instead"); return queueingStrategy; } JsonObject queueingStrategyConfig = (JsonObject) queueingStrategyConfigObj; Strategy strategy = Strategy.fromString(queueingStrategyConfig.getString("type")); if(strategy == null){ LOG.warn("Invalid 'queueingStrategy' configuration found: " + queueingStrategyConfig.encode() + ". Using DefaultQueueingStrategy instead"); return queueingStrategy; } switch (strategy){ case DISCARD_PAYLOAD: queueingStrategy = new DiscardPayloadQueueingStrategy(); break; case REDUCED_PROPAGATION: queueingStrategy = buildReducedPropagationQueueingStrategy(queueingStrategyConfig); break; default: LOG.warn("Unknown strategy '"+strategy+"'. Using DefaultQueueingStrategy instead"); } return queueingStrategy; } private  QueueingStrategyFactory(); static QueueingStrategy buildQueueStrategy(JsonObject hookConfiguration);  }
@Test public void testDefaultQueueingStrategy(){ assertThat(QueueingStrategyFactory.buildQueueStrategy(null), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(null)), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(null).put("queueingStrategy", "not_a_jsonobject")), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(null).put("queueingStrategy", new JsonArray())), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(null).put("queueingStrategy", 123)), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "unknownType"))), instanceOf(DefaultQueueingStrategy.class)); }
@Test public void testDiscardPayloadQueueingStrategy(){ assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "discardPayload"))), instanceOf(DiscardPayloadQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "discardpayload"))), instanceOf(DiscardPayloadQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "DISCARDPAYLOAD"))), instanceOf(DiscardPayloadQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("typeXX", "discardPayload"))), instanceOf(DefaultQueueingStrategy.class)); }
@Test public void testReducedPropagationQueueingStrategy(){ QueueingStrategy queueingStrategy = QueueingStrategyFactory.buildQueueStrategy( buildHookConfig(new JsonObject().put("type", "reducedPropagation").put("intervalMs", 22))); assertThat(queueingStrategy, instanceOf(ReducedPropagationQueueingStrategy.class)); assertEquals(22, ((ReducedPropagationQueueingStrategy)queueingStrategy).getPropagationIntervalMs()); queueingStrategy = QueueingStrategyFactory.buildQueueStrategy( buildHookConfig(new JsonObject().put("type", "reducedpropagation").put("intervalMs", 888))); assertThat(queueingStrategy, instanceOf(ReducedPropagationQueueingStrategy.class)); assertEquals(888, ((ReducedPropagationQueueingStrategy)queueingStrategy).getPropagationIntervalMs()); queueingStrategy = QueueingStrategyFactory.buildQueueStrategy( buildHookConfig(new JsonObject().put("type", "REDUCEDPROPAGATION").put("intervalMs", 999))); assertThat(queueingStrategy, instanceOf(ReducedPropagationQueueingStrategy.class)); assertEquals(999, ((ReducedPropagationQueueingStrategy)queueingStrategy).getPropagationIntervalMs()); queueingStrategy = QueueingStrategyFactory.buildQueueStrategy( buildHookConfig(new JsonObject().put("type", "REDUCEDPROPAGATION").put("intervalMs", 999999999999999999L))); assertThat(queueingStrategy, instanceOf(ReducedPropagationQueueingStrategy.class)); assertEquals(999999999999999999L, ((ReducedPropagationQueueingStrategy)queueingStrategy).getPropagationIntervalMs()); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "reducedPropagation"))), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("typeXX", "reducedPropagation").put("intervalMs", 123))), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "reducedPropagation").put("intervalMs", "234"))), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "reducedPropagation").put("intervalXX", 234))), instanceOf(DefaultQueueingStrategy.class)); assertThat(QueueingStrategyFactory.buildQueueStrategy(buildHookConfig(new JsonObject().put("type", "reducedPropagation").put("intervalMs", new JsonObject()))), instanceOf(DefaultQueueingStrategy.class)); }