|
1 | 1 | /*
|
2 |
| - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License").
|
5 | 5 | * You may not use this file except in compliance with the License.
|
|
16 | 16 | package com.amazon.randomcutforest.parkservices.state;
|
17 | 17 |
|
18 | 18 | import com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest;
|
| 19 | +import com.fasterxml.jackson.core.JsonProcessingException; |
19 | 20 | import com.fasterxml.jackson.databind.MapperFeature;
|
20 | 21 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 22 | +import io.protostuff.ProtostuffIOUtil; |
| 23 | +import io.protostuff.Schema; |
| 24 | +import io.protostuff.runtime.RuntimeSchema; |
21 | 25 | import org.junit.jupiter.params.ParameterizedTest;
|
22 | 26 | import org.junit.jupiter.params.provider.EnumSource;
|
23 | 27 |
|
|
26 | 30 | import java.io.InputStream;
|
27 | 31 | import java.io.InputStreamReader;
|
28 | 32 | import java.nio.charset.StandardCharsets;
|
| 33 | +import java.util.Base64; |
29 | 34 |
|
| 35 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
30 | 36 | import static org.junit.jupiter.api.Assertions.fail;
|
31 | 37 |
|
32 | 38 | public class V2TRCFToV3StateConverterTest {
|
33 | 39 |
|
| 40 | + private ThresholdedRandomCutForestMapper trcfMapper = new ThresholdedRandomCutForestMapper(); |
| 41 | + |
34 | 42 | @ParameterizedTest
|
35 | 43 | @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 | + } |
37 | 53 |
|
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 | + } |
40 | 66 |
|
| 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))) { |
41 | 70 | StringBuilder b = new StringBuilder();
|
42 | 71 | String line;
|
43 | 72 | while ((line = rr.readLine()) != null) {
|
44 | 73 | b.append(line);
|
45 | 74 | }
|
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(); |
56 | 76 | } catch (IOException e) {
|
57 |
| - fail("Unable to load JSON resource"); |
| 77 | + fail("Unable to load resource"); |
58 | 78 | }
|
| 79 | + return null; |
59 | 80 | }
|
60 | 81 |
|
61 | 82 | }
|
0 commit comments