Skip to content

Commit

Permalink
MOSIP-32453 : Sonar bug fixes and reliability issues (mosip#1018)
Browse files Browse the repository at this point in the history
* sonar bugs and reliability issues

Signed-off-by: GOKULRAJ136 <[email protected]>

* fixed api testrig failiures in GitHub actions

Signed-off-by: GOKULRAJ136 <[email protected]>

* naming convention changes

Signed-off-by: GOKULRAJ136 <[email protected]>

* naming conventions for triggers

Signed-off-by: GOKULRAJ136 <[email protected]>

---------

Signed-off-by: GOKULRAJ136 <[email protected]>
  • Loading branch information
GOKULRAJ136 authored Jul 15, 2024
1 parent 5218b83 commit 139d9b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.mosip.admin.packetstatusupdater.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import io.mosip.admin.constant.ApplicantDetailErrorCode;
import io.mosip.admin.packetstatusupdater.constant.ApiName;
import io.mosip.admin.packetstatusupdater.exception.MasterDataServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
Expand All @@ -16,7 +13,9 @@
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import io.mosip.admin.packetstatusupdater.constant.ApiName;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;



Expand Down Expand Up @@ -202,9 +201,10 @@ public <T> T getApi(String url,

T result = null;
try {
ResponseEntity responseEntity= (ResponseEntity) restTemplate
ResponseEntity responseEntity= restTemplate
.exchange(url, HttpMethod.GET, setRequestHeader(null, null), responseType);
if(url.contains("datashare") && responseEntity.getHeaders().getContentType().equals(MediaType.APPLICATION_JSON)){
if(url != null && url.contains("datashare") && responseEntity != null && responseEntity.getHeaders() != null &&
responseEntity.getHeaders().getContentType().equals(MediaType.APPLICATION_JSON)){
throw new MasterDataServiceException(ApplicantDetailErrorCode.DATA_SHARE_EXPIRED_EXCEPTION.getErrorCode(),
ApplicantDetailErrorCode.DATA_SHARE_EXPIRED_EXCEPTION.getErrorMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.mosip.admin.dto.ApplicantUserDetailsDto;
import io.mosip.admin.dto.DigitalCardStatusResponseDto;
import io.mosip.admin.packetstatusupdater.constant.ApiName;

import io.mosip.admin.packetstatusupdater.exception.DataNotFoundException;
import io.mosip.admin.packetstatusupdater.exception.MasterDataServiceException;
import io.mosip.admin.packetstatusupdater.exception.RequestException;
Expand All @@ -26,6 +25,8 @@
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -42,6 +43,8 @@ public class ApplicantDetailServiceImpl implements ApplicantDetailService {

private static final String FACE = "Face";

private static final Logger log = LoggerFactory.getLogger(ApplicantDetailServiceImpl.class);

@Autowired
RestClient restClient;

Expand Down Expand Up @@ -69,7 +72,7 @@ public class ApplicantDetailServiceImpl implements ApplicantDetailService {

private static final String DOCUMENTS="documents";

private static final String ApplicantPhoto = "applicantPhoto";
private static final String APPLICANTPHOTO = "applicantPhoto";

private static final String VALUE = "value";
private static final String DOB = "dob";
Expand Down Expand Up @@ -110,11 +113,17 @@ public ApplicantDetailsDto getApplicantDetails(String rid) throws Exception {
JSONObject mapperIdentity=utility.getJSONObject(idenitityJsonObject,IDENTITY);
List<String> mapperJsonKeys = new ArrayList<>(mapperIdentity.keySet());
for(String valueObj: applicantDetails){
if(valueObj!=null && !valueObj.equalsIgnoreCase(ApplicantPhoto)){
if (valueObj == null) {
log.warn("Encountered null value in applicantDetails list");
continue;
}
if(!valueObj.equalsIgnoreCase(APPLICANTPHOTO)){
LinkedHashMap<String, String> jsonObject = utility.getJSONValue(mapperIdentity, valueObj);
String value = jsonObject.get(VALUE);
applicantDataMap.put(value,identityObj.get(value).toString());
} else if (valueObj.equalsIgnoreCase(ApplicantPhoto)) {
if (jsonObject != null && VALUE != null && jsonObject.containsKey(VALUE)) {
String value = jsonObject.get(VALUE);
applicantDataMap.put(value, identityObj.get(value).toString());
}
} else {
getImageData(documents,applicantDataMap);
}
}
Expand Down Expand Up @@ -187,7 +196,7 @@ private void getImageData(JSONArray documents, Map<String, String> applicantData
byte[] data = FaceDecoder.convertFaceISOToImageBytes(convertRequestDto);
String encodedBytes = StringUtils.newStringUtf8(Base64.encodeBase64(data, false));
String imageData = "data:image/png;base64," + encodedBytes;
applicantDataMap.put(ApplicantPhoto, imageData);
applicantDataMap.put(APPLICANTPHOTO, imageData);
} else {
throw new DataNotFoundException(ApplicantDetailErrorCode.DATA_NOT_FOUND.getErrorCode(), ApplicantDetailErrorCode.DATA_NOT_FOUND.getErrorMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private boolean hasURLDetails(Class clazz, boolean isV2API, boolean deltaSync) {

public Map<Class, CompletableFuture> getInitiateDataFetch(String machineId, String regCenterId,
LocalDateTime lastUpdated, LocalDateTime currentTimestamp, boolean isV2API, boolean deltaSync, String fullSyncEntities) {
List<String> entities = (fullSyncEntities != null && !fullSyncEntities.isBlank()) ? Arrays.asList(fullSyncEntities.split("\\s*,\\s*")) : new ArrayList<>();
List<String> entities = (fullSyncEntities != null && !fullSyncEntities.isBlank()) ? Arrays.asList(fullSyncEntities.split("\\s*,\\s*",-1)) : new ArrayList<>();

Map<Class, CompletableFuture> futuresMap = new HashMap<>();
futuresMap.put(AppAuthenticationMethod.class,
Expand Down Expand Up @@ -201,10 +201,10 @@ public List<SyncDataBaseDto> retrieveData(Map<Class, CompletableFuture> futures,
}

private void handleDynamicData(List entities, List<SyncDataBaseDto> list, RegistrationCenterMachineDto registrationCenterMachineDto, boolean isV2) {
Map<String, List<DynamicFieldDto>> data = new HashMap<String, List<DynamicFieldDto>>();
Map<String, List<DynamicFieldDto>> data = new HashMap<>();
entities.forEach(dto -> {
if (!data.containsKey(((DynamicFieldDto) dto).getName())) {
List<DynamicFieldDto> langBasedData = new ArrayList<DynamicFieldDto>();
List<DynamicFieldDto> langBasedData = new ArrayList<>();
langBasedData.add(((DynamicFieldDto) dto));
data.put(((DynamicFieldDto) dto).getName(), langBasedData);
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,23 @@ public LocalDateTime getFullSyncCurrentTimestamp() {

public LocalDateTime getDeltaSyncCurrentTimestamp() {
CronExpression cronExpression = CronExpression.parse(deltaCacheEvictCron);
LocalDateTime nextTrigger1 = cronExpression.next(LocalDateTime.now().atZone(ZoneOffset.UTC).toLocalDateTime());
LocalDateTime nextTrigger2 = cronExpression.next(nextTrigger1);

long minutes = ChronoUnit.MINUTES.between(nextTrigger1.toInstant(ZoneOffset.UTC).atZone(ZoneOffset.UTC).toLocalDateTime(),
nextTrigger2.toInstant(ZoneOffset.UTC).atZone(ZoneOffset.UTC).toLocalDateTime());
LocalDateTime immediateNextTrigger = cronExpression.next(LocalDateTime.now().atZone(ZoneOffset.UTC).toLocalDateTime());
if (immediateNextTrigger == null) {
logger.error("Cron expression might be invalid or has no upcoming triggers.");
return null;
}

LocalDateTime nextTrigger = cronExpression.next(immediateNextTrigger);
if (nextTrigger == null) {
logger.error("No upcoming triggers after {}", immediateNextTrigger);
return null;
}

long minutes = ChronoUnit.MINUTES.between(immediateNextTrigger.toInstant(ZoneOffset.UTC).atZone(ZoneOffset.UTC).toLocalDateTime(),
nextTrigger.toInstant(ZoneOffset.UTC).atZone(ZoneOffset.UTC).toLocalDateTime());

LocalDateTime previousTrigger = nextTrigger1.toInstant(ZoneOffset.UTC).atZone(ZoneOffset.UTC).toLocalDateTime().minus(minutes, ChronoUnit.MINUTES);
LocalDateTime previousTrigger = immediateNextTrigger.toInstant(ZoneOffset.UTC).atZone(ZoneOffset.UTC).toLocalDateTime().minus(minutes, ChronoUnit.MINUTES);

logger.debug("Identified previous trigger : {}", previousTrigger);
return previousTrigger;
Expand Down Expand Up @@ -177,10 +187,10 @@ private void retrieveAndCreateSnapshot(Map<Class, CompletableFuture> futures) {
}

private void handleDynamicFields(List entities) {
Map<String, List<DynamicFieldDto>> data = new HashMap<String, List<DynamicFieldDto>>();
Map<String, List<DynamicFieldDto>> data = new HashMap<>();
entities.forEach(dto -> {
if(!data.containsKey(((DynamicFieldDto)dto).getName())) {
List<DynamicFieldDto> langBasedData = new ArrayList<DynamicFieldDto>();
List<DynamicFieldDto> langBasedData = new ArrayList<>();
langBasedData.add(((DynamicFieldDto)dto));
data.put(((DynamicFieldDto)dto).getName(), langBasedData);
}
Expand Down

0 comments on commit 139d9b7

Please sign in to comment.