This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 617df2f RestClient tests.
617df2f is described below
commit 617df2f5508becc88733089cefa60d8b29b0b662
Author: JamesBognar <[email protected]>
AuthorDate: Thu Jul 2 09:43:21 2020 -0400
RestClient tests.
---
.../org/apache/juneau/assertions/Assertions.java | 53 ++++++++
.../juneau/assertions/ByteArrayAssertion.java | 74 +++++++++++
.../assertions/FluentByteArrayAssertion.java | 140 +++++++++++++++++++++
.../juneau/assertions/FluentObjectAssertion.java | 2 +-
.../assertions/FluentThrowableAssertion.java | 6 +-
.../client2/RestClient_Response_Body_Test.java | 12 ++
.../apache/juneau/rest/client2/RestResponse.java | 28 +++++
.../juneau/rest/client2/RestResponseBody.java | 31 +++++
8 files changed, 340 insertions(+), 6 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
index f9aab40..68a10f1 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/Assertions.java
@@ -12,8 +12,11 @@
//
***************************************************************************************************************************
package org.apache.juneau.assertions;
+import java.io.*;
import java.util.*;
+import org.apache.juneau.internal.*;
+
/**
* Main class for creation of assertions for testing.
*/
@@ -138,4 +141,54 @@ public class Assertions {
}
return assertThrowable(null);
}
+
+ /**
+ * Used for assertion calls against the contents of input streams.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the stream contains the string "foo".</jc>
+ * <jsm>assertStream</jsm>(myStream).hex().is(<js>"666F6F"</js>);
+ * </p>
+ *
+ * @param is The input stream being wrapped.
+ * @return A new {@link ByteArrayAssertion} object. Never
<jk>null</jk>.
+ * @throws IOException If thrown while reading contents from stream.
+ */
+ public static ByteArrayAssertion assertStream(InputStream is) throws
IOException {
+ return new ByteArrayAssertion(is == null ? null :
IOUtils.readBytes(is));
+ }
+
+ /**
+ * Used for assertion calls against byte arrays.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the byte array contains the string
"foo".</jc>
+ * <jsm>assertBytes</jsm>(myBytes).hex().is(<js>"666F6F"</js>);
+ * </p>
+ *
+ * @param bytes The byte array being wrapped.
+ * @return A new {@link ByteArrayAssertion} object. Never
<jk>null</jk>.
+ */
+ public static ByteArrayAssertion assertBytes(byte[] bytes) {
+ return new ByteArrayAssertion(bytes);
+ }
+
+ /**
+ * Used for assertion calls against the contents of readers.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates the throwable message or one of the parent
messages contain 'Foobar'.</jc>
+ * <jsm>assertReader</jsm>(myReader).is(<js>"foo"</js>);
+ * </p>
+ *
+ * @param r The reader being wrapped.
+ * @return A new {@link StringAssertion} object. Never <jk>null</jk>.
+ * @throws IOException If thrown while reading contents from reader.
+ */
+ public static StringAssertion assertReader(Reader r) throws IOException
{
+ return new StringAssertion(r == null ? null : IOUtils.read(r));
+ }
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/ByteArrayAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/ByteArrayAssertion.java
new file mode 100644
index 0000000..7d6697f
--- /dev/null
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/ByteArrayAssertion.java
@@ -0,0 +1,74 @@
+//
***************************************************************************************************************************
+// * 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.juneau.assertions;
+
+import org.apache.juneau.internal.*;
+
+/**
+ * Used for assertion calls against byte arrays.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates the byte array contains the string "foo".</jc>
+ * <jsm>assertBytes</jsm>(myByteArray).hex().is(<js>"666F6F"</js>);
+ * </p>
+ */
+@FluentSetters(returns="ByteArrayAssertion")
+public class ByteArrayAssertion extends
FluentByteArrayAssertion<ByteArrayAssertion> {
+
+ /**
+ * Creator.
+ *
+ * @param contents The contents being wrapped.
+ * @return A new {@link ByteArrayAssertion} object.
+ */
+ public static ByteArrayAssertion create(byte[] contents) {
+ return new ByteArrayAssertion(contents);
+ }
+
+ /**
+ * Creator.
+ *
+ * @param contents The contents being wrapped.
+ */
+ public ByteArrayAssertion(byte[] contents) {
+ super(contents, null);
+ }
+
+ @Override
+ protected ByteArrayAssertion returns() {
+ return this;
+ }
+
+ // <FluentSetters>
+
+ @Override /* GENERATED - Assertion */
+ public ByteArrayAssertion msg(String msg, Object...args) {
+ super.msg(msg, args);
+ return this;
+ }
+
+ @Override /* GENERATED - Assertion */
+ public ByteArrayAssertion stderr() {
+ super.stderr();
+ return this;
+ }
+
+ @Override /* GENERATED - Assertion */
+ public ByteArrayAssertion stdout() {
+ super.stdout();
+ return this;
+ }
+
+ // </FluentSetters>
+}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
new file mode 100644
index 0000000..b5ad79a
--- /dev/null
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
@@ -0,0 +1,140 @@
+//
***************************************************************************************************************************
+// * 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.juneau.assertions;
+
+import static org.apache.juneau.internal.StringUtils.*;
+
+import java.nio.charset.*;
+
+import org.apache.juneau.internal.*;
+
+/**
+ * Used for fluent assertion calls.
+ *
+ * @param <R> The return type.
+ */
+@FluentSetters(returns="FluentByteArrayAssertion<R>")
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public class FluentByteArrayAssertion<R> extends FluentAssertion<R> {
+
+ private byte[] contents;
+
+ /**
+ * Constructor.
+ *
+ * @param contents The byte array being tested.
+ * @param returns The object to return after the test.
+ */
+ public FluentByteArrayAssertion(byte[] contents, R returns) {
+ super(returns);
+ this.contents = contents;
+ }
+
+ /**
+ * Converts this byte array to a UTF-8 encoded string and returns it as
a new assertion.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the specified byte array contains the
string "foobar".</jc>
+ *
<jsm>assertBytes<jsm>(myByteArray).string().is(<js>"foobar"</js>);
+ * </p>
+ *
+ * @return A new fluent string assertion.
+ */
+ public FluentStringAssertion<R> string() {
+ return string(IOUtils.UTF8);
+ }
+
+ /**
+ * Converts this byte array to a string and returns it as a new
assertion.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the specified byte array contains the
string "foobar" encoded in ASCII.</jc>
+ *
<jsm>assertBytes<jsm>(myByteArray).string(<js>"iso8859-1"</js>).is(<js>"foobar"</js>);
+ * </p>
+ *
+ * @param cs The charset to use to decode the string.
+ * @return A new fluent string assertion.
+ */
+ public FluentStringAssertion<R> string(Charset cs) {
+ return new FluentStringAssertion(contents == null ? null : new
String(contents, cs), this);
+ }
+
+ /**
+ * Converts this byte array to a base-64 encoded string and returns it
as a new assertion.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the specified byte array contains the
string "foo".</jc>
+ * <jsm>assertBytes<jsm>(myByteArray).base64().is(<js>"Zm9v"</js>);
+ * </p>
+ *
+ * @return A new fluent string assertion.
+ */
+ public FluentStringAssertion<R> base64() {
+ return new FluentStringAssertion(contents == null ? null :
base64Encode(contents), this);
+ }
+
+ /**
+ * Converts this byte array to hexadecimal and returns it as a new
assertion.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the specified byte array contains the
string "foo".</jc>
+ * <jsm>assertBytes<jsm>(myByteArray).hex().is(<js>"666F6F"</js>);
+ * </p>
+ *
+ * @return A new string consisting of hexadecimal characters.
+ */
+ public FluentStringAssertion<R> hex() {
+ return new FluentStringAssertion(contents == null ? null :
toHex(contents), this);
+ }
+
+ /**
+ * Converts this byte array to spaced hexadecimal and returns it as a
new assertion.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates that the specified byte array contains the
string "foo".</jc>
+ * <jsm>assertBytes<jsm>(myByteArray).spacedHex().is(<js>"66 6F
6F"</js>);
+ * </p>
+ *
+ * @return A new string consisting of hexadecimal characters.
+ */
+ public FluentStringAssertion<R> spacedHex() {
+ return new FluentStringAssertion(contents == null ? null :
toSpacedHex(contents), this);
+ }
+
+ // <FluentSetters>
+
+ @Override /* GENERATED - Assertion */
+ public FluentByteArrayAssertion<R> msg(String msg, Object...args) {
+ super.msg(msg, args);
+ return this;
+ }
+
+ @Override /* GENERATED - Assertion */
+ public FluentByteArrayAssertion<R> stderr() {
+ super.stderr();
+ return this;
+ }
+
+ @Override /* GENERATED - Assertion */
+ public FluentByteArrayAssertion<R> stdout() {
+ super.stdout();
+ return this;
+ }
+
+ // </FluentSetters>
+}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
index 5998ee2..5783a64 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
@@ -25,6 +25,7 @@ import org.apache.juneau.serializer.*;
* @param <R> The return type.
*/
@FluentSetters(returns="FluentObjectAssertion<R>")
+@SuppressWarnings({ "rawtypes", "unchecked" })
public class FluentObjectAssertion<R> extends FluentAssertion<R> {
private final Object value;
@@ -98,7 +99,6 @@ public class FluentObjectAssertion<R> extends
FluentAssertion<R> {
* @param ws The serializer to use to convert the object to text.
* @return A new fluent string assertion.
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
public FluentStringAssertion<R> serialized(WriterSerializer ws) {
try {
String s = ws.serialize(this.value);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
index b34401c..feb4782 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
@@ -22,6 +22,7 @@ import org.apache.juneau.internal.*;
* @param <R> The return type.
*/
@FluentSetters(returns="FluentThrowableAssertion<R>")
+@SuppressWarnings({ "rawtypes", "unchecked" })
public class FluentThrowableAssertion<R> extends FluentAssertion<R> {
private final Throwable value;
@@ -159,7 +160,6 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
*
* @return An assertion against the throwable message. Never
<jk>null</jk>.
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
public FluentStringAssertion<R> message() {
return new FluentStringAssertion(value == null ? null :
value.getMessage(), this);
}
@@ -175,7 +175,6 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
*
* @return An assertion against the throwable localized message. Never
<jk>null</jk>.
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
public FluentStringAssertion<R> localizedMessage() {
return new FluentStringAssertion(value == null ? null :
value.getLocalizedMessage(), this);
}
@@ -191,7 +190,6 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
*
* @return An assertion against the throwable stacktrace. Never
<jk>null</jk>.
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
public FluentStringAssertion<R> stackTrace() {
return new FluentStringAssertion(value == null ? null :
StringUtils.getStackTrace(value), this);
}
@@ -207,7 +205,6 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
*
* @return An assertion against the caused-by. Never <jk>null</jk>.
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
public FluentThrowableAssertion<R> causedBy() {
return new FluentThrowableAssertion(value == null ? null :
value.getCause(), this);
}
@@ -224,7 +221,6 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @param throwableClass The class type to search for in the caused-by
chain.
* @return An assertion against the caused-by throwable. Never
<jk>null</jk>.
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
public FluentThrowableAssertion<R> find(Class<?> throwableClass) {
Throwable t = value;
while (t != null) {
diff --git
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
index 85c6c1c..f451923 100644
---
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
+++
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
@@ -44,6 +44,10 @@ public class RestClient_Response_Body_Test {
public InputStream postEcho(InputStream is) {
return is;
}
+ @RestMethod
+ public ABean getBean() {
+ return bean;
+ }
}
@Test
@@ -60,6 +64,14 @@ public class RestClient_Response_Body_Test {
assertThrown(()->x.post("/echo",bean).run().getBody().parser(XmlParser.DEFAULT).assertObject(ABean.class)).contains("ParseError
at [row,col]:[1,1]");
}
+ @Test
+ public void a03_asInputStream() throws Exception {
+ RestResponse x = client().build().get("/bean").run();
+ InputStream is = x.getBody().asInputStream();
+ assertStream(is).string().is("{f:1}");
+
assertThrown(()->x.getBody().asInputStream()).contains("Response has already
been consumed.");
+ }
+
//------------------------------------------------------------------------------------------------------------------
// Helper methods.
//------------------------------------------------------------------------------------------------------------------
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponse.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponse.java
index 46ac27a..d449953 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponse.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponse.java
@@ -519,6 +519,34 @@ public class RestResponse implements HttpResponse {
}
/**
+ * Provides the ability to perform fluent-style assertions on the bytes
of the response body.
+ *
+ * <h5 class='section'>Examples:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates the response body equals the text "foo".</jc>
+ * client
+ * .get(<jsf>URL</jsf>)
+ * .run()
+ * .assertBodyBytes().hex().is(<js>"666F6F"</js>);
+ * </p>
+ *
+ * <ul class='notes'>
+ * <li>
+ * If no charset was found on the
<code>Content-Type</code> response header, <js>"UTF-8"</js> is assumed.
+ * <li>
+ * When using this method, the body is automatically
cached by calling the {@link RestResponseBody#cache()}.
+ * <li>
+ * The input stream is automatically closed after this
call.
+ * </ul>
+ *
+ * @return A new fluent assertion object.
+ * @throws RestCallException If REST call failed.
+ */
+ public FluentByteArrayAssertion<RestResponse> assertBodyBytes() throws
RestCallException {
+ return responseBody.cache().assertBytes();
+ }
+
+ /**
* Provides the ability to perform fluent-style assertions on this
response body.
*
* <p>
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
index efb6d47..323cc3b 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
@@ -1613,6 +1613,37 @@ public class RestResponseBody implements HttpEntity {
}
/**
+ * Provides the ability to perform fluent-style assertions on the bytes
of the response body.
+ *
+ * <p>
+ * This method is called directly from the {@link
RestResponse#assertBodyBytes()} method to instantiate a fluent assertions
object.
+ *
+ * <h5 class='section'>Examples:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Validates the response body equals the text "foo".</jc>
+ * client
+ * .get(<jsf>URL</jsf>)
+ * .run()
+ * .assertBodyBytes().hex().is(<js>"666F6F"</js>);
+ * </p>
+ *
+ * <ul class='notes'>
+ * <li>
+ * If no charset was found on the
<code>Content-Type</code> response header, <js>"UTF-8"</js> is assumed.
+ * <li>
+ * When using this method, the body is automatically
cached by calling the {@link RestResponseBody#cache()}.
+ * <li>
+ * The input stream is automatically closed after this
call.
+ * </ul>
+ *
+ * @return A new fluent assertion object.
+ * @throws RestCallException If REST call failed.
+ */
+ public FluentByteArrayAssertion<RestResponse> assertBytes() throws
RestCallException {
+ return new FluentByteArrayAssertion<>(asBytes(), response);
+ }
+
+ /**
* Provides the ability to perform fluent-style assertions on this
response body.
*
* <p>