-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Protobuf data corruption for CloudEvent serialized/deserialized…
… several times (#524) Fixed issue where mutiple serialize/de-serialize operations would result in corrupted data if the data was a protobuf message object. - Introduced equality checks for ProtoDataWrapper. - Refactored and cleaned up data-wrappers. Fixes #523 Signed-off-by: Jem Day <[email protected]>
- Loading branch information
Showing
6 changed files
with
249 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
formats/protobuf/src/test/java/io/cloudevents/protobuf/ProtoDataWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2018-Present The CloudEvents Authors | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package io.cloudevents.protobuf; | ||
|
||
import com.google.protobuf.Any; | ||
import com.google.protobuf.Message; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ProtoDataWrapperTest { | ||
|
||
// == Closing Quotes for 2023/02/23 | ||
private final Message quote1 = io.cloudevents.test.v1.proto.Test.Quote.newBuilder() | ||
.setPrice(io.cloudevents.test.v1.proto.Test.Decimal.newBuilder().setScale(2).setUnscaled(7519).build()) | ||
.setHigh(io.cloudevents.test.v1.proto.Test.Decimal.newBuilder().setScale(2).setUnscaled(7628).build()) | ||
.setSymbol("PYPL") | ||
.build(); | ||
|
||
private final Message quote2 = io.cloudevents.test.v1.proto.Test.Quote.newBuilder() | ||
.setPrice(io.cloudevents.test.v1.proto.Test.Decimal.newBuilder().setScale(2).setUnscaled(13097).build()) | ||
.setHigh(io.cloudevents.test.v1.proto.Test.Decimal.newBuilder().setScale(2).setUnscaled(13170).build()) | ||
.setSymbol("IBM") | ||
.build(); | ||
|
||
@Test | ||
public void testBasic() { | ||
|
||
ProtoDataWrapper pdw = new ProtoDataWrapper(quote1); | ||
|
||
assertThat(pdw).isNotNull(); | ||
assertThat(pdw.getMessage()).isNotNull(); | ||
assertThat(pdw.toBytes()).withFailMessage("toBytes was NULL").isNotNull(); | ||
assertThat(pdw.toBytes()).withFailMessage("toBytes[] returned empty array").hasSizeGreaterThan(0); | ||
|
||
// This is current behavior and will probably change in the next version. | ||
assertThat(pdw.getMessage()).isInstanceOf(io.cloudevents.test.v1.proto.Test.Quote.class); | ||
} | ||
|
||
@Test | ||
public void testEquality() { | ||
|
||
ProtoDataWrapper pdw1 = new ProtoDataWrapper(quote1); | ||
ProtoDataWrapper pdw2 = new ProtoDataWrapper(quote1); | ||
|
||
ProtoDataWrapper pdw3 = new ProtoDataWrapper(quote2); | ||
|
||
assertThat(pdw1).withFailMessage("Self Equality Failed - 1").isEqualTo(pdw1); | ||
assertThat(pdw2).withFailMessage("Self Equality Failed - 2").isEqualTo(pdw2); | ||
assertThat(pdw1).withFailMessage("Self Equality Failed - 3").isEqualTo(pdw2); | ||
assertThat(pdw2).withFailMessage("Self Equality Failed - 4").isEqualTo(pdw1); | ||
|
||
assertThat(pdw1).withFailMessage("Non-Equality Failed - 1").isNotEqualTo(null); | ||
assertThat(pdw1).withFailMessage("Non-Equality Failed - 2").isNotEqualTo(pdw3); | ||
assertThat(pdw3).withFailMessage("Non-Equality Failed - 3").isNotEqualTo(pdw2); | ||
|
||
} | ||
|
||
/** | ||
* Verify the generated bytes[] is correct | ||
*/ | ||
@Test | ||
public void testBytes() { | ||
|
||
// Our expected 'Any' | ||
final Any expAny = Any.pack(quote1); | ||
|
||
// Our expected 'data' | ||
final byte[] expData = expAny.toByteArray(); | ||
|
||
// Build the wrapper | ||
final ProtoDataWrapper pdw = new ProtoDataWrapper(quote1); | ||
|
||
// Get the actual data | ||
final byte[] actData = pdw.toBytes(); | ||
|
||
// Verify | ||
Arrays.equals(expData, actData); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.