Skip to content

Commit 2e711aa

Browse files
author
Boris Kuzmic
committed
Creating StringPayloadOf class for tests
1 parent 4b735df commit 2e711aa

File tree

3 files changed

+83
-58
lines changed

3 files changed

+83
-58
lines changed

src/test/java/com/amihaiemil/docker/RtPluginTestCase.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,14 @@
2929
import com.amihaiemil.docker.mock.AssertRequest;
3030
import com.amihaiemil.docker.mock.Condition;
3131
import com.amihaiemil.docker.mock.Response;
32-
import java.io.IOException;
32+
import com.amihaiemil.docker.mock.StringPayloadOf;
3333
import java.net.URI;
3434
import java.util.HashMap;
3535
import java.util.Map;
3636
import javax.json.Json;
3737
import javax.json.JsonArray;
3838
import javax.json.JsonObject;
39-
import org.apache.http.HttpEntityEnclosingRequest;
40-
import org.apache.http.HttpRequest;
4139
import org.apache.http.HttpStatus;
42-
import org.apache.http.util.EntityUtils;
4340
import org.hamcrest.MatcherAssert;
4441
import org.hamcrest.collection.IsCollectionWithSize;
4542
import org.hamcrest.core.IsEqual;
@@ -52,8 +49,6 @@
5249
*
5350
* @author Boris Kuzmic ([email protected])
5451
* @since 0.0.8
55-
* @todo #266:30min Extract method stringPayloadOf to class to reuse in
56-
* other test cases (like in RtPluginsTestCase.createOk() method)
5752
* @checkstyle MethodName (500 lines)
5853
*/
5954
public final class RtPluginTestCase {
@@ -482,7 +477,9 @@ public void configureOk() throws Exception {
482477
),
483478
new Condition(
484479
"configure() must send String Array as request body",
485-
req -> "[\"DEBUG=1\"]".equals(this.stringPayloadOf(req))
480+
req -> "[\"DEBUG=1\"]".equals(
481+
new StringPayloadOf(req).value()
482+
)
486483
)
487484
),
488485
URI.create("http://localhost/plugins/sshfs"),
@@ -520,27 +517,4 @@ public void configureFailsPluginNotInstalled() throws Exception {
520517
);
521518
plugin.configure(new HashMap<>());
522519
}
523-
524-
/**
525-
* Extracts request payload as String.
526-
* @param request Http Request.
527-
* @return Payload as String.
528-
*/
529-
private String stringPayloadOf(final HttpRequest request) {
530-
try {
531-
final String payload;
532-
if (request instanceof HttpEntityEnclosingRequest) {
533-
payload = EntityUtils.toString(
534-
((HttpEntityEnclosingRequest) request).getEntity()
535-
);
536-
} else {
537-
payload = "";
538-
}
539-
return payload;
540-
} catch (final IOException ex) {
541-
throw new IllegalStateException(
542-
"Cannot read request payload", ex
543-
);
544-
}
545-
}
546520
}

src/test/java/com/amihaiemil/docker/RtPluginsTestCase.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
import com.amihaiemil.docker.mock.AssertRequest;
3030
import com.amihaiemil.docker.mock.Condition;
3131
import com.amihaiemil.docker.mock.Response;
32-
import java.io.IOException;
32+
import com.amihaiemil.docker.mock.StringPayloadOf;
3333
import java.net.URI;
3434
import java.util.Iterator;
3535
import javax.json.Json;
3636
import javax.json.JsonArray;
3737
import javax.json.JsonObject;
38-
import org.apache.http.HttpEntityEnclosingRequest;
39-
import org.apache.http.HttpRequest;
4038
import org.apache.http.HttpStatus;
41-
import org.apache.http.util.EntityUtils;
4239
import org.hamcrest.MatcherAssert;
4340
import org.hamcrest.core.IsEqual;
4441
import org.junit.Test;
@@ -80,7 +77,9 @@ public void createOk() throws Exception {
8077
),
8178
new Condition(
8279
"create() must send String body request",
83-
req -> "/home/pluginDir".equals(this.stringPayloadOf(req))
80+
req -> "/home/pluginDir".equals(
81+
new StringPayloadOf(req).value()
82+
)
8483
)
8584
),
8685
URI.create("http://localhost/plugins"),
@@ -210,27 +209,4 @@ public void iteratePluginPrivileges() throws Exception {
210209
);
211210
}
212211

213-
/**
214-
* Extracts request payload as String.
215-
* @param request Http Request.
216-
* @return Payload as String.
217-
*/
218-
private String stringPayloadOf(final HttpRequest request) {
219-
try {
220-
final String payload;
221-
if (request instanceof HttpEntityEnclosingRequest) {
222-
payload = EntityUtils.toString(
223-
((HttpEntityEnclosingRequest) request).getEntity()
224-
);
225-
} else {
226-
payload = "";
227-
}
228-
return payload;
229-
} catch (final IOException ex) {
230-
throw new IllegalStateException(
231-
"Cannot read request payload", ex
232-
);
233-
}
234-
}
235-
236212
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright (c) 2018-2019, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker.mock;
27+
28+
import java.io.IOException;
29+
import org.apache.http.HttpEntityEnclosingRequest;
30+
import org.apache.http.HttpRequest;
31+
import org.apache.http.util.EntityUtils;
32+
33+
/**
34+
* String payload of an HttpRequest.
35+
*
36+
* @author Boris Kuzmic ([email protected])
37+
* @since 0.0.8
38+
*/
39+
public final class StringPayloadOf {
40+
41+
/**
42+
* Payload as String.
43+
*/
44+
private final String stringPayload;
45+
46+
/**
47+
* Ctor.
48+
*
49+
* @param request The http request
50+
* @throws IllegalStateException if the request's payload cannot be read
51+
*/
52+
public StringPayloadOf(final HttpRequest request) {
53+
try {
54+
if (request instanceof HttpEntityEnclosingRequest) {
55+
this.stringPayload = EntityUtils.toString(
56+
((HttpEntityEnclosingRequest) request).getEntity()
57+
);
58+
} else {
59+
this.stringPayload = "";
60+
}
61+
} catch (final IOException ex) {
62+
throw new IllegalStateException(
63+
"Cannot read request payload", ex
64+
);
65+
}
66+
}
67+
68+
/**
69+
* Returns payload value as String.
70+
* @return Payload as String.
71+
*/
72+
public String value() {
73+
return this.stringPayload;
74+
}
75+
}

0 commit comments

Comments
 (0)