Procházet zdrojové kódy

* [master 316dd48] Fake Zinc: added feature: can test errors are returned

Jonathan LALOU před 7 roky
rodič
revize
aa57c8a1ec

+ 33
- 11
src/main/java/fr/sayasoft/fake/zinc/FakeZincController.java Zobrazit soubor

@@ -1,9 +1,10 @@
1 1
 package fr.sayasoft.fake.zinc;
2 2
 
3
+import com.google.gson.Gson;
3 4
 import fr.sayasoft.zinc.sdk.domain.OrderRequest;
5
+import fr.sayasoft.zinc.sdk.domain.ZincError;
6
+import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
4 7
 import lombok.extern.log4j.Log4j;
5
-import org.apache.commons.lang3.builder.ToStringBuilder;
6
-import org.apache.commons.lang3.builder.ToStringStyle;
7 8
 import org.springframework.http.HttpStatus;
8 9
 import org.springframework.http.ResponseEntity;
9 10
 import org.springframework.web.bind.annotation.PathVariable;
@@ -70,21 +71,42 @@ public class FakeZincController {
70 71
         return GET_ORDER_RESPONSE;
71 72
     }
72 73
 
74
+    /**
75
+     * Fake method to test order posting
76
+     * Conventions for testing:
77
+     * <ul>
78
+     * <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
+     * </ul>
81
+     */
73 82
     @SuppressWarnings("unused")
74 83
     @RequestMapping(
75 84
             value = "/v1/order",
76 85
             method = RequestMethod.POST,
77 86
             produces = "application/json; charset=UTF-8"
78 87
     )
79
-    public ResponseEntity<?> postOrder(@RequestBody OrderRequest orderRequest) {
80
-// FIXME: at this point, the orderRequest, for an unknown reason, is not well deserialized
81
-        // (except the field 'retailer'). But it *was* well serialized on the client side...
88
+    public ResponseEntity<?> postOrder(@RequestBody String json) {
89
+        final Gson gson = new Gson();
90
+        final OrderRequest orderRequest = gson.fromJson(json, OrderRequest.class);
82 91
 
83
-//        if (log.isDebugEnabled()) {
84
-//            log.debug(ToStringBuilder.reflectionToString(orderRequest));
85
-//        }
86
-        System.out.println(ToStringBuilder.reflectionToString(orderRequest, ToStringStyle.MULTI_LINE_STYLE));
87
-        return new ResponseEntity<>(POST_ORDER_RESPONSE, HttpStatus.CREATED);
88
-//        return new ResponseEntity<>(POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, orderRequest.getIdempotencyKey()), HttpStatus.CREATED);
92
+        try {
93
+            final ZincErrorCode zincErrorCode;
94
+            zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
95
+            final ZincError zincError = new ZincError(
96
+                    zincErrorCode,
97
+                    zincErrorCode.getMeaning(),
98
+                    orderRequest.getIdempotencyKey()
99
+            );
100
+            log.info("Received request to generate error code, returning: " + zincError);
101
+            return new ResponseEntity<>(
102
+                    zincError,
103
+                    HttpStatus.BAD_REQUEST); // TODO check and confirm the actual API returns an error code 400
104
+        } catch (Exception e) {
105
+            e.printStackTrace();
106
+        }
107
+
108
+        return new ResponseEntity<>(
109
+                POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, orderRequest.getIdempotencyKey()),
110
+                HttpStatus.CREATED);
89 111
     }
90 112
 }