Skip to content

Commit 789a1ae

Browse files
authored
add bwc test for byte base64 TRCF state (#315)
Signed-off-by: Yaliang Wu <ylwu@amazon.com>
1 parent 3ce04ae commit 789a1ae

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

Java/parkservices/pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,17 @@
5555
<version>2.12.6.1</version>
5656
<scope>test</scope>
5757
</dependency>
58+
<dependency>
59+
<groupId>io.protostuff</groupId>
60+
<artifactId>protostuff-core</artifactId>
61+
<version>1.7.2</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.protostuff</groupId>
66+
<artifactId>protostuff-runtime</artifactId>
67+
<version>1.7.2</version>
68+
<scope>test</scope>
69+
</dependency>
5870
</dependencies>
5971
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazon.randomcutforest.parkservices.state;
17+
18+
import lombok.Getter;
19+
20+
@Getter
21+
public enum V2TRCFByteBase64Resource {
22+
23+
TRCF_STATE_1("byte_base64_1.txt"),
24+
TRCF_STATE_2("byte_base64_2.txt");
25+
26+
private final String resource;
27+
28+
V2TRCFByteBase64Resource(String resource) {
29+
this.resource = resource;
30+
}
31+
}

Java/parkservices/src/test/java/com/amazon/randomcutforest/parkservices/state/V2TRCFJsonResource.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -20,7 +20,8 @@
2020
@Getter
2121
public enum V2TRCFJsonResource {
2222

23-
TRCF_1("state_1.json"), TRCF_2("state_2.json");
23+
TRCF_1("state_1.json"),
24+
TRCF_2("state_2.json");
2425

2526
private final String resource;
2627

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -16,8 +16,12 @@
1616
package com.amazon.randomcutforest.parkservices.state;
1717

1818
import com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest;
19+
import com.fasterxml.jackson.core.JsonProcessingException;
1920
import com.fasterxml.jackson.databind.MapperFeature;
2021
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import io.protostuff.ProtostuffIOUtil;
23+
import io.protostuff.Schema;
24+
import io.protostuff.runtime.RuntimeSchema;
2125
import org.junit.jupiter.params.ParameterizedTest;
2226
import org.junit.jupiter.params.provider.EnumSource;
2327

@@ -26,36 +30,53 @@
2630
import java.io.InputStream;
2731
import java.io.InputStreamReader;
2832
import java.nio.charset.StandardCharsets;
33+
import java.util.Base64;
2934

35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3036
import static org.junit.jupiter.api.Assertions.fail;
3137

3238
public class V2TRCFToV3StateConverterTest {
3339

40+
private ThresholdedRandomCutForestMapper trcfMapper = new ThresholdedRandomCutForestMapper();
41+
3442
@ParameterizedTest
3543
@EnumSource(V2TRCFJsonResource.class)
36-
public void test(V2TRCFJsonResource jsonResource) {
44+
public void testJson(V2TRCFJsonResource jsonResource) throws JsonProcessingException {
45+
String json = getStateFromFile(jsonResource.getResource());
46+
assertNotNull(json);
47+
ObjectMapper mapper = new ObjectMapper();
48+
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
49+
ThresholdedRandomCutForestState state = mapper.readValue(json, ThresholdedRandomCutForestState.class);
50+
ThresholdedRandomCutForest forest = trcfMapper.toModel(state);
51+
assertNotNull(forest);
52+
}
3753

38-
try (InputStream is = V2TRCFToV3StateConverterTest.class.getResourceAsStream(jsonResource.getResource());
39-
BufferedReader rr = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));) {
54+
@ParameterizedTest
55+
@EnumSource(V2TRCFByteBase64Resource.class)
56+
public void testByteBase64(V2TRCFByteBase64Resource byteBase64Resource) {
57+
String byteBase64 = getStateFromFile(byteBase64Resource.getResource());
58+
assertNotNull(byteBase64);
59+
Schema<ThresholdedRandomCutForestState> trcfSchema = RuntimeSchema.getSchema(ThresholdedRandomCutForestState.class);
60+
byte[] bytes = Base64.getDecoder().decode(byteBase64);
61+
ThresholdedRandomCutForestState state = trcfSchema.newMessage();
62+
ProtostuffIOUtil.mergeFrom(bytes, state, trcfSchema);
63+
ThresholdedRandomCutForest forest = trcfMapper.toModel(state);
64+
assertNotNull(forest);
65+
}
4066

67+
private String getStateFromFile(String resourceFile) {
68+
try (InputStream is = V2TRCFToV3StateConverterTest.class.getResourceAsStream(resourceFile);
69+
BufferedReader rr = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
4170
StringBuilder b = new StringBuilder();
4271
String line;
4372
while ((line = rr.readLine()) != null) {
4473
b.append(line);
4574
}
46-
47-
String json = b.toString();
48-
49-
ObjectMapper mapper = new ObjectMapper();
50-
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
51-
ThresholdedRandomCutForestState state = mapper.readValue(json, ThresholdedRandomCutForestState.class);
52-
53-
ThresholdedRandomCutForestMapper mapper1 = new ThresholdedRandomCutForestMapper();
54-
ThresholdedRandomCutForest forest = mapper1.toModel(state);
55-
75+
return b.toString();
5676
} catch (IOException e) {
57-
fail("Unable to load JSON resource");
77+
fail("Unable to load resource");
5878
}
79+
return null;
5980
}
6081

6182
}

Java/parkservices/src/test/resources/com/amazon/randomcutforest/parkservices/state/byte_base64_1.txt

+1
Large diffs are not rendered by default.

Java/parkservices/src/test/resources/com/amazon/randomcutforest/parkservices/state/byte_base64_2.txt

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)