diff --git a/postgres-dataprovider-plugin/pom.xml b/postgres-dataprovider-plugin/pom.xml index 7341362..acc5e35 100644 --- a/postgres-dataprovider-plugin/pom.xml +++ b/postgres-dataprovider-plugin/pom.xml @@ -25,7 +25,7 @@ - Mosip + MOSIP mosip.emailnotifier@gmail.com io.mosip https://www.mosip.io @@ -46,16 +46,9 @@ 3.0.1 0.8.11 3.6.3 - 1.3.0-beta.1 - - junit - junit - 3.8.1 - test - org.projectlombok lombok @@ -68,27 +61,16 @@ 0.10.0-SNAPSHOT provided - - io.mosip.kernel - kernel-keymanager-service - ${kernel-keymanager-service.version} - provided - lib - - - org.springframework.cloud - spring-cloud-starter-sleuth - - - org.springframework.security - spring-security-test - - - org.slf4j - slf4j-api - - + org.mockito + mockito-core + 5.11.0 + test + + + org.springframework + spring-test + 6.1.4 org.slf4j diff --git a/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepository.java b/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepository.java index 2ebde3e..29daaf3 100644 --- a/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepository.java +++ b/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepository.java @@ -1,6 +1,8 @@ package io.mosip.certify.postgresdataprovider.integration.repository; +import java.util.Map; + public interface DataProviderRepository { - Object[] fetchDataFromIdentifier(String id, String queryString); + Map fetchQueryResult(String id, String queryString); } \ No newline at end of file diff --git a/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepositoryImpl.java b/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepositoryImpl.java index 545f414..4a6dcc4 100644 --- a/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepositoryImpl.java +++ b/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/repository/DataProviderRepositoryImpl.java @@ -1,10 +1,13 @@ package io.mosip.certify.postgresdataprovider.integration.repository; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import jakarta.persistence.Query; +import jakarta.persistence.*; import org.springframework.stereotype.Repository; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + @Repository(value = "dataProviderRepository") public class DataProviderRepositoryImpl implements DataProviderRepository { @@ -12,9 +15,23 @@ public class DataProviderRepositoryImpl implements DataProviderRepository { private EntityManager entityManager; @Override - public Object[] fetchDataFromIdentifier(String id, String queryString) { - Query query = entityManager.createNativeQuery(queryString); + public Map fetchQueryResult(String id, String queryString) { + Query query = entityManager.createNativeQuery(queryString, Tuple.class); query.setParameter("id", id); - return (Object[]) query.getSingleResult(); + List list = query.getResultList(); + List> result = convertTuplesToMap(list); + return result.getFirst(); + } + + public static List> convertTuplesToMap(List tuples) { + List> result = new ArrayList<>(); + for (Tuple single : tuples) { + Map tempMap = new HashMap<>(); + for (TupleElement key : single.getElements()) { + tempMap.put(key.getAlias(), single.get(key)); + } + result.add(tempMap); + } + return result; } } \ No newline at end of file diff --git a/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/service/PostgresDataProviderPlugin.java b/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/service/PostgresDataProviderPlugin.java index 5f2832f..e661dd7 100644 --- a/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/service/PostgresDataProviderPlugin.java +++ b/postgres-dataprovider-plugin/src/main/java/io/mosip/certify/postgresdataprovider/integration/service/PostgresDataProviderPlugin.java @@ -25,28 +25,24 @@ public class PostgresDataProviderPlugin implements DataProviderPlugin { @Autowired private ObjectMapper objectMapper; - @Value("#{${mosip.certify.postgres.scope-values}}") - private LinkedHashMap> scopeToQueryMapping; + @Value("#{${mosip.certify.data-provider-plugin.postgres.scope-query-mapping}}") + private LinkedHashMap scopeQueryMapping; @Override public JSONObject fetchData(Map identityDetails) throws DataProviderExchangeException { try { String individualId = (String) identityDetails.get("sub"); String scope = (String) identityDetails.get("scope"); - LinkedHashMap queryMap = scopeToQueryMapping.get(scope); + String queryString = scopeQueryMapping.get(scope); if (individualId != null) { - Object[] dataRecord = dataProviderRepository.fetchDataFromIdentifier(individualId, - queryMap.get("query")); - List includeFields = Arrays.asList(queryMap.get("fields").split(",")); - JSONObject jsonRes = new JSONObject(); - for(int i=0;i dataRecord = dataProviderRepository.fetchQueryResult(individualId, + queryString); + JSONObject jsonResponse = new JSONObject(dataRecord); + return jsonResponse; } } catch (Exception e) { log.error("Failed to fetch json data for from data provider plugin", e); - throw new DataProviderExchangeException("ERROR_FETCHING_IDENTITY_DATA"); + throw new DataProviderExchangeException("ERROR_FETCHING_DATA_RECORD_FROM_TABLE"); } throw new DataProviderExchangeException("No Data Found"); } diff --git a/postgres-dataprovider-plugin/src/test/java/io/mosip/certify/postgresdataprovider/integration/PostgresDataProviderPluginTest.java b/postgres-dataprovider-plugin/src/test/java/io/mosip/certify/postgresdataprovider/integration/PostgresDataProviderPluginTest.java index fd0f93f..3a8f3d5 100644 --- a/postgres-dataprovider-plugin/src/test/java/io/mosip/certify/postgresdataprovider/integration/PostgresDataProviderPluginTest.java +++ b/postgres-dataprovider-plugin/src/test/java/io/mosip/certify/postgresdataprovider/integration/PostgresDataProviderPluginTest.java @@ -28,14 +28,11 @@ public class PostgresDataProviderPluginTest { @Before public void setup() { - LinkedHashMap> scopeMapping = new LinkedHashMap<>(); - LinkedHashMap queryMap = new LinkedHashMap<>(); - queryMap.put("query", "testQuery"); - queryMap.put("fields","individualId,name,dateOfBirth,phoneNumber,email,landArea"); - scopeMapping.put("test_vc_ldp", queryMap); - ReflectionTestUtils.setField(postgresDataProviderPlugin, "scopeToQueryMapping", scopeMapping); - Object[] obj = new Object[]{"1234567", "John Doe", "01/01/1980", "012345", "john@test.com", 100.24}; - Mockito.when(dataProviderRepository.fetchDataFromIdentifier("1234567", "testQuery")).thenReturn(obj); + LinkedHashMap scopeQueryMapping = new LinkedHashMap<>(); + scopeQueryMapping.put("test_vc_ldp", "test_query"); + ReflectionTestUtils.setField(postgresDataProviderPlugin, "scopeQueryMapping", scopeQueryMapping); + Map queryResult = Map.of("id","1234567", "name", "John Doe", "dateOfBirth", "01/01/1980", "phoneNumber", "012345", "email", "john@test.com", "landArea", 100.24); + Mockito.when(dataProviderRepository.fetchQueryResult("1234567", "test_query")).thenReturn(queryResult); } @Test @@ -59,7 +56,7 @@ public void fetchJsonDataWithInValidIndividualId_thenFail() throws DataProviderE try { postgresDataProviderPlugin.fetchData(Map.of("sub", "12345678", "client_id", "CLIENT_ID", "scope", "test_vc_ldp")); } catch (DataProviderExchangeException e) { - Assert.assertEquals("ERROR_FETCHING_IDENTITY_DATA", e.getMessage()); + Assert.assertEquals("ERROR_FETCHING_DATA_RECORD_FROM_TABLE", e.getMessage()); } } @@ -68,7 +65,7 @@ public void fetchJsonDataWithInValidScope_thenFail() throws DataProviderExchange try { postgresDataProviderPlugin.fetchData(Map.of("sub", "1234567", "client_id", "CLIENT_ID", "scope", "sample_vc_ldp")); } catch (DataProviderExchangeException e) { - Assert.assertEquals("ERROR_FETCHING_IDENTITY_DATA", e.getMessage()); + Assert.assertEquals("ERROR_FETCHING_DATA_RECORD_FROM_TABLE", e.getMessage()); } } } \ No newline at end of file