Преглед изворни кода

added feature: the FakeController can call back the webhooks

Jonathan LALOU пре 7 година
родитељ
комит
7d845f5de9

+ 32
- 14
src/main/java/fr/sayasoft/fake/zinc/FakeZincController.java Прегледај датотеку

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.OrderResponse;
5
 import fr.sayasoft.zinc.sdk.domain.ZincConstants;
6
 import fr.sayasoft.zinc.sdk.domain.ZincConstants;
6
 import fr.sayasoft.zinc.sdk.domain.ZincError;
7
 import fr.sayasoft.zinc.sdk.domain.ZincError;
7
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
8
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
14
 import org.springframework.web.bind.annotation.RequestMethod;
15
 import org.springframework.web.bind.annotation.RequestMethod;
15
 import org.springframework.web.bind.annotation.RequestParam;
16
 import org.springframework.web.bind.annotation.RequestParam;
16
 import org.springframework.web.bind.annotation.RestController;
17
 import org.springframework.web.bind.annotation.RestController;
18
+import org.springframework.web.client.RestTemplate;
17
 
19
 
18
 import java.util.concurrent.atomic.AtomicLong;
20
 import java.util.concurrent.atomic.AtomicLong;
19
 
21
 
93
         final String idempotencyKey = orderRequest.getIdempotencyKey();
95
         final String idempotencyKey = orderRequest.getIdempotencyKey();
94
 
96
 
95
         try {
97
         try {
96
-            final ZincErrorCode zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
97
-            final ZincError zincError = ZincError.builder()
98
-                    .type(ZincConstants.error)
99
-                    .code(zincErrorCode)
100
-                    .data("{'fakeField': '" + idempotencyKey + "'}") // TODO replace with a fake Map<>
101
-                    .message(zincErrorCode.getMeaning())
102
-                    .orderRequest(orderRequest)
103
-                    .build()
104
-                    ;
105
-            log.info("Received request to generate error code, returning: " + zincError);
98
+            if (null != orderRequest.getClientNotes()) {
99
+                final ZincErrorCode zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
100
+                final ZincError zincError = ZincError.builder()
101
+                        .type(ZincConstants.error)
102
+                        .code(zincErrorCode)
103
+                        .data("{'fakeField': '" + idempotencyKey + "'}") // TODO replace with a fake Map<>
104
+                        .message(zincErrorCode.getMeaning())
105
+                        .orderRequest(orderRequest)
106
+                        .build();
107
+                log.info("Received request to generate error code, returning: " + zincError);
106
             /*
108
             /*
107
             Precision obtained from Zinc support: although an error message is returned, the HTTP header is a 200
109
             Precision obtained from Zinc support: although an error message is returned, the HTTP header is a 200
108
             (and not 4XX as may be assumed)
110
             (and not 4XX as may be assumed)
109
             */
111
             */
110
-            return new ResponseEntity<>(
111
-                    zincError,
112
-                    HttpStatus.OK);
112
+                return new ResponseEntity<>(
113
+                        zincError,
114
+                        HttpStatus.OK);
115
+            }
113
         } catch (IllegalArgumentException e) {
116
         } catch (IllegalArgumentException e) {
114
             // nothing to do
117
             // nothing to do
115
-            if (log.isInfoEnabled()){
118
+            if (log.isInfoEnabled()) {
116
                 log.info("received clientNotes: " + orderRequest.getClientNotes() + " ; will go on");
119
                 log.info("received clientNotes: " + orderRequest.getClientNotes() + " ; will go on");
117
             }
120
             }
118
         } catch (Exception e) {
121
         } catch (Exception e) {
119
             log.error("An error occured", e);
122
             log.error("An error occured", e);
120
         }
123
         }
121
 
124
 
125
+        if (!orderRequest.getWebhooks().isEmpty()) {
126
+            callbackWebHooks(orderRequest);
127
+        }
128
+
122
         return new ResponseEntity<>(
129
         return new ResponseEntity<>(
123
                 POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey),
130
                 POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey),
124
                 HttpStatus.CREATED);
131
                 HttpStatus.CREATED);
125
         // TODO call the webhooks if present
132
         // TODO call the webhooks if present
126
     }
133
     }
134
+
135
+    protected void callbackWebHooks(OrderRequest orderRequest) {
136
+        final RestTemplate restTemplate = new RestTemplate();
137
+        final OrderResponse orderResponse = OrderResponse.builder().orderRequest(orderRequest).build();
138
+        orderRequest.getWebhooks()
139
+                .forEach((zincWebhookType, url) -> {
140
+                            log.info("Calling webhook for zincWebhookType: " + zincWebhookType + " and URL: " + url);
141
+                            restTemplate.postForObject(url, orderResponse, String.class);
142
+                        }
143
+                );
144
+    }
127
 }
145
 }

+ 21
- 1
src/test/java/fr/sayasoft/fake/zinc/FakeZincControllerUnitTest.java Прегледај датотеку

24
 import fr.sayasoft.zinc.sdk.domain.ZincAddress;
24
 import fr.sayasoft.zinc.sdk.domain.ZincAddress;
25
 import fr.sayasoft.zinc.sdk.enums.ShippingMethod;
25
 import fr.sayasoft.zinc.sdk.enums.ShippingMethod;
26
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
26
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
27
-
27
+import fr.sayasoft.zinc.sdk.enums.ZincWebhookType;
28
 import org.hamcrest.core.StringContains;
28
 import org.hamcrest.core.StringContains;
29
+import org.junit.Ignore;
29
 import org.junit.Test;
30
 import org.junit.Test;
30
 import org.junit.runner.RunWith;
31
 import org.junit.runner.RunWith;
31
 import org.springframework.beans.factory.annotation.Autowired;
32
 import org.springframework.beans.factory.annotation.Autowired;
36
 import org.springframework.test.web.servlet.MockMvc;
37
 import org.springframework.test.web.servlet.MockMvc;
37
 
38
 
38
 import java.nio.charset.Charset;
39
 import java.nio.charset.Charset;
40
+import java.util.HashMap;
39
 
41
 
40
 import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE;
42
 import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE;
41
 import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE_TO_BE_REPLACED;
43
 import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE_TO_BE_REPLACED;
200
     }
202
     }
201
 
203
 
202
     @Test
204
     @Test
205
+    @Ignore
206
+    public void postOrder_withWebHooks() throws Exception {
207
+        final String idempotencyKey = "Carina-β-Carinae-Miaplacidus";
208
+        orderRequest.setIdempotencyKey(idempotencyKey);
209
+        orderRequest.setWebhooks(new HashMap<>(2));
210
+//        orderRequest.getWebhooks().put(ZincWebhookType.statusUpdated, "https://reqres.in/api/users");
211
+//        orderRequest.getWebhooks().put(ZincWebhookType.requestSucceeded, "https://reqres.in/api/users");
212
+        orderRequest.getWebhooks().put(ZincWebhookType.statusUpdated, "http://localhost:8080/hook/zinc?eventType=statusUpdated&uuid=abcd");
213
+        orderRequest.getWebhooks().put(ZincWebhookType.requestSucceeded, "http://localhost:8080/hook/zinc?eventType=requestSucceeded&uuid=abcd");
214
+        this.mockMvc.perform(post("/v1/order")
215
+                .contentType(contentType)
216
+                .content(new Gson().toJson(orderRequest)))
217
+                .andDo(print())
218
+                .andExpect(status().isCreated())
219
+                .andExpect(content().string(POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey)));
220
+    }
221
+
222
+    @Test
203
     public void postOrder_KO() throws Exception {
223
     public void postOrder_KO() throws Exception {
204
         final String idempotencyKey = "Ursa-Major-Ursae-Majoris-Phecda";
224
         final String idempotencyKey = "Ursa-Major-Ursae-Majoris-Phecda";
205
         final String clientNotes = ZincErrorCode.invalid_quantity.name();
225
         final String clientNotes = ZincErrorCode.invalid_quantity.name();