2020import com .vonage .client .verify2 .VerificationRequest .Builder ;
2121import static org .junit .jupiter .api .Assertions .*;
2222import org .junit .jupiter .api .*;
23+ import java .io .IOException ;
24+ import java .io .InputStream ;
25+ import java .nio .charset .StandardCharsets ;
2326import java .util .Arrays ;
2427import java .util .Collections ;
2528import java .util .Locale ;
@@ -44,6 +47,39 @@ Builder newBuilder() {
4447 return VerificationRequest .builder ().brand (BRAND );
4548 }
4649
50+ /**
51+ * Loads a JSON resource file and replaces placeholders with provided values.
52+ * This follows the pattern used in identityinsights tests for better maintainability.
53+ *
54+ * @param filename The resource filename (relative to this test class)
55+ * @param placeholders Variable arguments of key-value pairs (key, value, key, value, ...)
56+ * @return The JSON string with placeholders replaced
57+ * @throws IOException If the resource cannot be loaded
58+ */
59+ private String loadJsonResource (String filename , String ... placeholders ) throws IOException {
60+ try (InputStream is = getClass ().getResourceAsStream (filename )) {
61+ if (is == null ) {
62+ throw new IOException ("Could not find resource: " + filename );
63+ }
64+ byte [] buffer = new byte [1024 ];
65+ StringBuilder sb = new StringBuilder ();
66+ int bytesRead ;
67+ while ((bytesRead = is .read (buffer )) != -1 ) {
68+ sb .append (new String (buffer , 0 , bytesRead , StandardCharsets .UTF_8 ));
69+ }
70+ String json = sb .toString ().trim ();
71+
72+ // Replace placeholders
73+ for (int i = 0 ; i < placeholders .length ; i += 2 ) {
74+ if (i + 1 < placeholders .length ) {
75+ json = json .replace (placeholders [i ], placeholders [i + 1 ]);
76+ }
77+ }
78+
79+ return json ;
80+ }
81+ }
82+
4783 Builder newBuilderAllParams () {
4884 return newBuilder ().codeLength (CODE_LENGTH ).clientRef (CLIENT_REF )
4985 .channelTimeout (CHANNEL_TIMEOUT ).locale (LOCALE ).fraudCheck ();
@@ -366,6 +402,35 @@ public void testWhatsappWorkflowWithoutSender() {
366402 assertEquals (expectedJson , request .toJson ());
367403 }
368404
405+ @ Test
406+ public void testTemplateId () throws Exception {
407+ String templateId = "4ed3027d-8762-44a0-aa3f-c393717413a4" ;
408+
409+ // Test with SMS workflow - using JSON resource file
410+ String expectedSmsJson = loadJsonResource (
411+ "verification-request-with-template-id.json" ,
412+ "TEMPLATE_ID_PLACEHOLDER" , templateId
413+ );
414+
415+ VerificationRequest smsRequest = newBuilder ()
416+ .addWorkflow (new SmsWorkflow (TO_NUMBER ))
417+ .templateId (templateId )
418+ .build ();
419+ assertEquals (templateId , smsRequest .getTemplateId ());
420+ assertEquals (expectedSmsJson , smsRequest .toJson ());
421+
422+ // Test without template_id - using JSON resource file
423+ String expectedJsonWithoutTemplate = loadJsonResource (
424+ "verification-request-without-template-id.json"
425+ );
426+
427+ VerificationRequest requestWithoutTemplate = newBuilder ()
428+ .addWorkflow (new SmsWorkflow (TO_NUMBER ))
429+ .build ();
430+ assertNull (requestWithoutTemplate .getTemplateId ());
431+ assertEquals (expectedJsonWithoutTemplate , requestWithoutTemplate .toJson ());
432+ }
433+
369434 @ Test
370435 public void triggerJsonProcessingException () {
371436 class SelfRefrencing extends VerificationRequest {
0 commit comments