File tree Expand file tree Collapse file tree 5 files changed +32
-19
lines changed
Expand file tree Collapse file tree 5 files changed +32
-19
lines changed Original file line number Diff line number Diff line change 3434
3535 - name : Tests sample code
3636 run : |
37- ./tests/test_code_samples.sh ${{ secrets.MINDEE_ACCOUNT_SE_TESTS }} ${{ secrets.MINDEE_ENDPOINT_SE_TESTS }} ${{ secrets.MINDEE_API_KEY_SE_TESTS }} ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }} ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
37+ ./tests/test_code_samples.sh ${{ secrets.MINDEE_ACCOUNT_SE_TESTS }} ${{ secrets.MINDEE_ENDPOINT_SE_TESTS }} ${{ secrets.MINDEE_API_KEY_SE_TESTS }} ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }} ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
Original file line number Diff line number Diff line change @@ -103,18 +103,6 @@ else if (resp.getJob().getStatus().equals("Processed")) {
103103 throw new RuntimeException ("Max retries exceeded (" + max + ")." );
104104 }
105105
106- /**
107- * Deserialize a webhook payload (or any saved response) into an
108- * {@link InferenceResponse}.
109- */
110- public InferenceResponse loadInference (LocalResponse localResponse ) throws IOException {
111- ObjectMapper mapper = new ObjectMapper ().findAndRegisterModules ();
112- InferenceResponse model =
113- mapper .readValue (localResponse .getFile (), InferenceResponse .class );
114- model .setRawResponse (localResponse .toString ());
115- return model ;
116- }
117-
118106 private static MindeeApiV2 createDefaultApiV2 (String apiKey ) {
119107 MindeeSettingsV2 settings = apiKey == null || apiKey .trim ().isEmpty ()
120108 ? new MindeeSettingsV2 ()
Original file line number Diff line number Diff line change 1313import java .util .stream .Stream ;
1414import javax .crypto .Mac ;
1515import javax .crypto .spec .SecretKeySpec ;
16+
17+ import com .fasterxml .jackson .databind .ObjectMapper ;
18+ import com .mindee .MindeeException ;
19+ import com .mindee .parsing .v2 .CommonResponse ;
1620import lombok .Getter ;
1721import org .apache .commons .codec .binary .Hex ;
1822
@@ -91,4 +95,26 @@ public String getHmacSignature(String secretKey) {
9195 public boolean isValidHmacSignature (String secretKey , String signature ) {
9296 return signature .equals (getHmacSignature (secretKey ));
9397 }
98+
99+
100+ /**
101+ * Deserialize this local JSON payload into a specific {@link CommonResponse}
102+ * subtype: {@code InferenceResponse}, {@code JobResponse}.
103+ *
104+ * @param responseClass the concrete class to instantiate
105+ * @param <T> generic {@link CommonResponse}
106+ * @return a fully populated instance of {@code responseClass}
107+ * @throws MindeeException if the payload cannot be deserialized into the
108+ * requested type
109+ */
110+ public <T extends CommonResponse > T deserializeResponse (Class <T > responseClass ) {
111+ ObjectMapper mapper = new ObjectMapper ();
112+ try {
113+ T response = mapper .readValue (this .file , responseClass );
114+ response .setRawResponse (new String (this .file , StandardCharsets .UTF_8 ));
115+ return response ;
116+ } catch (Exception ex ) {
117+ throw new MindeeException ("Invalid class specified for deserialization." , ex );
118+ }
119+ }
94120}
Original file line number Diff line number Diff line change @@ -114,18 +114,17 @@ void document_getInference_async() throws IOException {
114114 }
115115
116116 @ Nested
117- @ DisplayName ("loadInference ()" )
118- class LoadInference {
117+ @ DisplayName ("deserializeResponse ()" )
118+ class DeserializeResponse {
119119
120120 @ Test
121121 @ DisplayName ("parses local JSON and exposes correct field values" )
122122 void inference_loadsLocally () throws IOException {
123- MindeeClientV2 mindeeClient = new MindeeClientV2 ("dummy" );
124123 File jsonFile =
125124 new File ("src/test/resources/v2/products/financial_document/complete.json" );
126125 LocalResponse localResponse = new LocalResponse (jsonFile );
127126
128- InferenceResponse loaded = mindeeClient . loadInference ( localResponse );
127+ InferenceResponse loaded = localResponse . deserializeResponse ( InferenceResponse . class );
129128
130129 assertNotNull (loaded , "Loaded InferenceResponse must not be null" );
131130 assertEquals (
Original file line number Diff line number Diff line change 1919class InferenceTest {
2020
2121 private InferenceResponse loadFromResource (String resourcePath ) throws IOException {
22- MindeeClientV2 dummyClient = new MindeeClientV2 ( "dummy" );
23- return dummyClient . loadInference ( new LocalResponse ( InferenceTest .class . getClassLoader (). getResourceAsStream ( resourcePath )) );
22+ LocalResponse localResponse = new LocalResponse ( InferenceTest . class . getClassLoader (). getResourceAsStream ( resourcePath ) );
23+ return localResponse . deserializeResponse ( InferenceResponse .class );
2424 }
2525
2626 private String readFileAsString (String path )
You can’t perform that action at this time.
0 commit comments