Ver código fonte

FakeZinc: cleaning + updated tests

Jonathan Lalou 7 anos atrás
pai
commit
3753901397

+ 9
- 4
src/main/java/fr/sayasoft/fake/zinc/FakeZincController.java Ver arquivo

2
 
2
 
3
 import com.google.gson.Gson;
3
 import com.google.gson.Gson;
4
 import fr.sayasoft.zinc.sdk.domain.OrderRequest;
4
 import fr.sayasoft.zinc.sdk.domain.OrderRequest;
5
+import fr.sayasoft.zinc.sdk.domain.ZincConstants;
5
 import fr.sayasoft.zinc.sdk.domain.ZincError;
6
 import fr.sayasoft.zinc.sdk.domain.ZincError;
6
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
7
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
7
 import lombok.extern.log4j.Log4j;
8
 import lombok.extern.log4j.Log4j;
76
      * Conventions for testing:
77
      * Conventions for testing:
77
      * <ul>
78
      * <ul>
78
      * <li>if the unmarshalled OrderRequest has a field clientNotes that is a ZincErrorCode, then a ZincError will be returned.</li>
79
      * <li>if the unmarshalled OrderRequest has a field clientNotes that is a ZincErrorCode, then a ZincError will be returned.</li>
79
-     * <li>else a response with the idemPotency in the requestId is returned</li>
80
+     * <li>else a response containing the idemPotency in the requestId is returned</li>
80
      * </ul>
81
      * </ul>
81
      */
82
      */
82
     @SuppressWarnings("unused")
83
     @SuppressWarnings("unused")
88
     public ResponseEntity<?> postOrder(@RequestBody String json) {
89
     public ResponseEntity<?> postOrder(@RequestBody String json) {
89
         final Gson gson = new Gson();
90
         final Gson gson = new Gson();
90
         final OrderRequest orderRequest = gson.fromJson(json, OrderRequest.class);
91
         final OrderRequest orderRequest = gson.fromJson(json, OrderRequest.class);
92
+        // can be null
93
+        final String idempotencyKey = orderRequest.getIdempotencyKey();
91
 
94
 
92
         try {
95
         try {
93
-            final ZincErrorCode zincErrorCode;
94
-            zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
96
+            final ZincErrorCode zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
95
             final ZincError zincError = ZincError.builder()
97
             final ZincError zincError = ZincError.builder()
98
+                    .type(ZincConstants.error)
96
                     .code(zincErrorCode)
99
                     .code(zincErrorCode)
100
+                    .data("{'fakeField': '" + idempotencyKey + "'}") // TODO replace with a fake Map<>
97
                     .message(zincErrorCode.getMeaning())
101
                     .message(zincErrorCode.getMeaning())
98
                     .orderRequest(orderRequest)
102
                     .orderRequest(orderRequest)
99
                     .build()
103
                     .build()
116
         }
120
         }
117
 
121
 
118
         return new ResponseEntity<>(
122
         return new ResponseEntity<>(
119
-                POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, orderRequest.getIdempotencyKey()),
123
+                POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey),
120
                 HttpStatus.CREATED);
124
                 HttpStatus.CREATED);
125
+        // TODO call the webhooks if present
121
     }
126
     }
122
 }
127
 }

+ 100
- 7
src/test/java/fr/sayasoft/fake/zinc/FakeZincControllerUnitTest.java Ver arquivo

15
  */
15
  */
16
 package fr.sayasoft.fake.zinc;
16
 package fr.sayasoft.fake.zinc;
17
 
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
+
28
+import org.hamcrest.core.StringContains;
18
 import org.junit.Test;
29
 import org.junit.Test;
19
 import org.junit.runner.RunWith;
30
 import org.junit.runner.RunWith;
20
 import org.springframework.beans.factory.annotation.Autowired;
31
 import org.springframework.beans.factory.annotation.Autowired;
26
 
37
 
27
 import java.nio.charset.Charset;
38
 import java.nio.charset.Charset;
28
 
39
 
40
+import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE;
41
+import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE_TO_BE_REPLACED;
29
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
42
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
43
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
31
 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
44
 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
37
 @SpringBootTest
50
 @SpringBootTest
38
 @AutoConfigureMockMvc
51
 @AutoConfigureMockMvc
39
 public class FakeZincControllerUnitTest {
52
 public class FakeZincControllerUnitTest {
53
+    private final OrderRequest orderRequest = OrderRequest.builder()
54
+            .retailer("amazon")
55
+            .products(Lists.newArrayList(new Product("0923568964")))
56
+            .shippingAddress(
57
+                    ZincAddress.builder()
58
+                            .firstName("John Hannibal")
59
+                            .lastName("Smith")
60
+                            .addressLine1("1234 Main Street")
61
+                            .addressLine2("above the bar")
62
+                            .zipCode("11907")
63
+                            .city("Brooklyn")
64
+                            .state("NY")
65
+                            .country("US")
66
+                            .phoneNumber("123-123-1234")
67
+                            .build()
68
+            ).shippingMethod(ShippingMethod.cheapest) // TODO
69
+            .billingAddress( // TODO parametrize
70
+                    ZincAddress.builder()
71
+                            .firstName("John Hannibal")
72
+                            .lastName("Smith")
73
+                            .addressLine1("1234 Main Street")
74
+                            .addressLine2("above the bar")
75
+                            .zipCode("11907")
76
+                            .city("Brooklyn")
77
+                            .state("NY")
78
+                            .country("US")
79
+                            .phoneNumber("123-123-1234")
80
+                            .build()
81
+            )
82
+            .paymentMethod(
83
+                    PaymentMethod.builder()
84
+                            .nameOnCard("Hello World")
85
+                            .number("0000000000000000")
86
+                            .securityCode("000")
87
+                            .expirationMonth(12)
88
+                            .expirationYear(2020)
89
+                            .useGift(false)
90
+                            .build()
91
+            )
92
+            .retailerCredentials(new RetailerCredentials("test@test.fr", "password")) // TODO
93
+            .maxPrice(0) // TODO
94
+            .giftMessage("Here is your package") // TODO
95
+            .isGift(true)
96
+            .build();
97
+
98
+    private final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
99
+            MediaType.APPLICATION_JSON.getSubtype(),
100
+            Charset.forName("utf8"));
40
 
101
 
41
     public static final String POST_ORDER_REQUEST = "{\n" +
102
     public static final String POST_ORDER_REQUEST = "{\n" +
42
             "  \"client_token\": \"public\",\n" +
103
             "  \"client_token\": \"public\",\n" +
104
+            "  \"idempotency_key\": \"XXX\", \n" +
43
             "  \"retailer\": \"amazon\",\n" +
105
             "  \"retailer\": \"amazon\",\n" +
44
             "  \"products\": [{\"product_id\": \"0923568964\", \"quantity\": 1}],\n" +
106
             "  \"products\": [{\"product_id\": \"0923568964\", \"quantity\": 1}],\n" +
45
             "  \"max_price\": 2300,\n" +
107
             "  \"max_price\": 2300,\n" +
110
     @Test
172
     @Test
111
     public void getOrder() throws Exception {
173
     public void getOrder() throws Exception {
112
 
174
 
113
-        this.mockMvc.perform(get("/orders/1234546"))
175
+        this.mockMvc.perform(get("/v1/orders/1234546"))
114
                 .andDo(print()).andExpect(status().isOk())
176
                 .andDo(print()).andExpect(status().isOk())
115
                 .andExpect(content().string(FakeZincController.GET_ORDER_RESPONSE));
177
                 .andExpect(content().string(FakeZincController.GET_ORDER_RESPONSE));
116
     }
178
     }
117
 
179
 
118
     @Test
180
     @Test
119
-    public void postOrder() throws Exception {
120
-        MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
121
-                MediaType.APPLICATION_JSON.getSubtype(),
122
-                Charset.forName("utf8"));
123
-        this.mockMvc.perform(post("/order")
181
+    public void postOrder_withString() throws Exception {
182
+        this.mockMvc.perform(post("/v1/order")
124
                 .contentType(contentType)
183
                 .contentType(contentType)
125
                 .content(POST_ORDER_REQUEST))
184
                 .content(POST_ORDER_REQUEST))
126
                 .andDo(print())
185
                 .andDo(print())
127
                 .andExpect(status().isCreated())
186
                 .andExpect(status().isCreated())
128
-                .andExpect(content().string(FakeZincController.POST_ORDER_RESPONSE));
187
+                .andExpect(content().string(POST_ORDER_RESPONSE));
188
+    }
189
+
190
+    @Test
191
+    public void postOrder_withObject() throws Exception {
192
+        final String idempotencyKey = "Carina-β-Carinae-Miaplacidus";
193
+        orderRequest.setIdempotencyKey(idempotencyKey);
194
+        this.mockMvc.perform(post("/v1/order")
195
+                .contentType(contentType)
196
+                .content(new Gson().toJson(orderRequest)))
197
+                .andDo(print())
198
+                .andExpect(status().isCreated())
199
+                .andExpect(content().string(POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey)));
200
+    }
201
+
202
+    @Test
203
+    public void postOrder_KO() throws Exception {
204
+        final String idempotencyKey = "Ursa-Major-Ursae-Majoris-Phecda";
205
+        final String clientNotes = ZincErrorCode.invalid_quantity.name();
206
+        orderRequest.setIdempotencyKey(idempotencyKey);
207
+        orderRequest.setClientNotes(clientNotes);
208
+
209
+        final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
210
+                MediaType.APPLICATION_JSON.getSubtype(),
211
+                Charset.forName("utf8"));
212
+        this.mockMvc.perform(post("/v1/order")
213
+                .contentType(contentType)
214
+                .content(new Gson().toJson(orderRequest)))
215
+                .andDo(print())
216
+                .andExpect(status().is2xxSuccessful())
217
+                .andExpect(content().string(StringContains.containsString("\"code\":\"invalid_quantity\"")))
218
+                .andExpect(content().string(StringContains.containsString("\"message\":\"The quantity for one of the products does not match the one available on the retailer.\"")))
219
+                .andExpect(content().string(StringContains.containsString("\"data\":\"{'fakeField': 'Ursa-Major-Ursae-Majoris-Phecda'}\"")))
220
+                .andExpect(content().string(StringContains.containsString("\"_type\":\"error\"")))
221
+                ;
129
     }
222
     }
130
 
223
 
131
 }
224
 }

+ 0
- 221
test/java/fr/sayasoft/fake/zinc/FakeZincControllerUnitTest.java Ver arquivo

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
-}