Skip to content

Commit 4d93685

Browse files
authored
Fix UdpStreamMonitor update callback to set all properties (#962)
1 parent 186207e commit 4d93685

3 files changed

Lines changed: 93 additions & 22 deletions

File tree

catalog/video/video-mpegts-stream/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,17 @@
287287
<limit implementation="org.codice.jacoco.LenientLimit">
288288
<counter>INSTRUCTION</counter>
289289
<value>COVEREDRATIO</value>
290-
<minimum>0.72</minimum>
290+
<minimum>0.78</minimum>
291291
</limit>
292292
<limit implementation="org.codice.jacoco.LenientLimit">
293293
<counter>BRANCH</counter>
294294
<value>COVEREDRATIO</value>
295-
<minimum>0.64</minimum>
295+
<minimum>0.70</minimum>
296296
</limit>
297297
<limit implementation="org.codice.jacoco.LenientLimit">
298298
<counter>COMPLEXITY</counter>
299299
<value>COVEREDRATIO</value>
300-
<minimum>0.65</minimum>
300+
<minimum>0.66</minimum>
301301
</limit>
302302
</limits>
303303
</rule>

catalog/video/video-mpegts-stream/src/main/java/org/codice/alliance/video/stream/mpegts/UdpStreamMonitor.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.URI;
3636
import java.net.URISyntaxException;
3737
import java.net.UnknownHostException;
38+
import java.util.Arrays;
3839
import java.util.Collections;
3940
import java.util.Date;
4041
import java.util.List;
@@ -109,6 +110,12 @@ public class UdpStreamMonitor implements StreamMonitor {
109110

110111
public static final String METATYPE_NETWORK_INTERFACE = "networkInterface";
111112

113+
public static final String METATYPE_START_IMMEDIATELY = "startImmediately";
114+
115+
public static final String METATYPE_STREAM_ID = "streamId";
116+
117+
public static final String METATYPE_ADDITIONAL_PROPERTIES = "additionalProperties";
118+
112119
public static final String STREAM_ID = "streamId";
113120

114121
static final int MONITORED_PORT_MIN = 1;
@@ -428,38 +435,46 @@ public void updateCallback(Map<String, Object> properties) {
428435
LOGGER.debug("--updateCallback-- properties={}", properties);
429436
if (properties != null) {
430437

431-
if (!checkMetaTypeClass(properties, METATYPE_MONITORED_ADDRESS, String.class)) {
438+
if (isRequiredPropertyWrongType(properties, METATYPE_MONITORED_ADDRESS, String.class)) {
432439
return;
433440
}
434441

435-
if (properties.containsKey(METATYPE_NETWORK_INTERFACE)
436-
&& !checkMetaTypeClass(properties, METATYPE_NETWORK_INTERFACE, String.class)) {
442+
if (isOptionalPropertyWrongType(properties, METATYPE_NETWORK_INTERFACE, String.class)) {
437443
return;
438444
}
439445

440-
if (!checkMetaTypeClass(properties, METATYPE_BYTE_COUNT_ROLLOVER_CONDITION, Integer.class)) {
446+
if (isRequiredPropertyWrongType(
447+
properties, METATYPE_BYTE_COUNT_ROLLOVER_CONDITION, Integer.class)) {
448+
return;
449+
}
450+
if (isRequiredPropertyWrongType(
451+
properties, METATYPE_ELAPSED_TIME_ROLLOVER_CONDITION, Long.class)) {
452+
return;
453+
}
454+
if (isRequiredPropertyWrongType(properties, METATYPE_FILENAME_TEMPLATE, String.class)) {
441455
return;
442456
}
443-
if (!checkMetaTypeClass(properties, METATYPE_ELAPSED_TIME_ROLLOVER_CONDITION, Long.class)) {
457+
if (isRequiredPropertyWrongType(properties, METATYPE_PARENT_TITLE, String.class)) {
444458
return;
445459
}
446-
if (!checkMetaTypeClass(properties, METATYPE_FILENAME_TEMPLATE, String.class)) {
460+
if (isRequiredPropertyWrongType(
461+
properties, METATYPE_METACARD_UPDATE_INITIAL_DELAY, Long.class)) {
447462
return;
448463
}
449-
if (!checkMetaTypeClass(properties, METATYPE_PARENT_TITLE, String.class)) {
464+
465+
if (isOptionalPropertyWrongType(properties, METATYPE_DISTANCE_TOLERANCE, Double.class)) {
450466
return;
451467
}
452-
if (!checkMetaTypeClass(properties, METATYPE_METACARD_UPDATE_INITIAL_DELAY, Long.class)) {
468+
469+
if (isOptionalPropertyWrongType(properties, METATYPE_STREAM_ID, String.class)) {
453470
return;
454471
}
455472

456-
if (!checkMetaTypeClass(properties, METATYPE_TITLE, String.class)) {
473+
if (isRequiredPropertyWrongType(properties, METATYPE_START_IMMEDIATELY, Boolean.class)) {
457474
return;
458475
}
459476

460-
if (properties.containsKey(METATYPE_DISTANCE_TOLERANCE)
461-
&& properties.get(METATYPE_DISTANCE_TOLERANCE) != null
462-
&& !checkMetaTypeClass(properties, METATYPE_DISTANCE_TOLERANCE, Double.class)) {
477+
if (isOptionalPropertyWrongType(properties, METATYPE_ADDITIONAL_PROPERTIES, String[].class)) {
463478
return;
464479
}
465480

@@ -473,22 +488,31 @@ public void updateCallback(Map<String, Object> properties) {
473488
setMetacardUpdateInitialDelay((Long) properties.get(METATYPE_METACARD_UPDATE_INITIAL_DELAY));
474489
setParentTitle((String) properties.get(METATYPE_PARENT_TITLE));
475490
setDistanceTolerance((Double) properties.get(METATYPE_DISTANCE_TOLERANCE));
491+
setStreamId((String) properties.get(METATYPE_STREAM_ID));
492+
setStartImmediately((Boolean) properties.get(METATYPE_START_IMMEDIATELY));
493+
setAdditionalProperties(
494+
Arrays.asList((String[]) properties.get(METATYPE_ADDITIONAL_PROPERTIES)));
476495

477496
init();
478497
}
479498
}
480499

481-
private boolean checkMetaTypeClass(
500+
private boolean isOptionalPropertyWrongType(
482501
Map<String, Object> properties, String fieldName, Class<?> clazz) {
483-
if (!properties.containsKey(fieldName)) {
484-
LOGGER.debug("the metatype id {} field was null", fieldName);
502+
if (properties.get(fieldName) == null) {
503+
LOGGER.debug("the metatype id {} field was null, but the field is optional", fieldName);
485504
return false;
486505
}
506+
return isRequiredPropertyWrongType(properties, fieldName, clazz);
507+
}
508+
509+
private boolean isRequiredPropertyWrongType(
510+
Map<String, Object> properties, String fieldName, Class<?> clazz) {
487511
if (!clazz.isInstance(properties.get(fieldName))) {
488512
LOGGER.debug("the metatype id {} field should be type {}", fieldName, clazz);
489-
return false;
513+
return true;
490514
}
491-
return true;
515+
return false;
492516
}
493517

494518
public String getMonitoredAddress() {
@@ -577,7 +601,7 @@ private Optional<Pair<NetworkInterface, InetAddress>> findLocalAddress(String in
577601

578602
if (networkIntf != null) {
579603
return Collections.list(networkIntf.getInetAddresses()).stream()
580-
.filter(inetAddress -> inetAddress instanceof Inet4Address)
604+
.filter(Inet4Address.class::isInstance)
581605
.map(inetAddress -> create(networkIntf, inetAddress))
582606
.findFirst();
583607
}

catalog/video/video-mpegts-stream/src/test/java/org/codice/alliance/video/stream/mpegts/UdpStreamMonitorTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
*/
1414
package org.codice.alliance.video.stream.mpegts;
1515

16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.Matchers.allOf;
18+
import static org.hamcrest.Matchers.hasEntry;
1619
import static org.hamcrest.Matchers.is;
17-
import static org.junit.Assert.assertThat;
1820
import static org.mockito.Mockito.mock;
1921
import static org.mockito.Mockito.verify;
22+
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
2023

2124
import ddf.catalog.CatalogFramework;
2225
import ddf.catalog.data.MetacardType;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
2328
import java.util.Collections;
29+
import java.util.HashMap;
2430
import java.util.List;
31+
import java.util.Map;
2532
import org.codice.alliance.video.stream.mpegts.filename.FilenameGenerator;
2633
import org.codice.alliance.video.stream.mpegts.netty.UdpStreamProcessor;
2734
import org.codice.alliance.video.stream.mpegts.rollover.RolloverCondition;
@@ -200,4 +207,44 @@ public void testSetFilenameGenerator() {
200207
udpStreamMonitor.setFilenameGenerator(filenameGenerator);
201208
verify(udpStreamProcessor).setFilenameGenerator(filenameGenerator);
202209
}
210+
211+
@Test
212+
public void testUpdateCallbackUpdatesAllProperties() throws URISyntaxException {
213+
final Map<String, Object> properties = new HashMap<>();
214+
properties.put(UdpStreamMonitor.METATYPE_TITLE, "Parent Metacard");
215+
properties.put(UdpStreamMonitor.METATYPE_STREAM_ID, "a1b2c3");
216+
properties.put(UdpStreamMonitor.METATYPE_MONITORED_ADDRESS, "udp://123.4.5.6:789");
217+
properties.put(UdpStreamMonitor.METATYPE_NETWORK_INTERFACE, "eth0");
218+
properties.put(UdpStreamMonitor.METATYPE_BYTE_COUNT_ROLLOVER_CONDITION, 25);
219+
properties.put(UdpStreamMonitor.METATYPE_ELAPSED_TIME_ROLLOVER_CONDITION, 30000L);
220+
properties.put(UdpStreamMonitor.METATYPE_FILENAME_TEMPLATE, "test-template");
221+
properties.put(UdpStreamMonitor.METATYPE_METACARD_UPDATE_INITIAL_DELAY, 5L);
222+
properties.put(UdpStreamMonitor.METATYPE_DISTANCE_TOLERANCE, 0.05);
223+
properties.put(UdpStreamMonitor.METATYPE_START_IMMEDIATELY, true);
224+
properties.put(
225+
UdpStreamMonitor.METATYPE_ADDITIONAL_PROPERTIES, new String[] {"foo=bar", "baz=bat"});
226+
227+
try {
228+
udpStreamMonitor.updateCallback(properties);
229+
} catch (StreamMonitorException e) {
230+
// expected because udpStreamProcessor.isReady() returns false
231+
}
232+
233+
assertThat(udpStreamMonitor.getTitle().orElse(null), is("Parent Metacard"));
234+
assertThat(udpStreamMonitor.getStreamId(), is("a1b2c3"));
235+
assertThat(udpStreamMonitor.getMonitoredAddress(), is("123.4.5.6"));
236+
assertThat(udpStreamMonitor.getStreamUri().orElse(null), is(new URI("udp://123.4.5.6:789")));
237+
assertThat(udpStreamMonitor.getNetworkInterface(), is("eth0"));
238+
assertThat(udpStreamMonitor.getByteCountRolloverCondition(), is(25));
239+
verify(udpStreamProcessor).setMegabyteCountRolloverCondition(25);
240+
assertThat(udpStreamMonitor.getElapsedTimeRolloverCondition(), is(30000L));
241+
verify(udpStreamProcessor).setElapsedTimeRolloverCondition(30000L);
242+
assertThat(udpStreamMonitor.getFileNameTemplate(), is("test-template"));
243+
verify(udpStreamProcessor).setFilenameTemplate("test-template");
244+
verify(udpStreamProcessor).setMetacardUpdateInitialDelay(5L);
245+
verify(udpStreamProcessor).setDistanceTolerance(0.05);
246+
assertThat(udpStreamMonitor.getStartImmediately(), is(true));
247+
verify(udpStreamProcessor)
248+
.setAdditionalProperties(argThat(allOf(hasEntry("foo", "bar"), hasEntry("baz", "bat"))));
249+
}
203250
}

0 commit comments

Comments
 (0)