/** * Validate that the file contains the specified content. * @param path file to validate * @param lines the expected content * @throws Exception on failure */ public static void validateFile(Path path, String[] lines) throws Exception { validateFile(path, lines, false); }
public String pattern() { return id.pattern(); } }
private String getStorePath(String storeLeaf) { return TestRepoPath.getPath("keystores/" + storeLeaf); }
@Test public void testRetainedFalse() throws Exception { Topology top = newTopology("testRetainedFalse"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean isRetained = false; List<String> expMsgsAsStr = msgsAsStr(topic, msgs); if (isRetained) expMsgsAsStr.add(0, (new Msg(retainedMsg, topic)).toString()); (topicArg, payload) -> { String m = new String(payload, StandardCharsets.UTF_8); return new Msg(m, topicArg); }); TStream<String> rcvdAsStr = rcvd.map(t -> t.toString()); completeAndValidate(clientId, top, rcvdAsStr, mgen, SEC_TIMEOUT, expMsgsAsStr.toArray(new String[0]));
@Test public void testGenericPublish() throws Exception { Topology top = newTopology("testGenericPublish"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean retain = false; .map(t -> new Msg(t, topic)); t -> t.getTopic(), t -> serialize(t), t -> qos, (topicArg, payload) -> deserialize(payload)); TStream<String> rcvdAsStr = rcvd.map(t -> t.toString()); completeAndValidate(clientId, top, rcvdAsStr, mgen, SEC_TIMEOUT, expMsgsAsStr.toArray(new String[0]));
public void completeAndValidate(boolean ordered, String msg, Topology t, TStream<String> s, MsgGenerator mgen, int secTimeout, String... expected) throws Exception { s = s.filter(tuple -> tuple.matches(mgen.pattern())); s.sink(tuple -> System.out.println( String.format("[%s][%s] rcvd: %s", t.getName(), simpleTS(), tuple))); super.completeAndValidate(ordered, msg, t, s, secTimeout, expected); }
@Test public void testAutoClientId() throws Exception { Topology top = newTopology("testAutoClientId"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean retain = false; String topic = getMqttTopics()[0]; List<String> msgs = createMsgs(mgen, topic, getMsg1(), getMsg2()); TStream<String> s = PlumbingStreams.blockingOneShotDelay( top.collection(msgs), PUB_DELAY_MSEC, TimeUnit.MILLISECONDS); // Test with auto-generated clientId MqttConfig config = newConfig(getServerURI(), null/*clientId*/); MqttStreams mqtt = new MqttStreams(top, () -> config); mqtt.publish(s, topic, qos, retain); TStream<String> rcvd = mqtt.subscribe(topic, qos); completeAndValidate("some-auto-clientId", top, rcvd, mgen, SEC_TIMEOUT, msgs.toArray(new String[0])); }
@Test public void testTextFileReader() throws Exception { Topology t = newTopology("testTextFileReader"); String[] lines = getLines(); String[] ucLines = toUpperCase(lines); String[] allLines = concat(lines, ucLines); Path tempFile1 = FileUtil.createTempFile("test1", "txt", lines); Path tempFile2 = FileUtil.createTempFile("test2", "txt", ucLines); TStream<String> contents = FileStreams.textFileReader( t.strings(tempFile1.toAbsolutePath().toString(), tempFile2.toAbsolutePath().toString())); try { completeAndValidate("", t, contents, 10, allLines); } finally { tempFile1.toFile().delete(); tempFile2.toFile().delete(); } }
@Override public String apply(String t, byte[] payload) { String msg = new String(payload, StandardCharsets.UTF_8); // caution "retained" msgs from prior tests if (msg.matches(mgen.pattern())) { ++tupCnt; if (tupCnt == 1) throw new RuntimeException("TEST message2tuple thrown exception"); } return msg; } });
public MsgGenerator(String testName) { id = new MsgId(testName); } public String create(String topic, String baseContent) {
public String create(String topic, String baseContent) { return String.format("%s [for-topic=%s] %s", id.next(), topic, baseContent); } public String pattern() {
@Test public void testRetainedTrue() throws Exception { Topology top = newTopology("testRetainedTrue"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean isRetained = true; List<String> expMsgsAsStr = msgsAsStr(topic, msgs); if (isRetained) expMsgsAsStr.add(0, (new Msg(retainedMsg, topic)).toString()); (topicArg, payload) -> { String m = new String(payload, StandardCharsets.UTF_8); return new Msg(m, topicArg); }); TStream<String> rcvdAsStr = rcvd.map(t -> t.toString()); completeAndValidate(clientId, top, rcvdAsStr, mgen, SEC_TIMEOUT, expMsgsAsStr.toArray(new String[0]));
private String getCmdPath(String cmd) { return TestRepoPath.getPath(cmd); }
public boolean valid() { try { return FileUtil.validateFile(tempFile1, getLines(), true); } catch (Exception e) { return false; } } public Object getResult() { return "todo-files-lines"; }
private String retainTestSetup(boolean isRetained, MsgGenerator mgen) throws Exception { // publish a msg to [not] retain. Topology top = newTopology("retainTestSetup"+isRetained); int qos = 0; String clientId = newClientId(top.getName()); String topic = getMqttTopics()[0]; String retainedMsg = mgen.create(topic, isRetained ? "RETAIN-THIS" : "DO-NOT-RETAIN-THIS"); MqttConfig config = newConfig(getServerURI(), clientId+"-setup"); MqttStreams mqtt = new MqttStreams(top, () -> config); TStream<String> s = PlumbingStreams.blockingOneShotDelay( top.strings(retainedMsg), PUB_DELAY_MSEC, TimeUnit.MILLISECONDS); mqtt.publish(s, topic, qos, isRetained); TStream<String> rcvd = mqtt.subscribe(topic, qos); completeAndValidate(clientId, top, rcvd, mgen, SEC_TIMEOUT, retainedMsg); // N.B. the topology should be shutdown at this point but it isn't // and this topology will also receive and print the tuples // generated by the main portion of the test. // issue#46 return retainedMsg; }
@Test public void testNoFilesCreated() throws Exception { // complete before any files are generated Topology t = newTopology("testNoFilesCreated"); // establish a base path Path basePath = createTempFile("test1", "txt", new String[0]); // build expected results List<List<String>> expResults = Collections.emptyList(); TStream<String> s = t.events(eventSetup -> { /* no tuples generated */ }); FileStreams.textFileWriter(s, () -> basePath.toString()); completeAndValidateWriter(t, TMO_SEC, basePath, expResults); }
private String getStorePath(String storeLeaf) { return TestRepoPath.getPath("keystores/" + storeLeaf); }
protected String getKeystorePath(String storeLeaf) { return TestRepoPath.getPath("keystores/" + storeLeaf); }