|
| 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 | +} |
0 commit comments