Kaynağa Gözat

added feature: the FakeController can call back the webhooks

Jonathan LALOU 7 yıl önce
ebeveyn
işleme
7d845f5de9

+ 32
- 14
src/main/java/fr/sayasoft/fake/zinc/FakeZincController.java Dosyayı Görüntüle

@@ -2,6 +2,7 @@ package fr.sayasoft.fake.zinc;
2 2
 
3 3
 import com.google.gson.Gson;
4 4
 import fr.sayasoft.zinc.sdk.domain.OrderRequest;
5
+import fr.sayasoft.zinc.sdk.domain.OrderResponse;
5 6
 import fr.sayasoft.zinc.sdk.domain.ZincConstants;
6 7
 import fr.sayasoft.zinc.sdk.domain.ZincError;
7 8
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
14 15
 import org.springframework.web.bind.annotation.RequestMethod;
15 16
 import org.springframework.web.bind.annotation.RequestParam;
16 17
 import org.springframework.web.bind.annotation.RestController;
18
+import org.springframework.web.client.RestTemplate;
17 19
 
18 20
 import java.util.concurrent.atomic.AtomicLong;
19 21
 
@@ -93,35 +95,51 @@ public class FakeZincController {
93 95
         final String idempotencyKey = orderRequest.getIdempotencyKey();
94 96
 
95 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 109
             Precision obtained from Zinc support: although an error message is returned, the HTTP header is a 200
108 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 116
         } catch (IllegalArgumentException e) {
114 117
             // nothing to do
115
-            if (log.isInfoEnabled()){
118
+            if (log.isInfoEnabled()) {
116 119
                 log.info("received clientNotes: " + orderRequest.getClientNotes() + " ; will go on");
117 120
             }
118 121
         } catch (Exception e) {
119 122
             log.error("An error occured", e);
120 123
         }
121 124
 
125
+        if (!orderRequest.getWebhooks().isEmpty()) {
126
+            callbackWebHooks(orderRequest);
127
+        }
128
+
122 129
         return new ResponseEntity<>(
123 130
                 POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey),
124 131
                 HttpStatus.CREATED);
125 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 Dosyayı Görüntüle

@@ -24,8 +24,9 @@ import fr.sayasoft.zinc.sdk.domain.RetailerCredentials;
24 24
 import fr.sayasoft.zinc.sdk.domain.ZincAddress;
25 25
 import fr.sayasoft.zinc.sdk.enums.ShippingMethod;
26 26
 import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
27
-
27
+import fr.sayasoft.zinc.sdk.enums.ZincWebhookType;
28 28
 import org.hamcrest.core.StringContains;
29
+import org.junit.Ignore;
29 30
 import org.junit.Test;
30 31
 import org.junit.runner.RunWith;
31 32
 import org.springframework.beans.factory.annotation.Autowired;
@@ -36,6 +37,7 @@ import org.springframework.test.context.junit4.SpringRunner;
36 37
 import org.springframework.test.web.servlet.MockMvc;
37 38
 
38 39
 import java.nio.charset.Charset;
40
+import java.util.HashMap;
39 41
 
40 42
 import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE;
41 43
 import static fr.sayasoft.fake.zinc.FakeZincController.POST_ORDER_RESPONSE_TO_BE_REPLACED;
@@ -200,6 +202,24 @@ public class FakeZincControllerUnitTest {
200 202
     }
201 203
 
202 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 223
     public void postOrder_KO() throws Exception {
204 224
         final String idempotencyKey = "Ursa-Major-Ursae-Majoris-Phecda";
205 225
         final String clientNotes = ZincErrorCode.invalid_quantity.name();