| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MkContainer |
|
| 1.0;1 |
| 1 | /** | |
| 2 | * Copyright (c) 2011-2017, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.http.mock; | |
| 31 | ||
| 32 | import java.io.Closeable; | |
| 33 | import java.io.IOException; | |
| 34 | import java.net.URI; | |
| 35 | import java.util.Collection; | |
| 36 | import org.hamcrest.Matcher; | |
| 37 | ||
| 38 | /** | |
| 39 | * Mock version of Java Servlet container. | |
| 40 | * | |
| 41 | * <p>A convenient tool to test your application classes against a web | |
| 42 | * service. For example: | |
| 43 | * | |
| 44 | * <pre> MkContainer container = new MkGrizzlyContainer() | |
| 45 | * .next(new MkAnswer.Simple(200, "works fine!")) | |
| 46 | * .start(); | |
| 47 | * new JdkRequest(container.home()) | |
| 48 | * .header("Accept", "text/xml") | |
| 49 | * .fetch().as(RestResponse.class) | |
| 50 | * .assertStatus(200) | |
| 51 | * .assertBody(Matchers.equalTo("works fine!")); | |
| 52 | * MatcherAssert.assertThat( | |
| 53 | * container.take().method(), | |
| 54 | * Matchers.equalTo("GET") | |
| 55 | * ); | |
| 56 | * container.stop();</pre> | |
| 57 | * | |
| 58 | * <p>Keep in mind that container automatically reserves a new free TCP port | |
| 59 | * and works until JVM is shut down. The only way to stop it is to call | |
| 60 | * {@link #stop()}. | |
| 61 | * | |
| 62 | * <p>Since version 0.11 container implements {@link Closeable} and can be | |
| 63 | * used in try-with-resource block. | |
| 64 | * | |
| 65 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
| 66 | * @version $Id: 855ccc08658e715f7f69fe44c19cb8367362d2dc $ | |
| 67 | * @since 0.10 | |
| 68 | * @see <a href="http://www.rexsl.com/rexsl-test/example-mock-servlet.html">Examples</a> | |
| 69 | * @checkstyle TooManyMethods (200 lines) | |
| 70 | */ | |
| 71 | @SuppressWarnings("PMD.TooManyMethods") | |
| 72 | public interface MkContainer extends Closeable { | |
| 73 | ||
| 74 | /** | |
| 75 | * Give this answer on the next request. | |
| 76 | * @param answer Next answer to give | |
| 77 | * @return This object | |
| 78 | */ | |
| 79 | MkContainer next(MkAnswer answer); | |
| 80 | ||
| 81 | /** | |
| 82 | * Give this answer on the next request if the matcher condition is | |
| 83 | * satisfied. | |
| 84 | * @param answer Next answer to give | |
| 85 | * @param condition The condition to match | |
| 86 | * @return This object | |
| 87 | */ | |
| 88 | MkContainer next(MkAnswer answer, Matcher<MkQuery> condition); | |
| 89 | ||
| 90 | /** | |
| 91 | * Give this answer on the next request(s) if the matcher condition is | |
| 92 | * satisfied up to a certain number of requests. | |
| 93 | * @param answer Next answer to give | |
| 94 | * @param condition The condition to match | |
| 95 | * @param count Number of requests to match | |
| 96 | * @return This object | |
| 97 | */ | |
| 98 | MkContainer next(MkAnswer answer, Matcher<MkQuery> condition, int count); | |
| 99 | ||
| 100 | /** | |
| 101 | * Get the oldest request received | |
| 102 | * ({@link java.util.NoSuchElementException} | |
| 103 | * if no more elements in the list). | |
| 104 | * @return Request received | |
| 105 | */ | |
| 106 | MkQuery take(); | |
| 107 | ||
| 108 | /** | |
| 109 | * Get the oldest request received subject to the matching condition. | |
| 110 | * ({@link java.util.NoSuchElementException} if no elements satisfy the | |
| 111 | * condition). | |
| 112 | * @param matcher The matcher specifying the condition | |
| 113 | * @return Request received satisfying the matcher | |
| 114 | */ | |
| 115 | MkQuery take(Matcher<MkAnswer> matcher); | |
| 116 | ||
| 117 | /** | |
| 118 | * Get the all requests received satisfying the given matcher. | |
| 119 | * ({@link java.util.NoSuchElementException} if no elements satisfy the | |
| 120 | * condition). | |
| 121 | * @param matcher The matcher specifying the condition | |
| 122 | * @return Collection of all requests satisfying the matcher, ordered from | |
| 123 | * oldest to newest. | |
| 124 | */ | |
| 125 | Collection<MkQuery> takeAll(Matcher<MkAnswer> matcher); | |
| 126 | ||
| 127 | /** | |
| 128 | * How many queries we have left. | |
| 129 | * @return Total number of queries you can retrieve with {@link #take()} | |
| 130 | * @since 1.0 | |
| 131 | */ | |
| 132 | int queries(); | |
| 133 | ||
| 134 | /** | |
| 135 | * Start it on the first available TCP port. | |
| 136 | * @return This object | |
| 137 | * @throws IOException If fails | |
| 138 | */ | |
| 139 | MkContainer start() throws IOException; | |
| 140 | ||
| 141 | /** | |
| 142 | * Start it on a provided port. | |
| 143 | * @param prt The port where it should start listening | |
| 144 | * @return This object | |
| 145 | * @throws IOException If fails | |
| 146 | */ | |
| 147 | MkContainer start(int prt) throws IOException; | |
| 148 | ||
| 149 | /** | |
| 150 | * Stop container. | |
| 151 | */ | |
| 152 | void stop(); | |
| 153 | ||
| 154 | /** | |
| 155 | * Get its home. | |
| 156 | * @return URI of the started container | |
| 157 | */ | |
| 158 | URI home(); | |
| 159 | ||
| 160 | } |