|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * You may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + ******************************************************************************/ |
| 19 | + |
| 20 | +package org.eclipse.microprofile.reactive.streams.tck; |
| 21 | + |
| 22 | +import org.eclipse.microprofile.reactive.streams.GraphAccessor; |
| 23 | +import org.eclipse.microprofile.reactive.streams.ReactiveStreams; |
| 24 | +import org.eclipse.microprofile.reactive.streams.spi.Graph; |
| 25 | +import org.eclipse.microprofile.reactive.streams.spi.Stage; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.testng.Assert.assertEquals; |
| 32 | +import static org.testng.Assert.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Test for the GraphAccessor class. |
| 36 | + * |
| 37 | + * This does not need an implementation of the engine to verify it. |
| 38 | + */ |
| 39 | +public class GraphAccessorVerification { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void buildGraphForPublisherShouldProduceTheCorrectGraph() { |
| 43 | + Graph graph = GraphAccessor.buildGraphFor(ReactiveStreams.of(1).map(i -> i * 2)); |
| 44 | + assertEquals(2, graph.getStages().size()); |
| 45 | + List<Stage> stages = new ArrayList<>(graph.getStages()); |
| 46 | + assertInstanceOf(stages.get(0), Stage.Of.class); |
| 47 | + assertInstanceOf(stages.get(1), Stage.Map.class); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void buildGraphForProcessorShouldProduceTheCorrectGraph() { |
| 52 | + Graph graph = GraphAccessor.buildGraphFor(ReactiveStreams.<Integer>builder().filter(i -> i > 10).takeWhile(i -> i < 20)); |
| 53 | + assertEquals(2, graph.getStages().size()); |
| 54 | + List<Stage> stages = new ArrayList<>(graph.getStages()); |
| 55 | + assertInstanceOf(stages.get(0), Stage.Filter.class); |
| 56 | + assertInstanceOf(stages.get(1), Stage.TakeWhile.class); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void buildGraphForSubscriberShouldProduceTheCorrectGraph() { |
| 61 | + Graph graph = GraphAccessor.buildGraphFor(ReactiveStreams.<Integer>builder().limit(10).toList()); |
| 62 | + assertEquals(2, graph.getStages().size()); |
| 63 | + List<Stage> stages = new ArrayList<>(graph.getStages()); |
| 64 | + assertInstanceOf(stages.get(0), Stage.Limit.class); |
| 65 | + assertInstanceOf(stages.get(1), Stage.Collect.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void buildGraphForCompletionRunnerShouldProduceTheCorrectGraph() { |
| 70 | + Graph graph = GraphAccessor.buildGraphFor(ReactiveStreams.failed(new Exception()).distinct().cancel()); |
| 71 | + assertEquals(3, graph.getStages().size()); |
| 72 | + List<Stage> stages = new ArrayList<>(graph.getStages()); |
| 73 | + assertInstanceOf(stages.get(0), Stage.Failed.class); |
| 74 | + assertInstanceOf(stages.get(1), Stage.Distinct.class); |
| 75 | + assertEquals(Stage.Cancel.INSTANCE, stages.get(2)); |
| 76 | + } |
| 77 | + |
| 78 | + private static void assertInstanceOf(Object obj, Class<?> clazz) { |
| 79 | + assertTrue(clazz.isInstance(obj), obj + " is not a an instance of " + clazz); |
| 80 | + } |
| 81 | +} |
0 commit comments