forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hotfix][cep] Added test harness output asserter
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/utils/OutputAsserter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.cep.utils; | ||
|
||
import org.apache.flink.streaming.api.watermark.Watermark; | ||
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; | ||
import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness; | ||
|
||
import java.util.Queue; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Asserter for output from {@link OneInputStreamOperatorTestHarness} | ||
*/ | ||
public class OutputAsserter { | ||
|
||
private final Queue<?> output; | ||
|
||
private OutputAsserter(Queue<?> output) { | ||
this.output = output; | ||
} | ||
|
||
public static OutputAsserter assertOutput(Queue<?> output) { | ||
return new OutputAsserter(output); | ||
} | ||
|
||
private AssertionError fail(Object record) { | ||
return new AssertionError("Received unexpected element: " + record); | ||
} | ||
|
||
public <T> OutputAsserter nextElementEquals(T expected) { | ||
final Object record = output.poll(); | ||
final Object actual; | ||
if (record instanceof StreamRecord) { | ||
// This is in case we assert side output | ||
actual = ((StreamRecord) record).getValue(); | ||
} else { | ||
// This is in case we assert the main output | ||
actual = record; | ||
} | ||
assertThat(actual, is(expected)); | ||
return this; | ||
} | ||
|
||
public void hasNoMoreElements() { | ||
assertTrue(output.isEmpty()); | ||
} | ||
|
||
public OutputAsserter watermarkEquals(long timestamp) { | ||
Object record = output.poll(); | ||
if (record instanceof Watermark) { | ||
Watermark watermark = (Watermark) record; | ||
assertThat(watermark.getTimestamp(), is(timestamp)); | ||
} else { | ||
throw fail(record); | ||
} | ||
return this; | ||
} | ||
} |