Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.

Commit a3ba311

Browse files
committed
test: Added CrudDispatcher integration test
1 parent 38d9f06 commit a3ba311

File tree

4 files changed

+219
-6
lines changed

4 files changed

+219
-6
lines changed

src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
import okhttp3.mockwebserver.MockResponse;
2424
import okhttp3.mockwebserver.RecordedRequest;
2525

26+
import java.net.HttpURLConnection;
2627
import java.util.ArrayList;
27-
import java.util.HashMap;
28+
import java.util.LinkedHashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031

@@ -36,7 +37,7 @@ public class CrudDispatcher extends Dispatcher {
3637
private static final String GET = "GET";
3738
private static final String DELETE = "DELETE";
3839

39-
protected final Map<AttributeSet, String> map = new HashMap<>();
40+
protected final Map<AttributeSet, String> map = new LinkedHashMap<>();
4041

4142
protected final Context context;
4243
protected final AttributeExtractor attributeExtractor;
@@ -51,9 +52,7 @@ public CrudDispatcher(Context context, AttributeExtractor attributeExtractor, Re
5152
@Override
5253
public MockResponse dispatch(RecordedRequest request) {
5354
String path = request.getPath();
54-
String method = request.getMethod();
55-
56-
switch (method.toUpperCase()) {
55+
switch (request.getMethod().toUpperCase()) {
5756
case POST:
5857
return handleCreate(path, request.getBody().readUtf8());
5958
case PUT:
@@ -124,7 +123,12 @@ public MockResponse handlePatch(String path, String body) {
124123
* @return a MockResponse to be dispatched.
125124
*/
126125
public MockResponse handleUpdate(String path, String body) {
127-
return handleCreate(path, body);
126+
final String currentItem = doGet(path);
127+
final MockResponse response = handleCreate(path, body);
128+
if (currentItem == null) {
129+
response.setResponseCode(HttpURLConnection.HTTP_CREATED);
130+
}
131+
return response;
128132
}
129133

130134
/**
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.mockwebserver
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper
19+
import io.fabric8.mockwebserver.crud.CrudDispatcher
20+
import okhttp3.MediaType
21+
import okhttp3.OkHttpClient
22+
import okhttp3.Request
23+
import okhttp3.RequestBody
24+
import okhttp3.mockwebserver.MockWebServer
25+
import spock.lang.Shared
26+
import spock.lang.Specification
27+
28+
class DefaultMockServerCrudTest extends Specification {
29+
30+
DefaultMockServer server
31+
32+
@Shared
33+
def client = new OkHttpClient()
34+
35+
@Shared
36+
def mapper = new ObjectMapper()
37+
38+
def setup() {
39+
server = new DefaultMockServer(new Context(), new MockWebServer(), new HashMap<>(),
40+
new CrudDispatcher(new Context(), new UserAttributeExtractor(), new JsonResponseComposer()), false)
41+
server.start()
42+
}
43+
44+
def cleanup() {
45+
server.shutdown()
46+
}
47+
48+
def "get /, with empty store, should return 404"() {
49+
when:
50+
def result = client.newCall(new Request.Builder().url(server.url("/")).build()).execute()
51+
52+
then:
53+
assert result.code() == 404
54+
assert result.body().string() == ""
55+
}
56+
57+
def "get /, with one item, should return item"() {
58+
given:
59+
client.newCall(new Request.Builder().url(server.url("/")).post(
60+
RequestBody.create(MediaType.parse("application/json"),
61+
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
62+
execute()
63+
64+
when:
65+
def result = client.newCall(new Request.Builder().url(server.url("/")).build()).execute()
66+
67+
then:
68+
assert result.code() == 200
69+
assert result.body().string() == "{\"id\":1,\"username\":\"user\",\"enabled\":true}"
70+
}
71+
72+
def "get /, with multiple items, should return array"() {
73+
given:
74+
client.newCall(new Request.Builder().url(server.url("/")).post(
75+
RequestBody.create(MediaType.parse("application/json"),
76+
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
77+
execute()
78+
client.newCall(new Request.Builder().url(server.url("/")).post(
79+
RequestBody.create(MediaType.parse("application/json"),
80+
mapper.writeValueAsString(new User(2L, "user-2", true)))).build()).
81+
execute()
82+
83+
when:
84+
def result = client.newCall(new Request.Builder().url(server.url("/")).build()).execute()
85+
86+
then:
87+
assert result.code() == 200
88+
assert result.body().string() ==
89+
"[{\"id\":1,\"username\":\"user\",\"enabled\":true},{\"id\":2,\"username\":\"user-2\",\"enabled\":true}]"
90+
}
91+
92+
def "get /1, with existent item, should return item"() {
93+
given:
94+
client.newCall(new Request.Builder().url(server.url("/")).post(
95+
RequestBody.create(MediaType.parse("application/json"),
96+
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
97+
execute()
98+
client.newCall(new Request.Builder().url(server.url("/")).post(
99+
RequestBody.create(MediaType.parse("application/json"),
100+
mapper.writeValueAsString(new User(2L, "user-2", true)))).build()).
101+
execute()
102+
103+
when:
104+
def result = client.newCall(new Request.Builder().url(server.url("/1")).build()).execute()
105+
106+
then:
107+
assert result.code() == 200
108+
assert result.body().string() == "{\"id\":1,\"username\":\"user\",\"enabled\":true}"
109+
}
110+
111+
def "put /1, with missing item, should create item"() {
112+
when:
113+
def result = client.newCall(new Request.Builder().url(server.url("/1")).put(
114+
RequestBody.create(MediaType.parse("application/json"),
115+
mapper.writeValueAsString(new User(1L, "user-replaced", true)))).build()).
116+
execute()
117+
118+
then:
119+
assert result.code() == 201
120+
assert result.body().string() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
121+
}
122+
123+
def "put /1, with existent item, should replace item"() {
124+
given:
125+
client.newCall(new Request.Builder().url(server.url("/")).post(
126+
RequestBody.create(MediaType.parse("application/json"),
127+
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
128+
execute()
129+
130+
when:
131+
def result = client.newCall(new Request.Builder().url(server.url("/1")).put(
132+
RequestBody.create(MediaType.parse("application/json"),
133+
mapper.writeValueAsString(new User(1L, "user-replaced", true)))).build()).
134+
execute()
135+
136+
then:
137+
assert result.code() == 202
138+
assert result.body().string() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
139+
def item = client.newCall(new Request.Builder().url(server.url("/1")).build()).execute()
140+
assert item.body().string() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
141+
}
142+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.mockwebserver
17+
18+
import io.fabric8.mockwebserver.crud.ResponseComposer
19+
20+
import java.util.stream.Collectors
21+
22+
class JsonResponseComposer implements ResponseComposer {
23+
@Override
24+
String compose(Collection<String> items) {
25+
return "[" + items.stream().collect(Collectors.joining(",")) + "]"
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.mockwebserver
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper
19+
import io.fabric8.mockwebserver.crud.Attribute
20+
import io.fabric8.mockwebserver.crud.AttributeExtractor
21+
import io.fabric8.mockwebserver.crud.AttributeSet
22+
23+
class UserAttributeExtractor implements AttributeExtractor {
24+
25+
static def mapper = new ObjectMapper()
26+
27+
@Override
28+
AttributeSet fromPath(String path) {
29+
if (path.trim().isBlank() || path.trim() == "/") {
30+
return new AttributeSet();
31+
}
32+
return new AttributeSet(new Attribute("id", path.substring(1)))
33+
}
34+
35+
@Override
36+
AttributeSet fromResource(String resource) {
37+
def user = mapper.readValue(resource, User)
38+
return new AttributeSet(new Attribute("id", user.getId().toString()))
39+
}
40+
}

0 commit comments

Comments
 (0)