|
| 1 | +/* |
| 2 | + * Copyright 2026 Flamingock (https://www.flamingock.io) |
| 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 io.flamingock.cloud.api.request; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import io.flamingock.api.StageType; |
| 22 | +import io.flamingock.cloud.api.vo.CloudStageStatus; |
| 23 | +import io.flamingock.cloud.api.vo.CloudTargetSystemAuditMarkType; |
| 24 | +import org.junit.jupiter.api.DisplayName; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +/** |
| 37 | + * Wire-contract serialization tests for {@link ClientSubmissionRequest}. Pins the JSON shape |
| 38 | + * consumed by the cloud server. A divergence here is the canonical "wire mismatch" symptom. |
| 39 | + */ |
| 40 | +class ClientSubmissionRequestSerializationTest { |
| 41 | + |
| 42 | + // Match production mapper configuration (see JsonObjectMapper.DEFAULT_INSTANCE in |
| 43 | + // flamingock-java-general-util): unknown properties are silently ignored on the wire. |
| 44 | + private static final ObjectMapper MAPPER = new ObjectMapper() |
| 45 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("Serializes blocks in input order with type + stages") |
| 49 | + void serializesBlocksWithTypeAndStages() throws Exception { |
| 50 | + StageRequest sysStage = new StageRequest( |
| 51 | + "system-stage", 0, CloudStageStatus.NOT_STARTED, |
| 52 | + Collections.singletonList(new ChangeRequest("sys-c1", |
| 53 | + CloudTargetSystemAuditMarkType.NONE, false))); |
| 54 | + StageRequest userStage = new StageRequest( |
| 55 | + "user-stage", 1, CloudStageStatus.NOT_STARTED, |
| 56 | + Collections.singletonList(new ChangeRequest("user-c1", |
| 57 | + CloudTargetSystemAuditMarkType.NONE, false))); |
| 58 | + |
| 59 | + ClientSubmissionRequest request = new ClientSubmissionRequest(Arrays.asList( |
| 60 | + new StageBlockRequest(StageType.SYSTEM, Collections.singletonList(sysStage)), |
| 61 | + new StageBlockRequest(StageType.DEFAULT, Collections.singletonList(userStage)))); |
| 62 | + |
| 63 | + JsonNode json = MAPPER.valueToTree(request); |
| 64 | + |
| 65 | + assertNotNull(json.get("blocks")); |
| 66 | + assertEquals(2, json.get("blocks").size()); |
| 67 | + |
| 68 | + JsonNode block0 = json.get("blocks").get(0); |
| 69 | + assertEquals("SYSTEM", block0.get("type").asText()); |
| 70 | + assertEquals(1, block0.get("stages").size()); |
| 71 | + assertEquals("system-stage", block0.get("stages").get(0).get("name").asText()); |
| 72 | + |
| 73 | + JsonNode block1 = json.get("blocks").get(1); |
| 74 | + assertEquals("DEFAULT", block1.get("type").asText()); |
| 75 | + assertEquals(1, block1.get("stages").size()); |
| 76 | + assertEquals("user-stage", block1.get("stages").get(0).get("name").asText()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("Round-trips through Jackson preserving block order and contents") |
| 81 | + void roundTripsPreservingBlockOrderAndContents() throws Exception { |
| 82 | + ClientSubmissionRequest original = new ClientSubmissionRequest(Arrays.asList( |
| 83 | + new StageBlockRequest(StageType.LEGACY, Collections.singletonList( |
| 84 | + new StageRequest("legacy", 0, CloudStageStatus.COMPLETED, |
| 85 | + Collections.singletonList(new ChangeRequest("legacy-c1", |
| 86 | + CloudTargetSystemAuditMarkType.APPLIED, true))))), |
| 87 | + new StageBlockRequest(StageType.DEFAULT, Arrays.asList( |
| 88 | + new StageRequest("user-a", 1, CloudStageStatus.STARTED, |
| 89 | + Collections.singletonList(new ChangeRequest("user-a-c1", |
| 90 | + CloudTargetSystemAuditMarkType.NONE, false))), |
| 91 | + new StageRequest("user-b", 2, CloudStageStatus.NOT_STARTED, |
| 92 | + Collections.singletonList(new ChangeRequest("user-b-c1", |
| 93 | + CloudTargetSystemAuditMarkType.NONE, false))))))); |
| 94 | + |
| 95 | + String json = MAPPER.writeValueAsString(original); |
| 96 | + ClientSubmissionRequest deserialized = MAPPER.readValue(json, ClientSubmissionRequest.class); |
| 97 | + |
| 98 | + assertEquals(original, deserialized); |
| 99 | + // Block order is significant and preserved verbatim. |
| 100 | + assertEquals(2, deserialized.getBlocks().size()); |
| 101 | + assertEquals(StageType.LEGACY, deserialized.getBlocks().get(0).getType()); |
| 102 | + assertEquals(StageType.DEFAULT, deserialized.getBlocks().get(1).getType()); |
| 103 | + assertEquals(2, deserialized.getBlocks().get(1).getStages().size()); |
| 104 | + assertEquals("user-a", deserialized.getBlocks().get(1).getStages().get(0).getName()); |
| 105 | + assertEquals("user-b", deserialized.getBlocks().get(1).getStages().get(1).getName()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @DisplayName("Same StageType repeated across multiple blocks is preserved on the wire (multi-block-same-type lock-in)") |
| 110 | + void sameStageTypeAcrossMultipleBlocksIsPreserved() throws Exception { |
| 111 | + StageRequest a = new StageRequest("user-a", 0, CloudStageStatus.NOT_STARTED, |
| 112 | + Collections.singletonList(new ChangeRequest("a-c1", |
| 113 | + CloudTargetSystemAuditMarkType.NONE, false))); |
| 114 | + StageRequest b = new StageRequest("user-b", 1, CloudStageStatus.NOT_STARTED, |
| 115 | + Collections.singletonList(new ChangeRequest("b-c1", |
| 116 | + CloudTargetSystemAuditMarkType.NONE, false))); |
| 117 | + |
| 118 | + ClientSubmissionRequest request = new ClientSubmissionRequest(Arrays.asList( |
| 119 | + new StageBlockRequest(StageType.DEFAULT, Collections.singletonList(a)), |
| 120 | + new StageBlockRequest(StageType.DEFAULT, Collections.singletonList(b)))); |
| 121 | + |
| 122 | + String json = MAPPER.writeValueAsString(request); |
| 123 | + ClientSubmissionRequest deserialized = MAPPER.readValue(json, ClientSubmissionRequest.class); |
| 124 | + |
| 125 | + // Two distinct blocks of the same StageType, NOT collapsed into one. |
| 126 | + assertEquals(2, deserialized.getBlocks().size()); |
| 127 | + assertEquals(StageType.DEFAULT, deserialized.getBlocks().get(0).getType()); |
| 128 | + assertEquals(StageType.DEFAULT, deserialized.getBlocks().get(1).getType()); |
| 129 | + assertEquals("user-a", deserialized.getBlocks().get(0).getStages().get(0).getName()); |
| 130 | + assertEquals("user-b", deserialized.getBlocks().get(1).getStages().get(0).getName()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("Empty blocks list serializes and deserializes cleanly") |
| 135 | + void emptyBlocksListRoundTrips() throws Exception { |
| 136 | + ClientSubmissionRequest original = new ClientSubmissionRequest(Collections.<StageBlockRequest>emptyList()); |
| 137 | + |
| 138 | + String json = MAPPER.writeValueAsString(original); |
| 139 | + ClientSubmissionRequest deserialized = MAPPER.readValue(json, ClientSubmissionRequest.class); |
| 140 | + |
| 141 | + assertNotNull(deserialized.getBlocks()); |
| 142 | + assertTrue(deserialized.getBlocks().isEmpty()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @DisplayName("Old flat 'stages' field at top level is silently ignored (clean cut — no fallback)") |
| 147 | + void oldFlatStagesFieldIsIgnored() throws Exception { |
| 148 | + // Wire format from a hypothetical old client: top-level `stages` instead of `blocks`. |
| 149 | + // The new server-side DTO has no `stages` getter/setter, so the field is dropped. |
| 150 | + String oldFormat = "{\"stages\":[{\"name\":\"legacy-flat\",\"order\":0}]}"; |
| 151 | + ClientSubmissionRequest deserialized = MAPPER.readValue(oldFormat, ClientSubmissionRequest.class); |
| 152 | + |
| 153 | + // No fallback — blocks is null (or empty) because the old field was ignored. |
| 154 | + assertNull(deserialized.getBlocks()); |
| 155 | + } |
| 156 | +} |
0 commit comments