From 300d30033483603203d6e0b50e6c5ce77c9f2055 Mon Sep 17 00:00:00 2001 From: kameshsr <47484458+kameshsr@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:52:12 +0530 Subject: [PATCH] MOSIP-29472 Create API dynamicfields/all. (#897) * MOSIP-29472 Create API dynamicfields/all. * MOSIP-29472 Create API dynamicfields/all. * MOSIP-29472 Create API dynamicfields/all. * MOSIP-29472 Covered Junit for Dynamic field all API. * MOSIP-29472 Covered Junit for Dynamic field all API. * MOSIP-29472 Changed logic as per review comment. * MOSIP-29472 Changed logic as per review comment. * MOSIP-29472 Changed logic as per review comment. --- .../controller/DynamicFieldController.java | 12 ++- .../getresponse/extn/DynamicFieldExtnDto.java | 1 - .../repository/DynamicFieldRepository.java | 1 - .../service/DynamicFieldService.java | 2 +- .../service/impl/DynamicFieldServiceImpl.java | 77 ++++++++++++------- .../DynamicFieldControllerTest.java | 45 ++++++----- .../test/service/SchemaServiceTest.java | 67 ++++++++-------- 7 files changed, 123 insertions(+), 82 deletions(-) diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/DynamicFieldController.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/DynamicFieldController.java index 4ac8c37ad50..7fcad875988 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/DynamicFieldController.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/DynamicFieldController.java @@ -5,7 +5,6 @@ import java.util.List; import javax.validation.Valid; -import javax.websocket.server.PathParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -39,7 +38,6 @@ import io.mosip.kernel.masterdata.dto.response.FilterResponseCodeDto; import io.mosip.kernel.masterdata.dto.response.FilterResponseDto; import io.mosip.kernel.masterdata.dto.response.PageResponseDto; -import io.mosip.kernel.masterdata.entity.DynamicField; import io.mosip.kernel.masterdata.service.DynamicFieldService; import io.mosip.kernel.masterdata.service.GenericService; import io.mosip.kernel.masterdata.utils.AuditUtil; @@ -93,6 +91,16 @@ public ResponseWrapper getDynamicFieldByName responseWrapper.setResponse(dynamicFieldService.getDynamicFieldByNameAndLangcode(fieldName,langCode,withValue)); return responseWrapper; } + + @ResponseFilter + @GetMapping("/all/{fieldName}") + @ApiOperation(value = " Service to fetch one dynamic field in all the languages") + public ResponseWrapper> getAllDynamicFieldByName( + @PathVariable("fieldName") String fieldName){ + ResponseWrapper> responseWrapper = new ResponseWrapper<>(); + responseWrapper.setResponse(dynamicFieldService.getAllDynamicFieldByName(fieldName)); + return responseWrapper; + } @ResponseFilter diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/dto/getresponse/extn/DynamicFieldExtnDto.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/dto/getresponse/extn/DynamicFieldExtnDto.java index 21893ee23fb..30767953f85 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/dto/getresponse/extn/DynamicFieldExtnDto.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/dto/getresponse/extn/DynamicFieldExtnDto.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.databind.JsonNode; import lombok.Data; -import org.json.JSONArray; import java.time.LocalDateTime; import java.util.List; diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/DynamicFieldRepository.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/DynamicFieldRepository.java index 9b11a2737e3..fbcb0975edf 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/DynamicFieldRepository.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/DynamicFieldRepository.java @@ -142,7 +142,6 @@ int updateDynamicField(String id, String description, String langCode, String da /** * update isDeleted as true - * @param id * @param updatedDateTime * @param updatedBy * @return diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/DynamicFieldService.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/DynamicFieldService.java index d40677997b3..22a97a2ca0e 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/DynamicFieldService.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/DynamicFieldService.java @@ -16,7 +16,6 @@ import io.mosip.kernel.masterdata.dto.request.SearchDto; import io.mosip.kernel.masterdata.dto.response.FilterResponseCodeDto; import io.mosip.kernel.masterdata.dto.response.PageResponseDto; -import io.mosip.kernel.masterdata.entity.DynamicField; /** * Methods to create / update / inactivate / addValues dynamic field @@ -83,4 +82,5 @@ public PageDto getAllDynamicField(int pageNumber, int pageS public DynamicFieldConsolidateResponseDto getDynamicFieldByNameAndLangcode(String fieldName,String langCode,boolean withValue); + List getAllDynamicFieldByName(String fieldName); } diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/DynamicFieldServiceImpl.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/DynamicFieldServiceImpl.java index f66c4d2c9d7..02f84529128 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/DynamicFieldServiceImpl.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/DynamicFieldServiceImpl.java @@ -13,7 +13,6 @@ import javax.transaction.Transactional; -import org.json.JSONArray; import org.json.JSONException; import io.mosip.kernel.masterdata.dto.response.FilterResult; @@ -31,10 +30,7 @@ import org.springframework.stereotype.Service; import org.springframework.util.Assert; -import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -75,17 +71,17 @@ @Service public class DynamicFieldServiceImpl implements DynamicFieldService { - + private static final Logger LOGGER = LoggerFactory.getLogger(DynamicFieldServiceImpl.class); - + private final ObjectMapper objectMapper = new ObjectMapper(); - + @Autowired private DynamicFieldRepository dynamicFieldRepository; - + @Autowired private MasterdataCreationUtil masterdataCreationUtil; - + @Autowired private MasterdataSearchHelper masterdataSearchHelper; @@ -103,11 +99,11 @@ public class DynamicFieldServiceImpl implements DynamicFieldService { @Autowired AuditUtil auditUtil; - + /* * (non-Javadoc) - * + * * @see * io.mosip.kernel.masterdata.service.DynamicFieldService#getAllDynamicField() */ @@ -116,18 +112,18 @@ public class DynamicFieldServiceImpl implements DynamicFieldService { condition="#langCode != null") @Override public PageDto getAllDynamicField(int pageNumber, int pageSize, String sortBy, String orderBy, String langCode, - LocalDateTime lastUpdated, LocalDateTime currentTimestamp) { + LocalDateTime lastUpdated, LocalDateTime currentTimestamp) { Page pagedResult = null; if (lastUpdated == null) { lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); } try { - - PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(Direction.fromString(orderBy), sortBy)); + + PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(Direction.fromString(orderBy), sortBy)); pagedResult = langCode == null ? dynamicFieldRepository.findAllLatestDynamicFieldNames(lastUpdated, currentTimestamp, pageRequest) : - dynamicFieldRepository.findAllLatestDynamicFieldNamesByLangCode(langCode,lastUpdated, currentTimestamp, pageRequest); - + dynamicFieldRepository.findAllLatestDynamicFieldNamesByLangCode(langCode,lastUpdated, currentTimestamp, pageRequest); + } catch (DataAccessException | DataAccessLayerException e) { throw new MasterDataServiceException(SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorCode(), SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorMessage() + " " @@ -153,11 +149,11 @@ public PageDto getAllDynamicField(int pageNumber, int pageS list.add(getDynamicFieldDto(groupedValues.get(lang))); } }); - + pagedFields.setPageNo(pagedResult.getNumber()); pagedFields.setTotalPages(pagedResult.getTotalPages()); pagedFields.setTotalItems(pagedResult.getTotalElements()); - } + } return pagedFields; } @@ -208,7 +204,7 @@ public List getDistinctDynamicFields(String langCode) { /* * (non-Javadoc) - * + * * @see * io.mosip.kernel.masterdata.service.DynamicFieldService#createDynamicField() */ @@ -245,7 +241,7 @@ public DynamicFieldResponseDto createDynamicField(DynamicFieldDto dto) { /* * (non-Javadoc) - * + * * @see * io.mosip.kernel.masterdata.service.DynamicFieldService#updateDynamicField() */ @@ -258,10 +254,10 @@ public DynamicFieldResponseDto updateDynamicField(String id, DynamicFieldPutDto String valueJson = getValidatedFieldValue(dto.getFieldVal()); - int updatedRows = dynamicFieldRepository.updateDynamicField(id, dto.getDescription(), dto.getLangCode(), + int updatedRows = dynamicFieldRepository.updateDynamicField(id, dto.getDescription(), dto.getLangCode(), dto.getDataType(), MetaDataUtils.getCurrentDateTime(), MetaDataUtils.getContextUser(), valueJson); - + if (updatedRows < 1) { throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorCode(), SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorMessage()); @@ -285,14 +281,14 @@ public StatusResponseDto deleteDynamicFieldValue(String id) { if(dynamicField == null) throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorCode(), SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorMessage()); - if(dynamicField.getValueJson()==null) - throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_VALUE_NOT_FOUND_EXCEPTION.getErrorCode(), + if(dynamicField.getValueJson()==null) + throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_VALUE_NOT_FOUND_EXCEPTION.getErrorCode(), SchemaErrorCode.DYNAMIC_FIELD_VALUE_NOT_FOUND_EXCEPTION.getErrorMessage()); JsonNode valueJson =objectMapper.readTree(dynamicField.getValueJson()); String code = valueJson.get("code").toString(); - - - + + + int deletedRows = dynamicFieldRepository.deleteDynamicField(dynamicField.getName(), "%"+code+"%", MetaDataUtils.getCurrentDateTime(), MetaDataUtils.getContextUser()); @@ -539,4 +535,29 @@ public DynamicFieldConsolidateResponseDto getDynamicFieldByNameAndLangcode(Strin } -} + @Override + @Cacheable(value = "dynamic-field", key = "'dynamicfield'.concat('-').concat(#fieldName)") + public List getAllDynamicFieldByName(String fieldName) { + List fields = null; + try { + fields = dynamicFieldRepository.findAllDynamicFieldByName(fieldName); + } catch (DataAccessException | DataAccessLayerException e) { + throw new MasterDataServiceException(SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorCode(), + SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorMessage() + " " + + ExceptionUtils.parseException(e)); + } + List list = new ArrayList<>(); + if(fields != null && !fields.isEmpty()) { + Map> groupedValues = fields + .stream() + .collect(Collectors.groupingBy(DynamicField::getLangCode)); + list = groupedValues.keySet() + .stream() + .map(lang -> getDynamicFieldDto(groupedValues.get(lang))) + .collect(Collectors.toList()); + + } + return list; + } + +} \ No newline at end of file diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/DynamicFieldControllerTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/DynamicFieldControllerTest.java index 46eea46fe16..5c094b25689 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/DynamicFieldControllerTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/DynamicFieldControllerTest.java @@ -1,13 +1,22 @@ package io.mosip.kernel.masterdata.test.controller; -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import io.mosip.kernel.masterdata.dto.request.*; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import io.mosip.kernel.core.http.RequestWrapper; +import io.mosip.kernel.core.websub.model.EventModel; +import io.mosip.kernel.core.websub.spi.PublisherClient; +import io.mosip.kernel.masterdata.dto.DynamicFieldDto; +import io.mosip.kernel.masterdata.dto.DynamicFieldPutDto; +import io.mosip.kernel.masterdata.dto.request.FilterDto; +import io.mosip.kernel.masterdata.dto.request.FilterValueDto; +import io.mosip.kernel.masterdata.dto.request.Pagination; +import io.mosip.kernel.masterdata.dto.request.SearchDto; +import io.mosip.kernel.masterdata.dto.request.SearchSort; +import io.mosip.kernel.masterdata.test.TestBootApplication; +import io.mosip.kernel.masterdata.test.utils.MasterDataTest; +import io.mosip.kernel.masterdata.utils.AuditUtil; import io.mosip.kernel.masterdata.validator.FilterColumnEnum; -import org.json.JSONObject; import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; @@ -24,18 +33,8 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; - -import io.mosip.kernel.core.http.RequestWrapper; -import io.mosip.kernel.core.websub.model.EventModel; -import io.mosip.kernel.core.websub.spi.PublisherClient; -import io.mosip.kernel.masterdata.dto.DynamicFieldDto; -import io.mosip.kernel.masterdata.dto.DynamicFieldPutDto; -import io.mosip.kernel.masterdata.test.TestBootApplication; -import io.mosip.kernel.masterdata.test.utils.MasterDataTest; -import io.mosip.kernel.masterdata.utils.AuditUtil; +import java.util.ArrayList; +import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = TestBootApplication.class) @@ -418,4 +417,12 @@ public void t022getDynamicFieldByNameTest2() throws Exception { "KER-SCH-003"); } + @Test + @WithUserDetails("global-admin") + public void t000getAllDynamicFieldByNameTest() throws Exception { + MasterDataTest.checkResponse( + mockMvc.perform(MockMvcRequestBuilders.get("/dynamicfields/all/gender")).andReturn(), + null); + } + } diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java index 01906e1043c..0c60f6508bc 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java @@ -1,35 +1,6 @@ package io.mosip.kernel.masterdata.test.service; -import static org.junit.Assert.assertEquals; - -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import io.mosip.kernel.masterdata.service.impl.SchemaDefinitionServiceImpl; -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Sort; -import org.springframework.data.domain.Sort.Direction; -import org.springframework.http.HttpHeaders; -import org.springframework.security.test.context.support.WithUserDetails; -import org.springframework.test.context.junit4.SpringRunner; - import io.mosip.kernel.core.dataaccess.exception.DataAccessLayerException; import io.mosip.kernel.core.websub.model.EventModel; import io.mosip.kernel.core.websub.spi.PublisherClient; @@ -50,7 +21,34 @@ import io.mosip.kernel.masterdata.service.DynamicFieldService; import io.mosip.kernel.masterdata.service.IdentitySchemaService; import io.mosip.kernel.masterdata.service.TemplateService; +import io.mosip.kernel.masterdata.service.impl.SchemaDefinitionServiceImpl; import io.mosip.kernel.masterdata.test.TestBootApplication; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; +import org.springframework.http.HttpHeaders; +import org.springframework.security.test.context.support.WithUserDetails; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; /** * @@ -95,10 +93,12 @@ public class SchemaServiceTest { private DynamicField mstatusField; PageRequest pageRequest = null; + + List list; @Before public void setup() { - List list = new ArrayList(); + list = new ArrayList(); bloodTypeField = new DynamicField(); bloodTypeField.setDataType("simpleType"); bloodTypeField.setDescription("test"); @@ -328,4 +328,11 @@ public void testDeleteIdentitySchemaFailedUpdate() throws Exception { identitySchemaService.deleteSchema("test-test"); } + @Test + @WithUserDetails("reg-officer") + public void testFetchAllDynamicFieldsAllLang() throws Exception { + Mockito.when(dynamicFieldRepository.findAllDynamicFieldByName(Mockito.anyString())).thenReturn(list); + dynamicFieldService.getAllDynamicFieldByName("gender"); + } + }