|
| 1 | +package com.mindee.input; |
| 2 | + |
| 3 | +import com.mindee.parsing.v2.InferenceResponse; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | + |
| 12 | +import static com.mindee.TestingUtilities.getV2ResourcePath; |
| 13 | + |
| 14 | + |
| 15 | +public class LocalResponseV2Test { |
| 16 | + /** |
| 17 | + * Fake secret key. |
| 18 | + */ |
| 19 | + String secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; |
| 20 | + |
| 21 | + /** |
| 22 | + * Real signature using fake secret key. |
| 23 | + */ |
| 24 | + String signature = "b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be"; |
| 25 | + |
| 26 | + /** |
| 27 | + * File which the signature applies to. |
| 28 | + */ |
| 29 | + Path filePath = getV2ResourcePath("inference/standard_field_types.json"); |
| 30 | + |
| 31 | + protected void assertLocalResponse(LocalResponse localResponse) { |
| 32 | + Assertions.assertNotNull(localResponse.getFile()); |
| 33 | + Assertions.assertFalse(localResponse.isValidHmacSignature( |
| 34 | + this.secretKey, "invalid signature is invalid") |
| 35 | + ); |
| 36 | + Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey)); |
| 37 | + Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature)); |
| 38 | + InferenceResponse response = localResponse.deserializeResponse(InferenceResponse.class); |
| 39 | + Assertions.assertNotNull(response); |
| 40 | + Assertions.assertNotNull(response.getInference()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void loadDocument_withFile_mustReturnValidLocalResponse() throws IOException { |
| 45 | + LocalResponse localResponse = new LocalResponse(new File(this.filePath.toString())); |
| 46 | + assertLocalResponse(localResponse); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void loadDocument_withString_mustReturnValidLocalResponse() { |
| 51 | + LocalResponse localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}"); |
| 52 | + Assertions.assertNotNull(localResponse.getFile()); |
| 53 | + Assertions.assertFalse(localResponse.isValidHmacSignature( |
| 54 | + this.secretKey, "invalid signature is invalid") |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void loadDocument_withInputStream_mustReturnValidLocalResponse() throws IOException { |
| 60 | + LocalResponse localResponse = new LocalResponse( |
| 61 | + Files.newInputStream(this.filePath) |
| 62 | + ); |
| 63 | + assertLocalResponse(localResponse); |
| 64 | + } |
| 65 | +} |
0 commit comments