Skip to content

Commit 76f4fbb

Browse files
Merge pull request #1957 from SradhaMohanty5899/MOSIP-45003
MOSIP-45003: Fix Handlebars escaping, correct JSON array tags, and add modifySchemaGenerateHbsV2.
2 parents 429ceb7 + e2e68ac commit 76f4fbb

2 files changed

Lines changed: 75 additions & 21 deletions

File tree

apitest-commons/src/main/java/io/mosip/testrig/apirig/utils/AdminTestUtil.java

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@
125125
import io.mosip.testrig.apirig.testrunner.OTPListener;
126126
import io.restassured.http.ContentType;
127127
import io.restassured.response.Response;
128+
import com.github.jknack.handlebars.EscapingStrategy;
129+
import com.github.jknack.handlebars.Helper;
130+
import com.github.jknack.handlebars.Options;
128131

129132
/**
130133
* @author Ravi Kant
@@ -3816,8 +3819,16 @@ public String getJsonFromTemplate(String input, String template) {
38163819

38173820
}
38183821

3819-
public static Handlebars handlebars = new Handlebars();
3822+
public static Handlebars handlebars = new Handlebars().with(EscapingStrategy.NOOP);
38203823
public static Gson gson = new Gson();
3824+
static {
3825+
handlebars.registerHelper("json", new Helper<Object>() {
3826+
@Override
3827+
public CharSequence apply(Object context, Options options) {
3828+
return gson.toJson(context);
3829+
}
3830+
});
3831+
}
38213832

38223833
public String getJsonFromTemplate(String input, String template, boolean readFile) {
38233834
String resultJson = null;
@@ -5446,7 +5457,7 @@ public static String modifySchemaGenerateHbs(boolean regenerateHbs) {
54465457
JSONObject identityJson = new JSONObject();
54475458
identityJson.put("UIN", "{{UIN}}");
54485459
JSONArray handleArray = new JSONArray();
5449-
handleArray.put("handles");
5460+
handleArray.put("handle");
54505461

54515462
List<String> selectedHandles = new ArrayList<>();
54525463

@@ -5643,6 +5654,21 @@ public static String modifySchemaGenerateHbs(boolean regenerateHbs) {
56435654
identityHbs = requestJson.toString();
56445655
return identityHbs;
56455656
}
5657+
5658+
public static String modifySchemaGenerateHbsV2(boolean regenerateHbs) {
5659+
5660+
String hbs = modifySchemaGenerateHbs(regenerateHbs);
5661+
5662+
JSONObject requestJson = new JSONObject(hbs);
5663+
requestJson.getJSONObject("request").put("verifiedAttributes", "$VERIFIED_ATTRIBUTES$");
5664+
5665+
return requestJson.toString().replace("\"$VERIFIED_ATTRIBUTES$\"", buildVerifiedAttributesHbs());
5666+
}
5667+
5668+
private static String buildVerifiedAttributesHbs() {
5669+
5670+
return "{{{json verifiedAttributes}}}";
5671+
}
56465672

56475673
public static String getSchemaURL() {
56485674
String schemaURL = ApplnURI + properties.getProperty(GlobalConstants.MASTER_SCHEMA_URL);
@@ -5706,7 +5732,7 @@ public static String updateIdentityHbs(boolean regenerateHbs) {
57065732
JSONObject identityJson = new JSONObject();
57075733
identityJson.put("UIN", "{{UIN}}");
57085734
JSONArray handleArray = new JSONArray();
5709-
handleArray.put("handles");
5735+
handleArray.put("handle");
57105736
List<String> selectedHandles = new ArrayList<>();
57115737

57125738
for (int i = 0, size = requiredPropsArray.length(); i < size; i++) {
@@ -5719,19 +5745,26 @@ public static String updateIdentityHbs(boolean regenerateHbs) {
57195745
boolean isHandle = eachPropDataJson.optBoolean("handle", false);
57205746

57215747
if (!ref.isEmpty() && ref.contains("TaggedListType")) {
5722-
JSONArray arr = new JSONArray();
5723-
JSONObject entry = new JSONObject();
5748+
// Legacy: backward compatibility for schemas using TaggedListType $ref
5749+
JSONArray eachPropDataArrayForHandles = new JSONArray();
5750+
JSONObject eachValueJsonForHandles = new JSONObject();
57245751
if (eachRequiredProp.equals(emailFieldName)) {
5725-
entry.put("value", "$EMAILVALUE$"); entry.put("tags", handleArray); selectedHandles.add(emailFieldName);
5752+
eachValueJsonForHandles.put("value", "{{" + eachRequiredProp + "}}");
5753+
eachValueJsonForHandles.put("tags", handleArray);
5754+
selectedHandles.add(emailFieldName);
57265755
} else if (eachRequiredProp.equals(phoneFieldName)) {
5727-
entry.put("value", "{{phone}}"); entry.put("tags", handleArray); selectedHandles.add(phoneFieldName);
5756+
eachValueJsonForHandles.put("value", "{{phone}}");
5757+
eachValueJsonForHandles.put("tags", handleArray);
5758+
selectedHandles.add(phoneFieldName);
57285759
} else {
5729-
entry.put("value", "$FUNCTIONALID$"); entry.put("tags", handleArray); selectedHandles.add(eachRequiredProp);
5760+
eachValueJsonForHandles.put("value", "$FUNCTIONALID$");
5761+
eachValueJsonForHandles.put("tags", handleArray);
5762+
selectedHandles.add(eachRequiredProp);
57305763
}
5731-
arr.put(entry);
5732-
identityJson.put(eachRequiredProp, arr);
5764+
eachPropDataArrayForHandles.put(eachValueJsonForHandles);
5765+
identityJson.put(eachRequiredProp, eachPropDataArrayForHandles);
57335766

5734-
} else if (!ref.isEmpty() && ref.contains("simpleType")) {
5767+
} else if (!ref.isEmpty() && ref.contains("simpleType")) {
57355768
if (isHandle) selectedHandles.add(eachRequiredProp);
57365769
JSONArray arr = new JSONArray();
57375770
for (int j = 0; j < BaseTestCase.getLanguageList().size(); j++) {
@@ -5778,18 +5811,27 @@ else if (eachRequiredProp.contains(GlobalConstants.GENDER))
57785811
else
57795812
identityJson.put(eachRequiredProp, "{{" + eachRequiredProp + "}}");
57805813
} else if (eachRequiredProp.equals(phoneFieldName)) {
5781-
if (isHandle) selectedHandles.add(eachRequiredProp);
5814+
if (isHandle) {
5815+
selectedHandles.add(eachRequiredProp);
5816+
}
57825817
identityJson.put(eachRequiredProp, "{{phone}}");
5818+
57835819
} else if (eachRequiredProp.equals(emailFieldName)) {
5784-
if (isHandle) selectedHandles.add(eachRequiredProp);
5820+
if (isHandle) {
5821+
selectedHandles.add(eachRequiredProp);
5822+
}
57855823
if ("array".equals(fieldType)) {
5824+
// Inline array handle (e.g. schema where email is type:array + handle:true)
57865825
JSONArray emailArray = new JSONArray();
57875826
JSONObject emailEntry = new JSONObject();
5788-
emailEntry.put("value", "$EMAILVALUE$"); emailEntry.put("tags", handleArray);
5789-
emailArray.put(emailEntry); identityJson.put(eachRequiredProp, emailArray);
5827+
emailEntry.put("value", "{{" + eachRequiredProp + "}}");
5828+
emailEntry.put("tags", handleArray);
5829+
emailArray.put(emailEntry);
5830+
identityJson.put(eachRequiredProp, emailArray);
57905831
} else {
5791-
identityJson.put(eachRequiredProp, "$EMAILVALUE$");
5832+
identityJson.put(eachRequiredProp, "{{" + eachRequiredProp + "}}");
57925833
}
5834+
57935835
} else if ("array".equals(fieldType) && isHandle) {
57945836
JSONArray arrayHandle = new JSONArray();
57955837
JSONObject handleEntry = new JSONObject();
@@ -5822,6 +5864,16 @@ else if (eachRequiredProp.contains(GlobalConstants.GENDER))
58225864
updateIdentityHbs = requestJson.toString();
58235865
return updateIdentityHbs;
58245866
}
5867+
5868+
public static String updateIdentityHbsV2(boolean regenerateHbs) {
5869+
5870+
String hbs = updateIdentityHbs(regenerateHbs);
5871+
5872+
JSONObject requestJson = new JSONObject(hbs);
5873+
requestJson.getJSONObject("request").put("verifiedAttributes", "$VERIFIED_ATTRIBUTES$");
5874+
5875+
return requestJson.toString().replace("\"$VERIFIED_ATTRIBUTES$\"", buildVerifiedAttributesHbs());
5876+
}
58255877

58265878
public static String generateLatestSchemaVersion() {
58275879
kernelAuthLib = new KernelAuthentication();
@@ -5892,7 +5944,7 @@ public static String generateHbsForUpdateDraft() {
58925944
requestJson.put("registrationId", "{{registrationId}}");
58935945
JSONObject identityJson = new JSONObject();
58945946
JSONArray handleArray = new JSONArray();
5895-
handleArray.put("handles");
5947+
handleArray.put("handle");
58965948
List<String> selectedHandles = new ArrayList<>();
58975949

58985950
for (int i = 0, size = requiredPropsArray.length(); i < size; i++) {
@@ -6129,11 +6181,11 @@ public static String generateHbsForPrereg(boolean isItUpdate) {
61296181
JSONArray arr = new JSONArray();
61306182
JSONObject entry = new JSONObject();
61316183
if (eachRequiredProp.equals(emailFieldName)) {
6132-
entry.put("value", "$EMAILVALUE$"); entry.put("tags", new JSONArray().put("handles"));
6184+
entry.put("value", "$EMAILVALUE$"); entry.put("tags", new JSONArray().put("handle"));
61336185
} else if (eachRequiredProp.equals(phoneFieldName)) {
6134-
entry.put("value", "{{phone}}"); entry.put("tags", new JSONArray().put("handles"));
6186+
entry.put("value", "{{phone}}"); entry.put("tags", new JSONArray().put("handle"));
61356187
} else {
6136-
entry.put("value", "$FUNCTIONALID$"); entry.put("tags", new JSONArray().put("handles"));
6188+
entry.put("value", "$FUNCTIONALID$"); entry.put("tags", new JSONArray().put("handle"));
61376189
}
61386190
arr.put(entry);
61396191
identityJson.put(eachRequiredProp, arr);
@@ -6166,7 +6218,7 @@ public static String generateHbsForPrereg(boolean isItUpdate) {
61666218
if ("array".equals(fieldType)) {
61676219
JSONArray emailArray = new JSONArray();
61686220
JSONObject emailEntry = new JSONObject();
6169-
emailEntry.put("value", "$EMAILVALUE$"); emailEntry.put("tags", new JSONArray().put("handles"));
6221+
emailEntry.put("value", "$EMAILVALUE$"); emailEntry.put("tags", new JSONArray().put("handle"));
61706222
emailArray.put(emailEntry); identityJson.put(eachRequiredProp, emailArray);
61716223
} else {
61726224
identityJson.put(eachRequiredProp, "$EMAILVALUE$");

apitest-commons/src/main/java/io/mosip/testrig/apirig/utils/GlobalConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ public class GlobalConstants {
259259
public static final String SESSION = "SESSION";
260260
public static final String COOKIE_NAME = "cookieName";
261261
public static final String ACCEPT_HEADER = "acceptHeader";
262+
263+
public static final String ADD_IDENTITY_V2_ENDPOINT = "/v1/identity/v2";
262264

263265
public static final String ACTIVE_PROFILES = "activeProfiles";
264266

0 commit comments

Comments
 (0)