Skip to content

Commit

Permalink
Explicit deviceId check (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu committed Nov 30, 2022
1 parent d6e3d64 commit 77b5ba6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pubber/.idea/runConfigurations/Pubber.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pubber/src/main/java/daq/pubber/Pubber.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ private static void singularPubber(String[] args) throws InterruptedException {
return true;
});
} catch (Exception e) {
new RuntimeException("While starting singular pubber", e).printStackTrace();
if (pubber != null) {
pubber.terminate();
}
throw new RuntimeException("While starting singular pubber", e);
}
}

Expand Down Expand Up @@ -361,6 +361,10 @@ private void initializeDevice() {
if (configuration.endpoint == null) {
configuration.endpoint = siteModel.makeEndpointConfig(configuration.projectId, deviceId);
}
if (!siteModel.allDeviceIds().contains(configuration.deviceId)) {
throw new IllegalArgumentException(
"Device ID " + configuration.deviceId + " not found in site model");
}
processDeviceMetadata(siteModel.getMetadata(configuration.deviceId));
} else if (pubSubClient != null) {
pullDeviceMessage();
Expand Down
33 changes: 33 additions & 0 deletions pubber/src/test/java/daq/pubber/PubberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package daq.pubber;

import com.google.common.collect.ImmutableList;
import java.util.List;
import org.junit.Test;

/**
* Unit tests for Pubber.
*/
public class PubberTest extends TestBase {

private static final String TEST_PROJECT = "test-project";
private static final String TEST_SITE = "../sites/udmi_site_model";
private static final String BAD_DEVICE = "ASHDQWHD";
private static final String SERIAL_NO = "18217398172";

@Test
public void missingDevice() {
try {
List<String> args = ImmutableList.of(TEST_PROJECT, TEST_SITE, BAD_DEVICE, SERIAL_NO);
Pubber.main(args.toArray(new String[0]));
} catch (Throwable e) {
while (e != null) {
String message = e.getMessage();
if (message.contains(BAD_DEVICE)) {
return;
}
e = e.getCause();
}
}
throw new RuntimeException("No exception thrown for bad device");
}
}
2 changes: 0 additions & 2 deletions pubber/src/test/java/daq/pubber/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static daq.pubber.ListPublisher.getMessageString;

import org.checkerframework.checker.units.qual.A;
import udmi.schema.Auth_provider;
import udmi.schema.DiscoveryCommand;
import udmi.schema.EndpointConfiguration;
import udmi.schema.PubberConfiguration;
Expand Down

0 comments on commit 77b5ba6

Please sign in to comment.