|
@@ -1,221 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright 2016 the original author or authors.
|
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 fr.sayasoft.fake.zinc;
|
17
|
|
-
|
18
|
|
-import com.google.common.collect.Lists;
|
19
|
|
-import com.google.gson.Gson;
|
20
|
|
-import fr.sayasoft.zinc.sdk.domain.OrderRequest;
|
21
|
|
-import fr.sayasoft.zinc.sdk.domain.PaymentMethod;
|
22
|
|
-import fr.sayasoft.zinc.sdk.domain.Product;
|
23
|
|
-import fr.sayasoft.zinc.sdk.domain.RetailerCredentials;
|
24
|
|
-import fr.sayasoft.zinc.sdk.domain.ZincAddress;
|
25
|
|
-import fr.sayasoft.zinc.sdk.enums.ShippingMethod;
|
26
|
|
-import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
|
27
|
|
-import org.junit.Test;
|
28
|
|
-import org.junit.runner.RunWith;
|
29
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
30
|
|
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
31
|
|
-import org.springframework.boot.test.context.SpringBootTest;
|
32
|
|
-import org.springframework.http.MediaType;
|
33
|
|
-import org.springframework.test.context.junit4.SpringRunner;
|
34
|
|
-import org.springframework.test.web.servlet.MockMvc;
|
35
|
|
-
|
36
|
|
-import java.nio.charset.Charset;
|
37
|
|
-
|
38
|
|
-import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE;
|
39
|
|
-import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE_TO_BE_REPLACED;
|
40
|
|
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
41
|
|
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
42
|
|
-import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
43
|
|
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
44
|
|
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
45
|
|
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
46
|
|
-
|
47
|
|
-@RunWith(SpringRunner.class)
|
48
|
|
-@SpringBootTest
|
49
|
|
-@AutoConfigureMockMvc
|
50
|
|
-public class FakeZincControllerUnitTest {
|
51
|
|
- private final OrderRequest orderRequest = OrderRequest.builder()
|
52
|
|
- .retailer("amazon")
|
53
|
|
- .products(Lists.newArrayList(new Product("0923568964")))
|
54
|
|
- .shippingAddress(
|
55
|
|
- ZincAddress.builder()
|
56
|
|
- .firstName("John Hannibal")
|
57
|
|
- .lastName("Smith")
|
58
|
|
- .addressLine1("1234 Main Street")
|
59
|
|
- .addressLine2("above the bar")
|
60
|
|
- .zipCode("11907")
|
61
|
|
- .city("Brooklyn")
|
62
|
|
- .state("NY")
|
63
|
|
- .country("US")
|
64
|
|
- .phoneNumber("123-123-1234")
|
65
|
|
- .build()
|
66
|
|
- ).shippingMethod(ShippingMethod.cheapest) // TODO
|
67
|
|
- .billingAddress( // TODO parametrize
|
68
|
|
- ZincAddress.builder()
|
69
|
|
- .firstName("John Hannibal")
|
70
|
|
- .lastName("Smith")
|
71
|
|
- .addressLine1("1234 Main Street")
|
72
|
|
- .addressLine2("above the bar")
|
73
|
|
- .zipCode("11907")
|
74
|
|
- .city("Brooklyn")
|
75
|
|
- .state("NY")
|
76
|
|
- .country("US")
|
77
|
|
- .phoneNumber("123-123-1234")
|
78
|
|
- .build()
|
79
|
|
- )
|
80
|
|
- .paymentMethod(
|
81
|
|
- PaymentMethod.builder()
|
82
|
|
- .nameOnCard("Hello World")
|
83
|
|
- .number("0000000000000000")
|
84
|
|
- .securityCode("000")
|
85
|
|
- .expirationMonth(12)
|
86
|
|
- .expirationYear(2020)
|
87
|
|
- .useGift(false)
|
88
|
|
- .build()
|
89
|
|
- )
|
90
|
|
- .retailerCredentials(new RetailerCredentials("test@test.fr", "password")) // TODO
|
91
|
|
- .maxPrice(0) // TODO
|
92
|
|
- .giftMessage("Here is your package") // TODO
|
93
|
|
- .isGift(true)
|
94
|
|
- .build();
|
95
|
|
-
|
96
|
|
- private final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
|
97
|
|
- MediaType.APPLICATION_JSON.getSubtype(),
|
98
|
|
- Charset.forName("utf8"));
|
99
|
|
-
|
100
|
|
- public static final String POST_ORDER_REQUEST = "{\n" +
|
101
|
|
- " \"client_token\": \"public\",\n" +
|
102
|
|
- " \"idempotency_key\": \"XXX\", \n" +
|
103
|
|
- " \"retailer\": \"amazon\",\n" +
|
104
|
|
- " \"products\": [{\"product_id\": \"0923568964\", \"quantity\": 1}],\n" +
|
105
|
|
- " \"max_price\": 2300,\n" +
|
106
|
|
- " \"shipping_address\": {\n" +
|
107
|
|
- " \"first_name\": \"Tim\",\n" +
|
108
|
|
- " \"last_name\": \"Beaver\",\n" +
|
109
|
|
- " \"address_line1\": \"77 Massachusetts Avenue\",\n" +
|
110
|
|
- " \"address_line2\": \"\",\n" +
|
111
|
|
- " \"zip_code\": \"02139\",\n" +
|
112
|
|
- " \"city\": \"Cambridge\", \n" +
|
113
|
|
- " \"state\": \"MA\",\n" +
|
114
|
|
- " \"country\": \"US\",\n" +
|
115
|
|
- " \"phone_number\": \"5551230101\"\n" +
|
116
|
|
- " },\n" +
|
117
|
|
- " \"is_gift\": true,\n" +
|
118
|
|
- " \"gift_message\": \"Here's your package, Tim! Enjoy!\",\n" +
|
119
|
|
- " \"shipping_method\": \"cheapest\",\n" +
|
120
|
|
- " \"payment_method\": {\n" +
|
121
|
|
- " \"name_on_card\": \"Ben Bitdiddle\",\n" +
|
122
|
|
- " \"number\": \"5555555555554444\",\n" +
|
123
|
|
- " \"security_code\": \"123\",\n" +
|
124
|
|
- " \"expiration_month\": 1,\n" +
|
125
|
|
- " \"expiration_year\": 2015,\n" +
|
126
|
|
- " \"use_gift\": false\n" +
|
127
|
|
- " },\n" +
|
128
|
|
- " \"billing_address\": {\n" +
|
129
|
|
- " \"first_name\": \"William\", \n" +
|
130
|
|
- " \"last_name\": \"Rogers\",\n" +
|
131
|
|
- " \"address_line1\": \"84 Massachusetts Ave\",\n" +
|
132
|
|
- " \"address_line2\": \"\",\n" +
|
133
|
|
- " \"zip_code\": \"02139\",\n" +
|
134
|
|
- " \"city\": \"Cambridge\", \n" +
|
135
|
|
- " \"state\": \"MA\",\n" +
|
136
|
|
- " \"country\": \"US\",\n" +
|
137
|
|
- " \"phone_number\": \"5551234567\"\n" +
|
138
|
|
- " },\n" +
|
139
|
|
- " \"retailer_credentials\": {\n" +
|
140
|
|
- " \"email\": \"timbeaver@gmail.com\",\n" +
|
141
|
|
- " \"password\": \"myAmazonPassword\"\n" +
|
142
|
|
- " },\n" +
|
143
|
|
- " \"webhooks\": {\n" +
|
144
|
|
- " \"order_placed\": \"http://mywebsite.com/zinc/order_placed\",\n" +
|
145
|
|
- " \"order_failed\": \"http://mywebsite.com/zinc/order_failed\",\n" +
|
146
|
|
- " \"tracking_obtained\": \"http://mywebsite.com/zinc/tracking_obtained\"\n" +
|
147
|
|
- " },\n" +
|
148
|
|
- " \"client_notes\": {\n" +
|
149
|
|
- " \"our_internal_order_id\": \"abc123\"\n" +
|
150
|
|
- " }\n" +
|
151
|
|
- "}";
|
152
|
|
- @Autowired
|
153
|
|
- private MockMvc mockMvc;
|
154
|
|
-
|
155
|
|
- @Test
|
156
|
|
- public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
|
157
|
|
-
|
158
|
|
- this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
|
159
|
|
- .andExpect(jsonPath("$.content").value("Hello, World!"));
|
160
|
|
- }
|
161
|
|
-
|
162
|
|
- @Test
|
163
|
|
- public void paramGreetingShouldReturnTailoredMessage() throws Exception {
|
164
|
|
-
|
165
|
|
- this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
|
166
|
|
- .andDo(print()).andExpect(status().isOk())
|
167
|
|
- .andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
|
168
|
|
- }
|
169
|
|
-
|
170
|
|
- @Test
|
171
|
|
- public void getOrder() throws Exception {
|
172
|
|
-
|
173
|
|
- this.mockMvc.perform(get("/v1/orders/1234546"))
|
174
|
|
- .andDo(print()).andExpect(status().isOk())
|
175
|
|
- .andExpect(content().string(FakeZincController.GET_ORDER_RESPONSE));
|
176
|
|
- }
|
177
|
|
-
|
178
|
|
- @Test
|
179
|
|
- public void postOrder_withString() throws Exception {
|
180
|
|
- this.mockMvc.perform(post("/v1/orders")
|
181
|
|
- .contentType(contentType)
|
182
|
|
- .content(POST_ORDER_REQUEST))
|
183
|
|
- .andDo(print())
|
184
|
|
- .andExpect(status().isCreated())
|
185
|
|
- .andExpect(content().string(POST_ORDER_RESPONSE));
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- @Test
|
189
|
|
- public void postOrder_withObject() throws Exception {
|
190
|
|
- final String idempotencyKey = "Carina-β-Carinae-Miaplacidus";
|
191
|
|
- orderRequest.setIdempotencyKey(idempotencyKey);
|
192
|
|
- this.mockMvc.perform(post("/v1/orders")
|
193
|
|
- .contentType(contentType)
|
194
|
|
- .content(new Gson().toJson(orderRequest)))
|
195
|
|
- .andDo(print())
|
196
|
|
- .andExpect(status().isCreated())
|
197
|
|
- .andExpect(content().string(POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey)));
|
198
|
|
- }
|
199
|
|
-
|
200
|
|
- @Test
|
201
|
|
- public void postOrder_KO() throws Exception {
|
202
|
|
- final String idempotencyKey = "Ursa-Major-γ-Ursae-Majoris-Phecda";
|
203
|
|
- final String clientNotes = ZincErrorCode.invalid_quantity.name();
|
204
|
|
- orderRequest.setIdempotencyKey(idempotencyKey);
|
205
|
|
- orderRequest.setClientNotes(clientNotes);
|
206
|
|
-
|
207
|
|
- final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
|
208
|
|
- MediaType.APPLICATION_JSON.getSubtype(),
|
209
|
|
- Charset.forName("utf8"));
|
210
|
|
- this.mockMvc.perform(post("/v1/orders")
|
211
|
|
- .contentType(contentType)
|
212
|
|
- .content(new Gson().toJson(orderRequest)))
|
213
|
|
- .andDo(print())
|
214
|
|
- .andExpect(status().is2xxSuccessful())
|
215
|
|
- .andExpect(content().string("{\"code\":\"invalid_quantity\"," +
|
216
|
|
- "\"message\":\"The quantity for one of the products does not match the one available on the retailer.\"," +
|
217
|
|
- "\"data\":\"Ursa-Major-γ-Ursae-Majoris-Phecda\"" +
|
218
|
|
- "}"));
|
219
|
|
- }
|
220
|
|
-
|
221
|
|
-}
|