Skip to content

Commit

Permalink
Replaced object[] with map for query resullt
Browse files Browse the repository at this point in the history
Signed-off-by: Piyush7034 <[email protected]>
  • Loading branch information
Piyush7034 committed Nov 26, 2024
1 parent 324bafa commit 5374bc8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 57 deletions.
38 changes: 10 additions & 28 deletions postgres-dataprovider-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</scm>
<developers>
<developer>
<name>Mosip</name>
<name>MOSIP</name>
<email>[email protected]</email>
<organization>io.mosip</organization>
<organizationUrl>https://www.mosip.io</organizationUrl>
Expand All @@ -46,16 +46,9 @@
<git-commit-id-plugin.version>3.0.1</git-commit-id-plugin.version>
<maven.jacoco.version>0.8.11</maven.jacoco.version>
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
<kernel-keymanager-service.version>1.3.0-beta.1</kernel-keymanager-service.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -68,27 +61,16 @@
<version>0.10.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.mosip.kernel</groupId>
<artifactId>kernel-keymanager-service</artifactId>
<version>${kernel-keymanager-service.version}</version>
<scope>provided</scope>
<classifier>lib</classifier>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.1.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> fetchQueryResult(String id, String queryString);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
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 {
@PersistenceContext
private EntityManager entityManager;

@Override
public Object[] fetchDataFromIdentifier(String id, String queryString) {
Query query = entityManager.createNativeQuery(queryString);
public Map<String, Object> fetchQueryResult(String id, String queryString) {
Query query = entityManager.createNativeQuery(queryString, Tuple.class);
query.setParameter("id", id);
return (Object[]) query.getSingleResult();
List<Tuple> list = query.getResultList();
List<Map<String, Object>> result = convertTuplesToMap(list);
return result.getFirst();
}

public static List<Map<String, Object>> convertTuplesToMap(List<Tuple> tuples) {
List<Map<String, Object>> result = new ArrayList<>();
for (Tuple single : tuples) {
Map<String, Object> tempMap = new HashMap<>();
for (TupleElement<?> key : single.getElements()) {
tempMap.put(key.getAlias(), single.get(key));
}
result.add(tempMap);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,24 @@ public class PostgresDataProviderPlugin implements DataProviderPlugin {
@Autowired
private ObjectMapper objectMapper;

@Value("#{${mosip.certify.postgres.scope-values}}")
private LinkedHashMap<String, LinkedHashMap<String, String>> scopeToQueryMapping;
@Value("#{${mosip.certify.data-provider-plugin.postgres.scope-query-mapping}}")
private LinkedHashMap<String, String> scopeQueryMapping;

@Override
public JSONObject fetchData(Map<String, Object> identityDetails) throws DataProviderExchangeException {
try {
String individualId = (String) identityDetails.get("sub");
String scope = (String) identityDetails.get("scope");
LinkedHashMap<String, String> queryMap = scopeToQueryMapping.get(scope);
String queryString = scopeQueryMapping.get(scope);
if (individualId != null) {
Object[] dataRecord = dataProviderRepository.fetchDataFromIdentifier(individualId,
queryMap.get("query"));
List<String> includeFields = Arrays.asList(queryMap.get("fields").split(","));
JSONObject jsonRes = new JSONObject();
for(int i=0;i<dataRecord.length;i++) {
jsonRes.put(includeFields.get(i), dataRecord[i]);
}
return jsonRes;
Map<String, Object> 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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ public class PostgresDataProviderPluginTest {

@Before
public void setup() {
LinkedHashMap<String, LinkedHashMap<String, String>> scopeMapping = new LinkedHashMap<>();
LinkedHashMap<String, String> 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", "[email protected]", 100.24};
Mockito.when(dataProviderRepository.fetchDataFromIdentifier("1234567", "testQuery")).thenReturn(obj);
LinkedHashMap<String, String> scopeQueryMapping = new LinkedHashMap<>();
scopeQueryMapping.put("test_vc_ldp", "test_query");
ReflectionTestUtils.setField(postgresDataProviderPlugin, "scopeQueryMapping", scopeQueryMapping);
Map<String, Object> queryResult = Map.of("id","1234567", "name", "John Doe", "dateOfBirth", "01/01/1980", "phoneNumber", "012345", "email", "[email protected]", "landArea", 100.24);
Mockito.when(dataProviderRepository.fetchQueryResult("1234567", "test_query")).thenReturn(queryResult);
}

@Test
Expand All @@ -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());
}
}

Expand All @@ -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());
}
}
}

0 comments on commit 5374bc8

Please sign in to comment.