Skip to content

Commit 20ac0af

Browse files
committed
Combined + unit tests
1 parent 27995f0 commit 20ac0af

File tree

4 files changed

+145
-5
lines changed

4 files changed

+145
-5
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) 2018, 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;
27+
28+
import javax.json.Json;
29+
import javax.json.JsonObject;
30+
import javax.json.JsonObjectBuilder;
31+
32+
/**
33+
* Combine more JsonObjects into a single one.
34+
* @author Mihai Andronache (amihaiemil@gmail.com)
35+
* @version $Id$
36+
* @since 0.0.2
37+
*/
38+
final class Combined extends JsonResource {
39+
40+
/**
41+
* Ctor.
42+
* @param objects JsonObjects to combine.
43+
*/
44+
Combined(final JsonObject... objects) {
45+
super(() -> {
46+
final JsonObjectBuilder combined = Json.createObjectBuilder();
47+
for(final JsonObject json : objects) {
48+
json.forEach(combined::add);
49+
}
50+
return combined.build();
51+
});
52+
}
53+
54+
}

src/main/java/com/amihaiemil/docker/JsonResource.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.Collection;
2929
import java.util.Map;
30+
import java.util.Objects;
3031
import java.util.Set;
3132
import java.util.function.Supplier;
3233
import javax.json.JsonArray;
@@ -195,4 +196,16 @@ public Set<Entry<String, JsonValue>> entrySet() {
195196
public String toString() {
196197
return this.resource.toString();
197198
}
199+
200+
@Override
201+
public boolean equals(final Object other) {
202+
return this.resource.equals(other);
203+
}
204+
205+
@Override
206+
public int hashCode() {
207+
int hash = 5;
208+
hash = 89 * hash + Objects.hashCode(this.resource);
209+
return hash;
210+
}
198211
}

src/main/java/com/amihaiemil/docker/RtContainers.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.io.IOException;
3737
import java.net.URI;
3838
import java.util.Iterator;
39-
import javax.json.JsonObjectBuilder;
4039

4140
/**
4241
* Containers API.
@@ -118,11 +117,8 @@ public Container create(
118117
new MatchStatus(post.getURI(), HttpStatus.SC_CREATED)
119118
)
120119
);
121-
final JsonObjectBuilder contr = Json.createObjectBuilder();
122-
container.forEach(contr::add);
123-
json.forEach(contr::add);
124120
return new RtContainer(
125-
contr.build(),
121+
new Combined(json, container),
126122
this.client,
127123
URI.create(
128124
this.baseUri.toString() + "/" + json.getString("Id")
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.amihaiemil.docker;
7+
8+
import javax.json.Json;
9+
import javax.json.JsonObject;
10+
import org.hamcrest.MatcherAssert;
11+
import org.hamcrest.Matchers;
12+
import org.junit.Test;
13+
14+
/**
15+
* Unit tests for {@link Combined}.
16+
* @author Mihai Andronache (amihaiemil@gmail.com)
17+
* @version $Id$
18+
* @since 0.0.2
19+
*/
20+
public final class CombinedTestCase {
21+
22+
/**
23+
* {@link Combined} can combine some JsonObjects.
24+
*/
25+
@Test
26+
public void combinesObjects() {
27+
final JsonObject first = Json.createObjectBuilder()
28+
.add("firstName", "John")
29+
.add("age", 23)
30+
.build();
31+
final JsonObject second = Json.createObjectBuilder()
32+
.add("lastName", "George")
33+
.add("job", "developer")
34+
.build();
35+
36+
final JsonObject expected = Json.createObjectBuilder()
37+
.add("firstName", "John")
38+
.add("lastName", "George")
39+
.add("age", 23)
40+
.add("job", "developer")
41+
.build();
42+
MatcherAssert.assertThat(
43+
new Combined(first, second),
44+
Matchers.equalTo(expected)
45+
);
46+
}
47+
48+
/**
49+
* {@link Combined} can combine one single JsonObject.
50+
*/
51+
@Test
52+
public void combinesSingleObject() {
53+
final JsonObject single = Json.createObjectBuilder()
54+
.add("firstName", "John")
55+
.add("age", 23)
56+
.build();
57+
MatcherAssert.assertThat(
58+
new Combined(single),
59+
Matchers.equalTo(single)
60+
);
61+
}
62+
63+
/**
64+
* {@link Combined} can combine one JsonObject and another empty one.
65+
*/
66+
@Test
67+
public void combinesWithEmptyObject() {
68+
final JsonObject single = Json.createObjectBuilder()
69+
.add("firstName", "John")
70+
.add("age", 23)
71+
.build();
72+
MatcherAssert.assertThat(
73+
new Combined(single, Json.createObjectBuilder().build()),
74+
Matchers.equalTo(single)
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)