From 2b3af3725f8f88634e4573bb672263fe5005650e Mon Sep 17 00:00:00 2001 From: Jayesh Kharode Date: Thu, 21 Nov 2024 11:10:02 +0530 Subject: [PATCH] MOSIP-35914 Signed-off-by: Jayesh Kharode --- .../controller/ContextController.java | 2 +- .../controller/PersonaController.java | 4 +- .../controller/TestDataController.java | 2 +- .../service/PacketSyncService.java | 210 ++++++++++-------- .../service/ResidentService.java | 2 +- .../dataprovider/PacketTemplateProvider.java | 2 +- .../dataprovider/models/ResidentModel.java | 49 ++-- .../variables/VariableManager.java | 161 ++++++-------- 8 files changed, 221 insertions(+), 211 deletions(-) diff --git a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/ContextController.java b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/ContextController.java index 1b231b94..07e8e285 100644 --- a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/ContextController.java +++ b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/ContextController.java @@ -48,7 +48,7 @@ public class ContextController { try { if (personaConfigPath != null && !personaConfigPath.equals("")) DataProviderConstants.RESOURCE = personaConfigPath; - VariableManager.Init(contextKey); +// VariableManager.Init(contextKey); return contextUtils.createUpdateServerContext(contextProperties, contextKey); } catch (Exception ex) { diff --git a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/PersonaController.java b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/PersonaController.java index b16719b7..e2554d72 100644 --- a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/PersonaController.java +++ b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/PersonaController.java @@ -56,7 +56,7 @@ public PersonaController(@Lazy PacketSyncService packetSyncService) { if (personaConfigPath != null && !personaConfigPath.equals("")) { DataProviderConstants.RESOURCE = personaConfigPath; } - VariableManager.Init(contextKey); +// VariableManager.Init(contextKey); return packetSyncService.updatePersonaData(personaRequestDto, contextKey); @@ -111,7 +111,7 @@ public PersonaController(@Lazy PacketSyncService packetSyncService) { // clear all tokens // VariableManager.setVariableValue("urlSwitched", "true"); - return packetSyncService.generateResidentData(residentRequestDto, contextKey).toString(); + return packetSyncService.generateResidentData(count, residentRequestDto, contextKey).toString(); } catch (Exception ex) { logger.error("generateResidentData", ex); diff --git a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/TestDataController.java b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/TestDataController.java index 176056f5..9dcdda65 100644 --- a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/TestDataController.java +++ b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/controller/TestDataController.java @@ -276,7 +276,7 @@ public TestDataController(@Lazy PacketSyncService packetSyncService, @Lazy Packe // clear all tokens // VariableManager.setVariableValue("urlSwitched", "true"); - return packetSyncService.generateResidentData(residentRequestDto, contextKey).toString(); + return packetSyncService.generateResidentData(count, residentRequestDto, contextKey).toString(); } catch (Exception ex) { logger.error("generateResidentData", ex); diff --git a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/PacketSyncService.java b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/PacketSyncService.java index 98c140a2..14ef672e 100644 --- a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/PacketSyncService.java +++ b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/PacketSyncService.java @@ -166,99 +166,117 @@ void loadServerContextProperties(String contextKey) { } } - public String generateResidentData( PersonaRequestDto residentRequestDto, String contextKey) { - logger.info(" Entered Persona generation at time: " + System.currentTimeMillis()); - // TO do --Check why we need to load the context here -// loadServerContextProperties(contextKey); - VariableManager.setVariableValue(contextKey, "process", "NEW"); - Properties props = residentRequestDto.getRequests().get(PersonaRequestType.PR_ResidentAttribute); - Gender enumGender = Gender.Any; - ResidentDataProvider provider = new ResidentDataProvider(); - if (props.containsKey("Gender")) { - enumGender = Gender.valueOf(props.get("Gender").toString()); // Gender.valueOf(residentRequestDto.getGender()); - } -// provider.addCondition(ResidentAttribute.RA_Count, count); + public String generateResidentData(int count, PersonaRequestDto residentRequestDto, String contextKey) { + String personaId = VariableManager.getVariableValue(contextKey, "personaId") != null + ? VariableManager.getVariableValue(contextKey, "personaId").toString() + : null; + JSONArray outIds = new JSONArray(); + if(personaId!=null && !personaId.isEmpty()) { + JSONObject id = new JSONObject(); + id.put("id",VariableManager.getVariableValue(contextKey, "id").toString()); + id.put("path", personaId); + outIds.put(id); + }else { + logger.info(" Entered Persona generation at time: " + System.currentTimeMillis()); + // TO do --Check why we need to load the context here +// loadServerContextProperties(contextKey); + VariableManager.setVariableValue(contextKey, "process", "NEW"); + Properties props = residentRequestDto.getRequests().get(PersonaRequestType.PR_ResidentAttribute); + Gender enumGender = Gender.Any; + ResidentDataProvider provider = new ResidentDataProvider(); + if (props.containsKey("Gender")) { + enumGender = Gender.valueOf(props.get("Gender").toString()); // Gender.valueOf(residentRequestDto.getGender()); + } + provider.addCondition(ResidentAttribute.RA_Count, count); - if (props.containsKey("Age")) { + if (props.containsKey("Age")) { - provider.addCondition(ResidentAttribute.RA_Age, ResidentAttribute.valueOf(props.get("Age").toString())); - } else - provider.addCondition(ResidentAttribute.RA_Age, ResidentAttribute.RA_Adult); + provider.addCondition(ResidentAttribute.RA_Age, ResidentAttribute.valueOf(props.get("Age").toString())); + } else + provider.addCondition(ResidentAttribute.RA_Age, ResidentAttribute.RA_Adult); - if (props.containsKey("SkipGaurdian")) { - provider.addCondition(ResidentAttribute.RA_SKipGaurdian, props.get("SkipGaurdian")); - } - provider.addCondition(ResidentAttribute.RA_Gender, enumGender); + if (props.containsKey("SkipGaurdian")) { + provider.addCondition(ResidentAttribute.RA_SKipGaurdian, props.get("SkipGaurdian")); + } + provider.addCondition(ResidentAttribute.RA_Gender, enumGender); - String primaryLanguage = "eng"; - if (props.containsKey("PrimaryLanguage")) { - primaryLanguage = props.get("PrimaryLanguage").toString(); - provider.addCondition(ResidentAttribute.RA_PRIMARAY_LANG, primaryLanguage); - } + String primaryLanguage = "eng"; + if (props.containsKey("PrimaryLanguage")) { + primaryLanguage = props.get("PrimaryLanguage").toString(); + provider.addCondition(ResidentAttribute.RA_PRIMARAY_LANG, primaryLanguage); + } - if (props.containsKey("SecondaryLanguage")) { - provider.addCondition(ResidentAttribute.RA_SECONDARY_LANG, props.get("SecondaryLanguage").toString()); - } - if (props.containsKey("Finger")) { - provider.addCondition(ResidentAttribute.RA_Finger, Boolean.parseBoolean(props.get("Finger").toString())); - } - if (props.containsKey("Iris")) { - provider.addCondition(ResidentAttribute.RA_Iris, Boolean.parseBoolean(props.get("Iris").toString())); - } - if (props.containsKey("Face")) { - provider.addCondition(ResidentAttribute.RA_Photo, Boolean.parseBoolean(props.get("Face").toString())); - } - if (props.containsKey("Document")) { - provider.addCondition(ResidentAttribute.RA_Document, - Boolean.parseBoolean(props.get("Document").toString())); - } - if (props.containsKey("Invalid")) { - List invalidList = Arrays.asList(props.get("invalid").toString().split(",", -1)); - provider.addCondition(ResidentAttribute.RA_InvalidList, invalidList); - } - if (props.containsKey("Miss")) { + if (props.containsKey("SecondaryLanguage")) { + provider.addCondition(ResidentAttribute.RA_SECONDARY_LANG, props.get("SecondaryLanguage").toString()); + } + if (props.containsKey("Finger")) { + provider.addCondition(ResidentAttribute.RA_Finger, Boolean.parseBoolean(props.get("Finger").toString())); + } + if (props.containsKey("Iris")) { + provider.addCondition(ResidentAttribute.RA_Iris, Boolean.parseBoolean(props.get("Iris").toString())); + } + if (props.containsKey("Face")) { + provider.addCondition(ResidentAttribute.RA_Photo, Boolean.parseBoolean(props.get("Face").toString())); + } + if (props.containsKey("Document")) { + provider.addCondition(ResidentAttribute.RA_Document, + Boolean.parseBoolean(props.get("Document").toString())); + } + if (props.containsKey("Invalid")) { + List invalidList = Arrays.asList(props.get("invalid").toString().split(",", -1)); + provider.addCondition(ResidentAttribute.RA_InvalidList, invalidList); + } + if (props.containsKey("Miss")) { - List missedList = Arrays.asList(props.get("Miss").toString().split(",", -1)); - provider.addCondition(ResidentAttribute.RA_MissList, missedList); - RestClient.logInfo(contextKey, "before Genrate: missthese:" + missedList.toString()); - } - if (props.containsKey("ThirdLanguage")) { + List missedList = Arrays.asList(props.get("Miss").toString().split(",", -1)); + provider.addCondition(ResidentAttribute.RA_MissList, missedList); + RestClient.logInfo(contextKey, "before Genrate: missthese:" + missedList.toString()); + } + if (props.containsKey("ThirdLanguage")) { - provider.addCondition(ResidentAttribute.RA_THIRD_LANG, props.get("ThirdLanguage").toString()); - } - if (props.containsKey("SchemaVersion")) { + provider.addCondition(ResidentAttribute.RA_THIRD_LANG, props.get("ThirdLanguage").toString()); + } + if (props.containsKey("SchemaVersion")) { - provider.addCondition(ResidentAttribute.RA_SCHEMA_VERSION, props.get("SchemaVersion").toString()); - } + provider.addCondition(ResidentAttribute.RA_SCHEMA_VERSION, props.get("SchemaVersion").toString()); + } - RestClient.logInfo(contextKey, "before Genrate"); - List lst = provider.generate(contextKey); - RestClient.logInfo(contextKey, "After Genrate"); + RestClient.logInfo(contextKey, "before Genrate"); + List lst = provider.generate(contextKey); + RestClient.logInfo(contextKey, "After Genrate"); - JSONArray outIds = new JSONArray(); + try { + String tmpDir; - try { - String tmpDir; + tmpDir = Files.createTempDirectory("residents_").toFile().getAbsolutePath(); + + VariableManager.setVariableValue(contextKey, "residents_", tmpDir); + + for (ResidentModel r : lst) { + Path tempPath = Path.of(tmpDir, r.getId() + ".json"); + r.setPath(tempPath.toString()); - tmpDir = Files.createTempDirectory("residents_").toFile().getAbsolutePath(); + String jsonStr = r.toJSONString(); + + + String personaAbsPath = tempPath.toFile().getAbsolutePath(); + VariableManager.setVariableValue(contextKey, "id", r.getId()); + VariableManager.setVariableValue(contextKey, "personaId", personaAbsPath); + VariableManager.setVariableValue(contextKey, personaAbsPath, jsonStr); - VariableManager.setVariableValue(contextKey, "residents_", tmpDir); + // Write to a file only when debug enabled +// To Do --------- CommonUtil.write(tempPath, jsonStr.getBytes()); - for (ResidentModel r : lst) { - Path tempPath = Path.of(tmpDir, r.getId() + ".json"); - r.setPath(tempPath.toString()); - String jsonStr = r.toJSONString(); - CommonUtil.write(tempPath, jsonStr.getBytes()); - JSONObject id = new JSONObject(); - id.put("id", r.getId()); - id.put("path", tempPath.toFile().getAbsolutePath()); - outIds.put(id); + JSONObject id = new JSONObject(); + id.put("id", r.getId()); + id.put("path", tempPath.toFile().getAbsolutePath()); + outIds.put(id); + } + } catch (IOException e) { + logger.error(e.getMessage()); + return "{\"" + e.getMessage() + "\"}"; } - } catch (IOException e) { - logger.error(e.getMessage()); - return "{\"" + e.getMessage() + "\"}"; } - JSONObject response = new JSONObject(); response.put(STATUS, SUCCESS); response.put(RESPONSE, outIds); @@ -381,7 +399,7 @@ public JSONObject makePacketAndSync(String preregId, String templateLocation, St public Path createIDJsonFromPersona(String personaFile, String contextKey) throws IOException { loadServerContextProperties(contextKey); - ResidentModel resident = ResidentModel.readPersona(personaFile); + ResidentModel resident = ResidentModel.readPersona(personaFile, contextKey); JSONObject jsonIdentity = CreatePersona.createIdentity(resident, null, contextKey); JSONObject jsonWrapper = new JSONObject(); jsonWrapper.put("identity", jsonIdentity); @@ -613,7 +631,7 @@ public String preRegisterResident(List personaFilePath, String contextKe loadServerContextProperties(contextKey); for (String path : personaFilePath) { - ResidentModel resident = ResidentModel.readPersona(path); + ResidentModel resident = ResidentModel.readPersona(path,contextKey); String response = PreRegistrationSteps.postApplication(resident, null, contextKey); // preregid saveRegIDMap(response, path); @@ -626,7 +644,7 @@ public String updateResidentApplication(String personaFilePath, String preregId, throws IOException { loadServerContextProperties(contextKey); - ResidentModel resident = ResidentModel.readPersona(personaFilePath); + ResidentModel resident = ResidentModel.readPersona(personaFilePath,contextKey); return PreRegistrationSteps.putApplication(resident, preregId, contextKey); } @@ -668,7 +686,7 @@ public String requestOtp(List personaFilePath, String to, String context loadServerContextProperties(contextKey); for (String path : personaFilePath) { - ResidentModel resident = ResidentModel.readPersona(path); + ResidentModel resident = ResidentModel.readPersona(path, contextKey); ResidentPreRegistration preReg = new ResidentPreRegistration(resident); builder.append(preReg.sendOtpTo(to, contextKey)); @@ -679,7 +697,7 @@ public String requestOtp(List personaFilePath, String to, String context public String verifyOtp(String personaFilePath, String to, String otp, String contextKey) throws IOException { loadServerContextProperties(contextKey); - ResidentModel resident = ResidentModel.readPersona(personaFilePath); + ResidentModel resident = ResidentModel.readPersona(personaFilePath, contextKey); ResidentPreRegistration preReg = new ResidentPreRegistration(resident); if(otp != null && otp.isEmpty()) { @@ -815,7 +833,7 @@ public String uploadDocuments(String personaFilePath, String preregId, String co String response = ""; loadServerContextProperties(contextKey); - ResidentModel resident = ResidentModel.readPersona(personaFilePath); + ResidentModel resident = ResidentModel.readPersona(personaFilePath, contextKey); for (MosipDocument a : resident.getDocuments()) { JSONObject respObject = PreRegistrationSteps.UploadDocument(a.getDocCategoryCode(), @@ -869,7 +887,7 @@ public String createPacketTemplates(List personaFilePaths, String proces new JSONObject(), contextKey); } for (String path : personaFilePaths) { - ResidentModel resident = ResidentModel.readPersona(path); + ResidentModel resident = ResidentModel.readPersona(path,contextKey); String packetPath = packetDir.toString() + File.separator + resident.getId(); RestClient.logInfo(contextKey, "packetPath=" + packetPath); machineId = VariableManager.getVariableValue(contextKey, MOSIP_TEST_REGCLIENT_MACHINEID).toString(); @@ -1050,7 +1068,7 @@ public String getPersonaData(List getPersonaRequest, String co for (UpdatePersonaDto req : getPersonaRequest) { - ResidentModel persona = ResidentModel.readPersona(req.getPersonaFilePath()); + ResidentModel persona = ResidentModel.readPersona(req.getPersonaFilePath(), contextKey); List retrieveAttrs = req.getRetriveAttributeList(); if (retrieveAttrs != null) { for (String attr : retrieveAttrs) { @@ -1207,12 +1225,12 @@ public String updatePersonaData(List updatePersonaRequest, Str String ret = "{Sucess}"; for (UpdatePersonaDto req : updatePersonaRequest) { try { - ResidentModel persona = ResidentModel.readPersona(req.getPersonaFilePath()); + ResidentModel persona = ResidentModel.readPersona(req.getPersonaFilePath(), contextKey); List regenAttrs = req.getRegenAttributeList(); if (regenAttrs != null) { for (String attr : regenAttrs) { if (req.getTestPersonaPath() != null) { - ResidentModel testPersona = ResidentModel.readPersona(req.getTestPersonaPath()); + ResidentModel testPersona = ResidentModel.readPersona(req.getTestPersonaPath(), contextKey); ResidentDataProvider.updateBiometricWithTestPersona(persona, testPersona, attr, contextKey); } else { ResidentDataProvider.updateBiometric(persona, attr, contextKey); @@ -1228,7 +1246,7 @@ public String updatePersonaData(List updatePersonaRequest, Str persona.setMissAttributes(missList); // persona.save(); - persona.writePersona(req.getPersonaFilePath()); + persona.writePersona(req.getPersonaFilePath(), contextKey); } catch (IOException e) { logger.error("updatePersonaData:" + e.getMessage()); @@ -1252,24 +1270,24 @@ public String updateResidentData(Hashtable hasht String keyS = key.toString().toLowerCase(); if (keyS.startsWith("uin")) { filePathResident = list.get(key).toString(); - persona = ResidentModel.readPersona(filePathResident); + persona = ResidentModel.readPersona(filePathResident, contextKey); persona.setUIN(uin); } else if (keyS.toString().startsWith("rid")) { filePathResident = list.get(key).toString(); - persona = ResidentModel.readPersona(filePathResident); + persona = ResidentModel.readPersona(filePathResident, contextKey); persona.setRID(rid); } else if (keyS.toString().startsWith("child")) { filePathResident = list.get(key).toString(); - persona = ResidentModel.readPersona(filePathResident); + persona = ResidentModel.readPersona(filePathResident, contextKey); } else if (keyS.startsWith("guardian")) { filePathParent = list.get(key).toString(); - guardian = ResidentModel.readPersona(filePathParent); + guardian = ResidentModel.readPersona(filePathParent, contextKey); } } if (guardian != null && persona != null) persona.setGuardian(guardian); if (persona != null) { - CommonUtil.write(Paths.get(filePathResident), persona.toJSONString().getBytes()); + CommonUtil.writeToCache(Paths.get(filePathResident), persona.toJSONString().getBytes(), contextKey); return "{\"response\":\"SUCCESS\"}"; } else { return "{\"response\":\"FAIL\"}"; @@ -1283,11 +1301,11 @@ public String updatePersonaBioExceptions(BioExceptionDto personaBERequestDto, St loadServerContextProperties(contextKey); String ret = "{Sucess}"; try { - ResidentModel persona = ResidentModel.readPersona(personaBERequestDto.getPersonaFilePath()); + ResidentModel persona = ResidentModel.readPersona(personaBERequestDto.getPersonaFilePath(),contextKey); persona.setBioExceptions(personaBERequestDto.getExceptions()); - persona.writePersona(personaBERequestDto.getPersonaFilePath()); + persona.writePersona(personaBERequestDto.getPersonaFilePath(), contextKey); } catch (Exception e) { logger.error("updatePersonaBioExceptions:" + e.getMessage()); } @@ -1420,7 +1438,7 @@ public String setPersonaMockABISExpectation(List expect loadServerContextProperties(contextKey); for (MockABISExpectationsDto expct : expectations) { - ResidentModel persona = ResidentModel.readPersona(expct.getPersonaPath()); + ResidentModel persona = ResidentModel.readPersona(expct.getPersonaPath(), contextKey); List modalities = expct.getModalities(); List capFingers = persona.getBiometric().getCapture() diff --git a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/ResidentService.java b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/ResidentService.java index 92f875f3..ffcbdbc8 100644 --- a/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/ResidentService.java +++ b/mosip-packet-creator/src/main/java/io/mosip/testrig/dslrig/packetcreator/service/ResidentService.java @@ -46,7 +46,7 @@ void loadServerContextProperties(String contextKey) { public String downloadCard(String personaPath, String uin, String context) throws Exception{ loadServerContextProperties(context); - ResidentModel resident = ResidentModel.readPersona(personaPath); + ResidentModel resident = ResidentModel.readPersona(personaPath,context); RegistrationSteps steps = new RegistrationSteps(); String resp = steps.downloadCard(resident, uin,context); diff --git a/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/PacketTemplateProvider.java b/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/PacketTemplateProvider.java index 04c8b075..63956529 100644 --- a/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/PacketTemplateProvider.java +++ b/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/PacketTemplateProvider.java @@ -655,7 +655,7 @@ Boolean generateCBEFF(ResidentModel resident, List bioAttrib, String out bos.close(); } } - resident.save(); + resident.save(contextKey); return true; } diff --git a/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/models/ResidentModel.java b/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/models/ResidentModel.java index e456a237..7bb4d798 100644 --- a/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/models/ResidentModel.java +++ b/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/models/ResidentModel.java @@ -19,6 +19,8 @@ import io.mosip.testrig.dslrig.dataprovider.util.CommonUtil; import io.mosip.testrig.dslrig.dataprovider.util.Gender; +import io.mosip.testrig.dslrig.dataprovider.util.RestClient; +import io.mosip.testrig.dslrig.dataprovider.variables.VariableManager; import lombok.Data; import java.io.*; import java.io.FileInputStream; @@ -118,8 +120,11 @@ public String toJSONString() { return jsonStr; } - public void save() throws IOException { - Files.write(Paths.get(path), this.toJSONString().getBytes()); + public void save(String contextKey) throws IOException { + if(RestClient.isDebugEnabled(contextKey)) { + Files.write(Paths.get(path), this.toJSONString().getBytes()); + } + VariableManager.setVariableValue(contextKey, path, this.toJSONString()); } @@ -132,29 +137,31 @@ public void save() throws IOException { // } // } - public static ResidentModel readPersona(String filePath) throws IOException { - - ObjectMapper mapper = new ObjectMapper(); - //mapper.registerModule(new SimpleModule().addDeserializer(Pair.class,new PairDeserializer())); - // mapper.registerModule(new SimpleModule().addSerializer(Pair.class, new PairSerializer())); - byte[] bytes = CommonUtil.read(filePath); - ResidentModel model = mapper.readValue(bytes, ResidentModel.class); - model.setPath(filePath); + public static ResidentModel readPersona(String filePath, String contextKey) throws IOException { + + ResidentModel model = null; + ObjectMapper mapper = new ObjectMapper(); + try { + byte[] bytes = VariableManager.getVariableValue(contextKey, filePath).toString().getBytes(); +// byte[] bytes = CommonUtil.read(filePath); + model = mapper.readValue(bytes, ResidentModel.class); + model.setPath(filePath); + } catch (Exception e) { + logger.error(e.getMessage()); + } + return model; - } - + } -// public void writePersona(String filePath) throws IOException { -// ObjectMapper mapper = new ObjectMapper(); -// try (OutputStream outputStream = new FileOutputStream(filePath)) { -// mapper.writeValue(outputStream, this.toJSONString().getBytes()); -// -// } -// } - public void writePersona(String filePath) throws IOException { - Files.write(Paths.get(filePath), this.toJSONString().getBytes()); + public void writePersona(String filePath, String contextKey) throws IOException { + + if(RestClient.isDebugEnabled(contextKey)) { + Files.write(Paths.get(filePath), this.toJSONString().getBytes()); + } + VariableManager.setVariableValue(contextKey, filePath, this.toJSONString()); } + public static void main(String [] args) { ResidentModel model = new ResidentModel(); diff --git a/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/variables/VariableManager.java b/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/variables/VariableManager.java index 191794ea..fa0d7364 100644 --- a/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/variables/VariableManager.java +++ b/mosipTestDataProvider/src/io/mosip/testrig/dslrig/dataprovider/variables/VariableManager.java @@ -35,58 +35,50 @@ public final class VariableManager { static boolean bInit = false; static { - Init(NS_DEFAULT); + Init(); } public static boolean isInit() { return bInit; } - public static void Init(String contextKey) { - - if (bInit) - return; - // resolve a cache manager - CachingProvider cachingProvider = Caching.getCachingProvider(); - cacheManager = cachingProvider.getCacheManager(); - - // configure the cache - cacheConfig = new MutableConfiguration().setTypes(String.class, Object.class) - .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ONE_DAY)).setStatisticsEnabled(true); - - // create the cache - if (varNameSpaces == null) { - varNameSpaces = new Hashtable>(); - // load predefined variables - - Cache cache = cacheManager.createCache(contextKey, cacheConfig); - varNameSpaces.put(contextKey, cache); - - } - CONFIG_PATH = DataProviderConstants.RESOURCE + "config/"; - Boolean bret = loadNamespaceFromPropertyFile(CONFIG_PATH + "default.properties", NS_DEFAULT); - bInit = bret; - - } - - static Cache createNameSpace(String contextKey) { - Cache ht = null; - try { - ht = varNameSpaces.get(contextKey); - } catch (Exception e) { - } - if (ht == null) { - ht = cacheManager.createCache(contextKey, cacheConfig); - varNameSpaces.put(contextKey, ht); - } - return ht; - } - - public static Object setVariableValue(String contextKey, String varName, Object value) { - Cache ht = createNameSpace(contextKey); - ht.put(varName, value); - return value; - } + public static synchronized void Init() { + synchronized (VariableManager.class) { + if (bInit) return; + // resolve a cache manager + CachingProvider cachingProvider = Caching.getCachingProvider(); + cacheManager = cachingProvider.getCacheManager(); + // configure the cache + cacheConfig = new MutableConfiguration() + .setTypes(String.class, Object.class) + .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.ONE_DAY)) + .setStatisticsEnabled(true); + // create the cache + if (varNameSpaces == null) { + varNameSpaces = new Hashtable>(); + Cache cache = cacheManager.createCache(NS_DEFAULT, cacheConfig); + varNameSpaces.put(NS_DEFAULT, cache); + } + CONFIG_PATH = DataProviderConstants.RESOURCE + "config/"; + Boolean bret = loadNamespaceFromPropertyFile(CONFIG_PATH + "default.properties", NS_DEFAULT); + bInit = bret; + } + } + + static Cache createNameSpace(String contextKey) { + Cache ht = varNameSpaces.get(contextKey); + if (ht == null) { + ht = cacheManager.createCache(contextKey, cacheConfig); + varNameSpaces.put(contextKey, ht); + } + return ht; + } + + public static Object setVariableValue(String contextKey, String varName, Object value) { + Cache ht = createNameSpace(contextKey); + ht.put(varName, value); + return value; + } public static String[] findVariables(String text) { @@ -109,32 +101,25 @@ public static String[] findVariables(String text) { return set.toArray(a); } - public static Object getVariableValue(String contextKey, String varName) { - - if (!bInit) - Init(contextKey); - - Cache ht = null; - Object ret = null; // To do ---- new Object() - - try { - ht = varNameSpaces.get(contextKey); - if (ht != null) { - ret = ht.get(varName); - if (ret == null && contextKey.equalsIgnoreCase(NS_DEFAULT)) { - // Cache expired , reloading the default namespace - loadNamespaceFromPropertyFile(VariableManager.CONFIG_PATH + "default.properties", - VariableManager.NS_DEFAULT); - ht = varNameSpaces.get(contextKey); - ret = ht.get(varName); - } - return ret; - } - } catch (Exception e) { - logger.error(e.getMessage()); - } - return ret; - } + public static Object getVariableValue(String contextKey, String varName) { + if (!bInit) Init(); + Cache ht = varNameSpaces.get(contextKey); + Object ret = null; + try { + if (ht != null) { + ret = ht.get(varName); + if (ret == null && contextKey.equalsIgnoreCase(NS_DEFAULT)) { + // Cache expired, reloading the default namespace + loadNamespaceFromPropertyFile(CONFIG_PATH + "default.properties", NS_DEFAULT); + ht = varNameSpaces.get(contextKey); + ret = ht.get(varName); + } + } + } catch (Exception e) { + logger.error(e.getMessage()); + } + return ret; + } public static Boolean loadNamespaceFromPropertyFile(String propFile, String contextKey) { Boolean bRet = false; @@ -155,21 +140,21 @@ public static Boolean loadNamespaceFromPropertyFile(String propFile, String cont return bRet; } - public static String deleteNameSpace(String contextKey) { - try { - printAllContents(); - Cache cache = varNameSpaces.remove(contextKey); - if (cache != null) { - synchronized (cacheManager) { - cacheManager.destroyCache(contextKey); - } - } - } catch (Exception e) { - logger.error(e.getMessage()); - return "false"; - } - return "true"; - } + public static String deleteNameSpace(String contextKey) { + try { + printAllContents(); + Cache cache = varNameSpaces.remove(contextKey); + if (cache != null) { + synchronized (cacheManager) { + cacheManager.destroyCache(contextKey); + } + } + } catch (Exception e) { + logger.error(e.getMessage()); + return "false"; + } + return "true"; + } public static void printAllContents() { StringBuffer s = new StringBuffer(); @@ -224,4 +209,4 @@ public static void testJCache() { } -} +} \ No newline at end of file