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 51bba12 RestClient tests
51bba12 is described below
commit 51bba12cf3296092faa5ca7be19a25095d4c1220
Author: JamesBognar <[email protected]>
AuthorDate: Tue Mar 24 17:28:20 2020 -0400
RestClient tests
---
.../java/org/apache/juneau/ContextBuilder.java | 28 ++++
.../org/apache/juneau/PropertyStoreBuilder.java | 151 ++++++++++++++++++++-
.../juneau/rest/client2/RestClientBuilderTest.java | 19 +++
.../juneau/rest/client2/RestClientBuilder.java | 14 +-
.../apache/juneau/rest/client2/RestRequest.java | 8 +-
5 files changed, 211 insertions(+), 9 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
index 6809ff2..9fa7409 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ContextBuilder.java
@@ -309,6 +309,34 @@ public abstract class ContextBuilder {
}
/**
+ * Adds a value to the end of a SET or LIST property.
+ *
+ * @param name The property name.
+ * @param value The new value to add to the SET property.
+ * @return This object (for method chaining).
+ * @throws ConfigException If property is not a SET property.
+ */
+ @ConfigurationProperty
+ public ContextBuilder appendTo(String name, Object value) {
+ psb.appendTo(name, value);
+ return this;
+ }
+
+ /**
+ * Adds a value to the beginning of a SET or LIST property.
+ *
+ * @param name The property name.
+ * @param value The new value to add to the SET property.
+ * @return This object (for method chaining).
+ * @throws ConfigException If property is not a SET property.
+ */
+ @ConfigurationProperty
+ public ContextBuilder prependTo(String name, Object value) {
+ psb.prependTo(name, value);
+ return this;
+ }
+
+ /**
* Adds or overwrites a value to a SET, LIST, or MAP property.
*
* @param name The property name.
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/PropertyStoreBuilder.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/PropertyStoreBuilder.java
index 3300c9a..d8c8ac9 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/PropertyStoreBuilder.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/PropertyStoreBuilder.java
@@ -294,6 +294,72 @@ public class PropertyStoreBuilder {
}
/**
+ * Appends a value to the end of a SET or LIST property.
+ *
+ * <p>
+ * NOTE: When adding to a list, the value is inserted at the end of
the list.
+ *
+ * @param key The property key.
+ * @param value
+ * The new value to add to the property.
+ * <br>This can be a single value, Collection, array, or JSON
array string.
+ * <br><jk>null</jk> values have the effect of removing entries.
+ * @return This object (for method chaining).
+ * @throws ConfigException If property is not a SET/LIST property, or
the argument is invalid.
+ */
+ public synchronized PropertyStoreBuilder appendTo(String key, Object
value) {
+ propertyStore = null;
+ String g = group(key);
+ String n = g.isEmpty() ? key : key.substring(g.length()+1);
+
+ PropertyGroupBuilder gb = groups.get(g);
+ if (gb == null) {
+ gb = new PropertyGroupBuilder();
+ groups.put(g, gb);
+ }
+
+ gb.appendTo(n, value);
+
+ if (gb.isEmpty())
+ groups.remove(g);
+
+ return this;
+ }
+
+ /**
+ * Prepends a value to the beginning of a SET or LIST property.
+ *
+ * <p>
+ * NOTE: When adding to a list, the value is inserted at the beginning
of the list.
+ *
+ * @param key The property key.
+ * @param value
+ * The new value to add to the property.
+ * <br>This can be a single value, Collection, array, or JSON
array string.
+ * <br><jk>null</jk> values have the effect of removing entries.
+ * @return This object (for method chaining).
+ * @throws ConfigException If property is not a SET/LIST property, or
the argument is invalid.
+ */
+ public synchronized PropertyStoreBuilder prependTo(String key, Object
value) {
+ propertyStore = null;
+ String g = group(key);
+ String n = g.isEmpty() ? key : key.substring(g.length()+1);
+
+ PropertyGroupBuilder gb = groups.get(g);
+ if (gb == null) {
+ gb = new PropertyGroupBuilder();
+ groups.put(g, gb);
+ }
+
+ gb.prependTo(n, value);
+
+ if (gb.isEmpty())
+ groups.remove(g);
+
+ return this;
+ }
+
+ /**
* Removes a value from a SET or LIST property.
*
* @param key The property key.
@@ -428,6 +494,38 @@ public class PropertyStoreBuilder {
}
}
+ synchronized void appendTo(String key, Object value) {
+ MutableProperty p = properties.get(key);
+ if (p == null) {
+ p = MutableProperty.create(key, null);
+ p.append(value);
+ if (! p.isEmpty())
+ properties.put(key, p);
+ } else {
+ p.append(value);
+ if (p.isEmpty())
+ properties.remove(key);
+ else
+ properties.put(key, p);
+ }
+ }
+
+ synchronized void prependTo(String key, Object value) {
+ MutableProperty p = properties.get(key);
+ if (p == null) {
+ p = MutableProperty.create(key, null);
+ p.prepend(value);
+ if (! p.isEmpty())
+ properties.put(key, p);
+ } else {
+ p.prepend(value);
+ if (p.isEmpty())
+ properties.remove(key);
+ else
+ properties.put(key, p);
+ }
+ }
+
synchronized void removeFrom(String key, Object value) {
MutableProperty p = properties.get(key);
if (p == null) {
@@ -527,6 +625,14 @@ public class PropertyStoreBuilder {
throw new ConfigException("Cannot add value {0} ({1})
to property ''{2}'' ({3}).", string(value), className(value), name, type);
}
+ void append(Object value) {
+ throw new ConfigException("Cannot append value {0}
({1}) to property ''{2}'' ({3}).", string(value), className(value), name, type);
+ }
+
+ void prepend(Object value) {
+ throw new ConfigException("Cannot prepend value {0}
({1}) to property ''{2}'' ({3}).", string(value), className(value), name, type);
+ }
+
void remove(Object value) {
throw new ConfigException("Cannot remove value {0}
({1}) from property ''{2}'' ({3}).", string(value), className(value), name,
type);
}
@@ -584,7 +690,7 @@ public class PropertyStoreBuilder {
//-------------------------------------------------------------------------------------------------------------------
static class MutableSetProperty extends MutableProperty {
- private final Set<Object> set;
+ private final ConcurrentSkipListSet<Object> set;
MutableSetProperty(String name, PropertyType type, Object
value) {
super(name, type);
@@ -625,6 +731,27 @@ public class PropertyStoreBuilder {
}
@Override /* MutableProperty */
+ synchronized void append(Object o) {
+ try {
+ set.addAll(normalize(type.converter, o));
+ } catch (Exception e) {
+ throw new ConfigException(e, "Cannot append
value {0} ({1}) to property ''{2}'' ({3}).", string(o), className(o), name,
type);
+ }
+ }
+
+ @Override /* MutableProperty */
+ synchronized void prepend(Object o) {
+ try {
+ Set<Object> s = new LinkedHashSet<>(set);
+ set.clear();
+ set.addAll(normalize(type.converter, o));
+ set.addAll(s);
+ } catch (Exception e) {
+ throw new ConfigException(e, "Cannot prepend
value {0} ({1}) to property ''{2}'' ({3}).", string(o), className(o), name,
type);
+ }
+ }
+
+ @Override /* MutableProperty */
synchronized void remove(Object o) {
try {
set.removeAll(normalize(type.converter, o));
@@ -698,6 +825,28 @@ public class PropertyStoreBuilder {
}
@Override /* MutableProperty */
+ synchronized void append(Object o) {
+ try {
+ List<Object> l = normalize(type.converter, o);
+ list.removeAll(l);
+ list.addAll(l);
+ } catch (Exception e) {
+ throw new ConfigException(e, "Cannot append
value {0} ({1}) to property ''{2}'' ({3}).", string(o), className(o), name,
type);
+ }
+ }
+
+ @Override /* MutableProperty */
+ synchronized void prepend(Object o) {
+ try {
+ List<Object> l = normalize(type.converter, o);
+ list.removeAll(l);
+ list.addAll(0, l);
+ } catch (Exception e) {
+ throw new ConfigException(e, "Cannot prepend
value {0} ({1}) to property ''{2}'' ({3}).", string(o), className(o), name,
type);
+ }
+ }
+
+ @Override /* MutableProperty */
synchronized void remove(Object o) {
try {
list.removeAll(normalize(type.converter, o));
diff --git
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientBuilderTest.java
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientBuilderTest.java
index 0a2a6b4..a7a0f9f 100644
---
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientBuilderTest.java
+++
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientBuilderTest.java
@@ -1142,6 +1142,25 @@ public class RestClientBuilderTest {
rc.get("").run().assertStatusCode(200);
}
+
//------------------------------------------------------------------------------------------------------------------
+ // Other Header tests
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test
+ public void h01_multipleHeaders() throws Exception {
+ MockLogger ml = new MockLogger();
+ RestClient rc = MockRestClient
+ .create(A.class)
+ .simpleJson()
+ .logTo(Level.SEVERE, ml)
+ .headers("Foo","bar","Foo","baz")
+ .header("Foo","qux")
+ .build();
+ rc.post("/bean", bean).complete();
+ ml.assertLevel(Level.SEVERE);
+ ml.assertMessageContains("\tFoo: bar\n\tFoo: baz\n\tFoo: qux");
+ }
+
// TODO - Multiple headers
// TODO - Headers overridden on request.
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestClientBuilder.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestClientBuilder.java
index 7b5a32e..73587ff 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestClientBuilder.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestClientBuilder.java
@@ -502,7 +502,7 @@ public class RestClientBuilder extends BeanContextBuilder {
*/
@ConfigurationProperty
public RestClientBuilder header(String name, Object value,
HttpPartSerializer serializer, HttpPartSchema schema) {
- return addTo(RESTCLIENT_headers,
SerializedNameValuePair.create().name(name).value(value).type(HEADER).serializer(serializer).schema(schema));
+ return appendTo(RESTCLIENT_headers,
SerializedNameValuePair.create().name(name).value(value).type(HEADER).serializer(serializer).schema(schema));
}
/**
@@ -550,7 +550,7 @@ public class RestClientBuilder extends BeanContextBuilder {
*/
@ConfigurationProperty
public RestClientBuilder header(Header header) {
- return addTo(RESTCLIENT_headers, header);
+ return appendTo(RESTCLIENT_headers, header);
}
/**
@@ -569,7 +569,7 @@ public class RestClientBuilder extends BeanContextBuilder {
*/
@ConfigurationProperty
public RestClientBuilder header(NameValuePair header) {
- return addTo(RESTCLIENT_headers, header);
+ return appendTo(RESTCLIENT_headers, header);
}
/**
@@ -588,7 +588,7 @@ public class RestClientBuilder extends BeanContextBuilder {
*/
@ConfigurationProperty
public RestClientBuilder header(HttpHeader header) {
- return addTo(RESTCLIENT_headers, header);
+ return appendTo(RESTCLIENT_headers, header);
}
/**
@@ -3033,6 +3033,12 @@ public class RestClientBuilder extends
BeanContextBuilder {
}
@Override /* GENERATED - ContextBuilder */
+ public RestClientBuilder appendTo(String name, Object value) {
+ super.appendTo(name, value);
+ return this;
+ }
+
+ @Override /* GENERATED - ContextBuilder */
public RestClientBuilder addTo(String name, String key, Object value) {
super.addTo(name, key, value);
return this;
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestRequest.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestRequest.java
index 0de6b6a..527546f 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestRequest.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestRequest.java
@@ -1537,7 +1537,7 @@ public final class RestRequest extends BeanSession
implements HttpUriRequest, Co
public RestRequest header(NameValuePair header, boolean skipIfEmpty) {
String v = header.getValue();
if (canAdd(v, skipIfEmpty))
- setHeader(header.getName(), v);
+ addHeader(header.getName(), v);
return this;
}
@@ -1782,7 +1782,7 @@ public final class RestRequest extends BeanSession
implements HttpUriRequest, Co
*/
public RestRequest headers(HttpHeader...headers) throws
RestCallException {
for (HttpHeader h : headers)
- setHeader(h.getName(), stringify(h.getValue()));
+ addHeader(h.getName(), stringify(h.getValue()));
return this;
}
@@ -2257,7 +2257,7 @@ public final class RestRequest extends BeanSession
implements HttpUriRequest, Co
request.setURI(uriBuilder.build());
// Pick the serializer if it hasn't been overridden.
- Header h = getFirstHeader("Content-Type");
+ Header h = getLastHeader("Content-Type");
String contentType = h == null ? null : h.getValue();
Serializer serializer = this.serializer;
if (serializer == null)
@@ -2266,7 +2266,7 @@ public final class RestRequest extends BeanSession
implements HttpUriRequest, Co
contentType =
serializer.getPrimaryMediaType().toString();
// Pick the parser if it hasn't been overridden.
- h = getFirstHeader("Accept");
+ h = getLastHeader("Accept");
String accept = h == null ? null : h.getValue();
Parser parser = this.parser;
if (parser == null)