From c654d79e01d35db3947c08d9c125b68254e03d06 Mon Sep 17 00:00:00 2001 From: Piyush7034 Date: Sat, 14 Dec 2024 19:57:13 +0530 Subject: [PATCH] [INJICERT-657] Remove mock-ida-dataprovider-plugin project from release and last sprint handover review comments (#85) * [INJICERT-657] Removed mock-ida-dataprovider-plugin project as it is not a part of 0.10.0 certify release Signed-off-by: Piyush7034 * Added code review changes for release Signed-off-by: Piyush7034 --------- Signed-off-by: Piyush7034 Signed-off-by: Vishwa --- .github/workflows/push-trigger.yml | 41 -- .../service/DataProviderService.java | 70 ---- .../service/MockCSVDataProviderPlugin.java | 61 ++- .../java/io/mosip/certify/util/CSVReader.java | 86 ++--- .../service/DataProviderServiceTest.java | 36 -- .../MockCSVDataProviderPluginTest.java | 19 +- .../io/mosip/certify/util/CSVReaderTest.java | 56 ++- .../test/resources/farmer_identity_data.csv | 8 - .../src/test/resources/test.csv | 2 +- mock-ida-dataprovider-plugin/pom.xml | 353 ------------------ .../service/MockIdaDataProviderPlugin.java | 141 ------- .../service/MockTransactionHelper.java | 25 -- .../MockIdaDataProviderPluginTest.java | 94 ----- .../service/MockTransactionHelperTest.java | 57 --- 14 files changed, 124 insertions(+), 925 deletions(-) delete mode 100644 mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/DataProviderService.java delete mode 100644 mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/DataProviderServiceTest.java delete mode 100644 mock-certify-plugin/src/test/resources/farmer_identity_data.csv delete mode 100644 mock-ida-dataprovider-plugin/pom.xml delete mode 100644 mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java delete mode 100644 mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java delete mode 100644 mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java delete mode 100644 mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java diff --git a/.github/workflows/push-trigger.yml b/.github/workflows/push-trigger.yml index 3296756..6467373 100644 --- a/.github/workflows/push-trigger.yml +++ b/.github/workflows/push-trigger.yml @@ -187,47 +187,6 @@ jobs: GPG_SECRET: ${{ secrets.GPG_SECRET }} SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} - build-maven-mock-ida-dataprovider-plugin: - uses: mosip/kattu/.github/workflows/maven-build.yml@master-java21 - with: - SERVICE_LOCATION: mock-ida-dataprovider-plugin - BUILD_ARTIFACT: mock-ida-dataprovider-plugin - secrets: - OSSRH_USER: ${{ secrets.OSSRH_USER }} - OSSRH_SECRET: ${{ secrets.OSSRH_SECRET }} - OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }} - GPG_SECRET: ${{ secrets.GPG_SECRET }} - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} - - publish_to_nexus_mock-ida-dataprovider-plugin: - if: "${{ !contains(github.ref, 'master') && github.event_name != 'pull_request' && github.event_name != 'release' && github.event_name != 'prerelease' && github.event_name != 'publish' }}" - needs: build-maven-mock-ida-dataprovider-plugin - uses: mosip/kattu/.github/workflows/maven-publish-to-nexus.yml@master-java21 - with: - SERVICE_LOCATION: ./mock-ida-dataprovider-plugin - secrets: - OSSRH_USER: ${{ secrets.OSSRH_USER }} - OSSRH_SECRET: ${{ secrets.OSSRH_SECRET }} - OSSRH_URL: ${{ secrets.OSSRH_SNAPSHOT_URL }} - OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }} - GPG_SECRET: ${{ secrets.GPG_SECRET }} - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} - - sonar_analysis_mock-ida-dataprovider-plugin: - needs: build-maven-mock-ida-dataprovider-plugin - if: "${{ github.event_name != 'pull_request' }}" - uses: mosip/kattu/.github/workflows/maven-sonar-analysis.yml@master-java21 - with: - SERVICE_LOCATION: ./mock-ida-dataprovider-plugin - secrets: - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - ORG_KEY: ${{ secrets.ORG_KEY }} - OSSRH_USER: ${{ secrets.OSSRH_USER }} - OSSRH_SECRET: ${{ secrets.OSSRH_SECRET }} - OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }} - GPG_SECRET: ${{ secrets.GPG_SECRET }} - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} - build-maven-postgres-dataprovider-plugin: uses: mosip/kattu/.github/workflows/maven-build.yml@master-java21 with: diff --git a/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/DataProviderService.java b/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/DataProviderService.java deleted file mode 100644 index e12837f..0000000 --- a/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/DataProviderService.java +++ /dev/null @@ -1,70 +0,0 @@ -package io.mosip.certify.mock.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.certify.util.CSVReader; -import jakarta.annotation.PostConstruct; -import lombok.extern.slf4j.Slf4j; -import org.json.JSONException; -import org.json.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpMethod; -import org.springframework.stereotype.Service; -import org.springframework.util.ResourceUtils; -import org.springframework.util.StreamUtils; -import org.springframework.web.client.RestTemplate; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; - -@Service -@Slf4j -public class DataProviderService { - @Autowired - private CSVReader csvReader; - // TODO: Change the properties value as per convention later - @Value("${mosip.certify.plugin.csv.file.uri}") - private String fileReference; - @Autowired - private RestTemplate restTemplate; - - /** - * initialize sets up a CSV data for this DataProviderPlugin on start of application - * @return - */ - @PostConstruct - public File initialize() throws IOException { - File f; - if (fileReference.startsWith("http")) { - // download the file to a path: usecase(docker, spring cloud config) - f = restTemplate.execute(fileReference, HttpMethod.GET, null, resp -> { - File ret = File.createTempFile("download", "tmp"); - StreamUtils.copy(resp.getBody(), new FileOutputStream(ret)); - return ret; - }); - } else if (fileReference.startsWith("classpath:")) { - try { - // usecase(local setup) - f = ResourceUtils.getFile(fileReference); - } catch (IOException e) { - throw new FileNotFoundException("File not found in: " + fileReference); - } - } else { - // usecase(local setup) - f = new File(fileReference); - if (!f.isFile()) { - // TODO: make sure it crashes the application - throw new FileNotFoundException("File not found: " + fileReference); - } - } - csvReader.readCSV(f); - return f; - } - - public JSONObject fetchDataFromCSVReader(String identifier) throws JSONException, DataProviderExchangeException { - JSONObject result = csvReader.getJsonObjectByIdentifier(identifier); - return result; - } -} diff --git a/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/MockCSVDataProviderPlugin.java b/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/MockCSVDataProviderPlugin.java index 98be5f2..1d1506b 100644 --- a/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/MockCSVDataProviderPlugin.java +++ b/mock-certify-plugin/src/main/java/io.mosip.certify.mock.integration/service/MockCSVDataProviderPlugin.java @@ -3,32 +3,83 @@ import io.mosip.certify.api.exception.DataProviderExchangeException; import io.mosip.certify.api.spi.DataProviderPlugin; +import io.mosip.certify.util.CSVReader; +import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; +import org.json.JSONException; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; +import org.springframework.util.ResourceUtils; +import org.springframework.util.StreamUtils; +import org.springframework.web.client.RestTemplate; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.Map; +import java.util.Set; @ConditionalOnProperty(value = "mosip.certify.integration.data-provider-plugin", havingValue = "MockCSVDataProviderPlugin") @Component @Slf4j public class MockCSVDataProviderPlugin implements DataProviderPlugin { - @Autowired - private DataProviderService dataService; @Value("${mosip.certify.mock.vciplugin.id-uri:https://example.com/}") private String id; + @Autowired + private CSVReader csvReader; + @Value("${mosip.certify.mock.data-provider.csv-registry-uri}") + private String csvRegistryURI; + @Value("${mosip.certify.mock.data-provider.csv.identifier-column}") + private String identifierColumn; + @Value("#{'${mosip.certify.mock.data-provider.csv.data-columns}'.split(',')}") + private Set dataColumns; + @Autowired + private RestTemplate restTemplate; + + /** + * initialize sets up a CSV data for this DataProviderPlugin on start of application + * @return + */ + @PostConstruct + public File initialize() throws IOException, JSONException { + File filePath; + if (csvRegistryURI.startsWith("http")) { + // download the file to a path: usecase(docker, spring cloud config) + filePath = restTemplate.execute(csvRegistryURI, HttpMethod.GET, null, resp -> { + File ret = File.createTempFile("download", "tmp"); + StreamUtils.copy(resp.getBody(), new FileOutputStream(ret)); + return ret; + }); + } else if (csvRegistryURI.startsWith("classpath:")) { + try { + // usecase(local setup) + filePath = ResourceUtils.getFile(csvRegistryURI); + } catch (IOException e) { + throw new FileNotFoundException("File not found in: " + csvRegistryURI); + } + } else { + // usecase(local setup) + filePath = new File(csvRegistryURI); + if (!filePath.isFile()) { + // TODO: make sure it crashes the application + throw new FileNotFoundException("File not found: " + csvRegistryURI); + } + } + csvReader.readCSV(filePath, identifierColumn, dataColumns); + return filePath; + } @Override public JSONObject fetchData(Map identityDetails) throws DataProviderExchangeException { try { String individualId = (String) identityDetails.get("sub"); if (individualId != null) { - JSONObject jsonRes; - jsonRes = dataService.fetchDataFromCSVReader(individualId); - jsonRes.put("id", id + individualId); + JSONObject jsonRes = csvReader.getJsonObjectByIdentifier(individualId); return jsonRes; } } catch (Exception e) { diff --git a/mock-certify-plugin/src/main/java/io/mosip/certify/util/CSVReader.java b/mock-certify-plugin/src/main/java/io/mosip/certify/util/CSVReader.java index 0edb046..b161f2f 100644 --- a/mock-certify-plugin/src/main/java/io/mosip/certify/util/CSVReader.java +++ b/mock-certify-plugin/src/main/java/io/mosip/certify/util/CSVReader.java @@ -19,75 +19,45 @@ @Component @Slf4j public class CSVReader { - private Map>> dataMap = new HashMap<>(); - - @Value("${mosip.certify.data-provider.identifier.column}") - private String identifierColumn; - @Value("${mosip.certify.data-provider.fields.include}") - private String includeFields; - - private Set fieldsToInclude; - - @PostConstruct - public void init() { - // Convert comma-separated fields to Set - // TODO: https://stackoverflow.com/questions/56454902/spring-value-with-arraylist-split-and-obtain-the-first-value - // We can get all fields in the Set directly - fieldsToInclude = new HashSet<>(Arrays.asList(includeFields.split(","))); - } - - public void readCSV(File f) throws IOException { - try { - // TODO: Eliminate nested try-catch - try (FileReader reader = new FileReader(f); - CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())) { - // Get header names - List headers = csvParser.getHeaderNames(); - // Validate that identifier column exists - if (!headers.contains(identifierColumn)) { - throw new IllegalArgumentException("Identifier column " + identifierColumn + " not found in CSV"); - } + private Map dataMap = new HashMap<>(); + + public void readCSV(File filePath, String identifierColumn, Set dataColumns) throws IOException, JSONException { + try (FileReader reader = new FileReader(filePath); + CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())) { + // Get header names + List headers = csvParser.getHeaderNames(); + // Validate that identifier column exists + if (!headers.contains(identifierColumn)) { + throw new IllegalArgumentException("Identifier column " + identifierColumn + " not found in CSV"); + } - // Process each record - for (CSVRecord record : csvParser) { - String identifier = record.get(identifierColumn); - Map rowData = new HashMap<>(); - // Store only the configured fields - for (String header : headers) { - if (fieldsToInclude.contains(header) || header.equals(identifierColumn)) { - rowData.put(header, record.get(header)); - } + // Process each record + for (CSVRecord record : csvParser) { + String identifier = record.get(identifierColumn); + JSONObject jsonObject = new JSONObject(); + // Store only the configured fields + for (String header : headers) { + if (dataColumns.contains(header) || header.equals(identifierColumn)) { + jsonObject.put(header, record.get(header)); } - - // Add to dataMap - dataMap.computeIfAbsent(identifier, k -> new ArrayList<>()).add(rowData); } - } catch (IOException e) { - log.error("Error finding csv file path", e); - throw new IOException("Unable to find the CSV file."); + + // Add to dataMap + dataMap.put(identifier, jsonObject); } } catch (IOException e) { - log.error("Error fetching csv file from classpath resource", e); - throw new IOException("Unable to find the classpath resource for csv file."); + log.error("Error finding csv file path", e); + throw new IOException("Unable to find the CSV file."); } } public JSONObject getJsonObjectByIdentifier(String identifier) throws DataProviderExchangeException, JSONException { - JSONObject jsonObject = new JSONObject(); - List> records = dataMap.get(identifier); - if(records == null || records.isEmpty()) { + JSONObject record = dataMap.get(identifier); + if(record == null) { log.error("No identifier found."); throw new DataProviderExchangeException("No record found in csv with the provided identifier"); } - if (records != null && !records.isEmpty()) { - Map record = records.get(0); - // Add only configured fields to JsonObject - for (Map.Entry entry : record.entrySet()) { - if (fieldsToInclude.contains(entry.getKey()) || entry.getKey().equals(identifierColumn)) { - jsonObject.put(entry.getKey(), entry.getValue()); - } - } - } - return jsonObject; + + return record; } } diff --git a/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/DataProviderServiceTest.java b/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/DataProviderServiceTest.java deleted file mode 100644 index bf9ea95..0000000 --- a/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/DataProviderServiceTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.mosip.certify.mock.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import net.javacrumbs.jsonunit.assertj.JsonAssertions; -import io.mosip.certify.util.CSVReader; -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.web.client.RestTemplate; - -@RunWith(MockitoJUnitRunner.class) -public class DataProviderServiceTest { - @Mock - CSVReader csvReader; - @Mock - private RestTemplate restTemplate; - - @InjectMocks - DataProviderService dataProviderService = new DataProviderService(); - - void setup() { - csvReader = new CSVReader(); - } - - // TODO: Write test for initialize() - - @Test - public void fetchDataFromValidFile_thenPass() throws JSONException, DataProviderExchangeException { - JSONObject actual = dataProviderService.fetchDataFromCSVReader("1234567"); - JsonAssertions.assertThatJson(actual).isEqualTo(null); - } -} diff --git a/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/MockCSVDataProviderPluginTest.java b/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/MockCSVDataProviderPluginTest.java index f9895ed..eb1d20b 100644 --- a/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/MockCSVDataProviderPluginTest.java +++ b/mock-certify-plugin/src/test/java/io/mosip/certify/mock/integration/service/MockCSVDataProviderPluginTest.java @@ -1,6 +1,7 @@ package io.mosip.certify.mock.integration.service; import io.mosip.certify.api.exception.DataProviderExchangeException; +import io.mosip.certify.util.CSVReader; import org.json.JSONException; import org.json.JSONObject; import org.junit.Assert; @@ -11,31 +12,39 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; +import java.util.Arrays; +import java.util.HashSet; import java.util.Map; +import java.util.Set; @RunWith(MockitoJUnitRunner.class) public class MockCSVDataProviderPluginTest { @Mock - DataProviderService dataProviderService; + CSVReader csvReader; @InjectMocks - MockCSVDataProviderPlugin mockIdaDataProviderPlugin = new MockCSVDataProviderPlugin(); + MockCSVDataProviderPlugin mockCSVDataProviderPlugin = new MockCSVDataProviderPlugin(); @Before public void setup() throws JSONException, DataProviderExchangeException { + String dataColumnFields = "name,age,phone"; + Set dataColumns = new HashSet<>(Arrays.asList(dataColumnFields.split(","))); + ReflectionTestUtils.setField(mockCSVDataProviderPlugin, "identifierColumn", "individualId"); + ReflectionTestUtils.setField(mockCSVDataProviderPlugin, "dataColumns", dataColumns); JSONObject jsonObject = new JSONObject(); jsonObject.put("individualId", "1234567"); jsonObject.put("name", "John Doe"); jsonObject.put("age", "40"); jsonObject.put("phone", "98765"); - Mockito.when(dataProviderService.fetchDataFromCSVReader("1234567")).thenReturn(jsonObject); + Mockito.when(csvReader.getJsonObjectByIdentifier("1234567")).thenReturn(jsonObject); } @Test public void fetchJsonDataWithValidIndividualId_thenPass() throws DataProviderExchangeException, JSONException { - JSONObject jsonObject = mockIdaDataProviderPlugin.fetchData(Map.of("sub", "1234567", "client_id", "CLIENT_ID")); + JSONObject jsonObject = mockCSVDataProviderPlugin.fetchData(Map.of("sub", "1234567", "client_id", "CLIENT_ID")); Assert.assertNotNull(jsonObject); Assert.assertNotNull(jsonObject.get("name")); Assert.assertNotNull(jsonObject.get("phone")); @@ -50,7 +59,7 @@ public void fetchJsonDataWithValidIndividualId_thenPass() throws DataProviderExc @Test public void fetchJsonDataWithInValidIndividualId_thenFail() { try { - mockIdaDataProviderPlugin.fetchData(Map.of("sub", "12345678", "client_id", "CLIENT_ID")); + mockCSVDataProviderPlugin.fetchData(Map.of("sub", "12345678", "client_id", "CLIENT_ID")); } catch (DataProviderExchangeException e) { Assert.assertEquals("ERROR_FETCHING_IDENTITY_DATA", e.getMessage()); } diff --git a/mock-certify-plugin/src/test/java/io/mosip/certify/util/CSVReaderTest.java b/mock-certify-plugin/src/test/java/io/mosip/certify/util/CSVReaderTest.java index 2191aff..9a872ab 100644 --- a/mock-certify-plugin/src/test/java/io/mosip/certify/util/CSVReaderTest.java +++ b/mock-certify-plugin/src/test/java/io/mosip/certify/util/CSVReaderTest.java @@ -2,7 +2,6 @@ import io.mosip.certify.api.exception.DataProviderExchangeException; import lombok.SneakyThrows; -import org.assertj.core.api.Assertions; import org.json.JSONException; import org.json.JSONObject; import org.junit.Assert; @@ -22,66 +21,61 @@ public class CSVReaderTest { @InjectMocks CSVReader csvReader; - Set fieldsToInclude; - Map>> dataMap = new HashMap<>(); + Set dataColumns; + Map dataMap = new HashMap<>(); @Before public void setup() { - String includeFields = "name,age,phone"; - ReflectionTestUtils.setField(csvReader, "identifierColumn", "individualid"); - ReflectionTestUtils.setField(csvReader, "includeFields", includeFields); - - fieldsToInclude = new HashSet<>(Arrays.asList(includeFields.split(","))); - ReflectionTestUtils.setField(csvReader, "fieldsToInclude", fieldsToInclude); + String dataColumnFields = "name,age,phone"; + dataColumns = new HashSet<>(Arrays.asList(dataColumnFields.split(","))); ReflectionTestUtils.setField(csvReader, "dataMap", dataMap); } @Test - public void readCSVDataFromValidFile_thenPass() throws IOException { + public void readCSVDataFromValidFile_thenPass() throws IOException, JSONException { File f = new File("src/test/resources/test.csv"); - csvReader.readCSV(f); + csvReader.readCSV(f, "individualId", dataColumns); Assert.assertNotNull(dataMap); Assert.assertNotNull(dataMap.get("1234567")); - List expected = List.of(Map.of("phone", "9876543210", "name", "John Doe", "individualid", "1234567", "age", "40")); - List> actual = dataMap.get("1234567"); - Assert.assertEquals(expected, actual); - Assert.assertTrue(expected.equals(actual)); - Assert.assertNotNull(dataMap.get("2345678")); + JSONObject jsonObject = dataMap.get("1234567"); + Assert.assertNotNull(jsonObject); + Assert.assertEquals("John Doe", jsonObject.get("name")); + Assert.assertEquals("9876543210", jsonObject.get("phone")); + Assert.assertEquals("40", jsonObject.get("age")); } @Test - public void readCSVDataFromInvalidFile_thenFail() { + public void readCSVDataFromInvalidFile_thenFail() throws JSONException { try { File f = new File("test.csv"); - csvReader.readCSV(f); + csvReader.readCSV(f, "individualId", dataColumns); } catch (IOException e) { - Assert.assertEquals("Unable to find the classpath resource for csv file.", e.getMessage()); + Assert.assertEquals("Unable to find the CSV file.", e.getMessage()); } } @SneakyThrows @Test public void getJsonObjectByValidIdentifier_thenPass() { - Map>> data = new HashMap<>(); - data.put("1234567", List.of(Map.of("individualid", "1234567", "name", "test", "age", "40", "phone", "98765"))); + Map data = new HashMap<>(); + JSONObject jsonObject = new JSONObject(Map.of("phone", "9876543210", "name", "John Doe", "individualId", "1234567", "age", "40")); + data.put("1234567", jsonObject); ReflectionTestUtils.setField(csvReader, "dataMap", data); - JSONObject jsonObject = null; - jsonObject = csvReader.getJsonObjectByIdentifier("1234567"); + JSONObject jsonObjectResult = csvReader.getJsonObjectByIdentifier("1234567"); Assert.assertNotNull(jsonObject); - Assert.assertEquals("1234567", jsonObject.get("individualid")); - Assert.assertEquals("test", jsonObject.get("name")); - Assert.assertEquals("40", jsonObject.get("age")); - Assert.assertEquals("98765", jsonObject.get("phone")); + Assert.assertEquals("1234567", jsonObjectResult.get("individualId")); + Assert.assertEquals("John Doe", jsonObjectResult.get("name")); + Assert.assertEquals("40", jsonObjectResult.get("age")); + Assert.assertEquals("9876543210", jsonObjectResult.get("phone")); } @Test public void getJsonObjectByInvalidIdentifier_thenFail() throws JSONException { - Map>> data = new HashMap<>(); - data.put("1234567", List.of(Map.of("individualid", "1234567", "name", "test", "age", "40", "phone", "98765"))); + Map data = new HashMap<>(); + JSONObject jsonObject = new JSONObject(Map.of("phone", "9876543210", "name", "John Doe", "individualId", "1234567", "age", "40")); + data.put("1234567", jsonObject); ReflectionTestUtils.setField(csvReader, "dataMap", data); - Assertions.catchThrowable(() -> csvReader.getJsonObjectByIdentifier("12345678")); - // TODO: Verify if the ^^ line achieves the same thing as below try { csvReader.getJsonObjectByIdentifier("12345678"); } catch (DataProviderExchangeException e) { diff --git a/mock-certify-plugin/src/test/resources/farmer_identity_data.csv b/mock-certify-plugin/src/test/resources/farmer_identity_data.csv deleted file mode 100644 index f168636..0000000 --- a/mock-certify-plugin/src/test/resources/farmer_identity_data.csv +++ /dev/null @@ -1,8 +0,0 @@ -id,name,phoneNumber,dateOfBirth,highestEducation,typeOfHouse,numberOfDependents,works,landArea,landOwnershipType,primaryCropType,secondaryCropType,maritalStatus -4567538768,John Doe,9876543210,1980-05-15,Bachelors Degree,Farmhouse,3,Full-time,50.5,Self-owned,Wheat,Vegetables,Married -4567538769,Mary Smith,8765432109,1975-08-22,High School,Ranch,4,Full-time,25.75,Leased,Rice,Pulses,Married -4567538770,Raj Patel,7654321098,1990-03-10,Primary Education,Cottage,2,Part-time,15.25,Family Owned,Cotton,Maize,Married -4567538771,Sarah Johnson,6543210987,1985-11-30,Diploma,Ranch,1,Full-time,35.5,Self-owned,Sugarcane,Rice,Single -4567538772,Kumar Swamy,5432109876,1970-04-18,No Formal Education,Cottage,5,Full-time,45.0,Self-owned,Maize,Wheat,Married -4567538773,Li Wei,4321098765,1988-09-25,Masters Degree,Farmhouse,2,Part-time,20.5,Leased,Vegetables,Fruits,Married -4567538774,Arun Kumar,9876543211,1987-06-20,Diploma,Cottage,2,Part-time,22.75,Family Owned,Rice,Cotton,Married diff --git a/mock-certify-plugin/src/test/resources/test.csv b/mock-certify-plugin/src/test/resources/test.csv index c7c2f2a..933fad3 100644 --- a/mock-certify-plugin/src/test/resources/test.csv +++ b/mock-certify-plugin/src/test/resources/test.csv @@ -1,4 +1,4 @@ -individualid,name,phone,age +individualId,name,phone,age 1234567,John Doe,9876543210,40 9876543,Mary Smith,8765432109,45 2345678,Raj Patel,7654321098,28 diff --git a/mock-ida-dataprovider-plugin/pom.xml b/mock-ida-dataprovider-plugin/pom.xml deleted file mode 100644 index 4ba5aa8..0000000 --- a/mock-ida-dataprovider-plugin/pom.xml +++ /dev/null @@ -1,353 +0,0 @@ - - 4.0.0 - - io.mosip.certify - mock-ida-dataprovider-plugin - 0.3.0-SNAPSHOT - jar - - mock-ida-dataprovider-plugin - Mockup of a data provider plugin implementation that is used to showcase the integration with certify - https://github.com/mosip/digital-credential-plugins - - - - MPL 2.0 - https://www.mozilla.org/en-US/MPL/2.0/ - - - - scm:git:git://github.com/mosip/digital-credential-plugins.git - scm:git:ssh://github.com:mosip/digital-credential-plugins.git - https://github.com/mosip/digital-credential-plugins - HEAD - - - - Mosip - mosip.emailnotifier@gmail.com - io.mosip - https://www.mosip.io - - - - - UTF-8 - 21 - 3.7.1 - 21 - 21 - 3.10.1 - 3.2.5 - 1.5 - 2.2.1 - 6.1.0 - 3.0.1 - 0.8.11 - 3.6.3 - 1.3.0-beta.1 - - - - - junit - junit - 3.8.1 - test - - - org.projectlombok - lombok - 1.18.30 - provided - - - io.mosip.certify - certify-core - 0.10.0-SNAPSHOT - provided - - - io.mosip.esignet - esignet-core - 1.4.1 - - - * - * - - - - - io.mosip.esignet - esignet-integration-api - 1.4.1 - - - * - * - - - - - - 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.slf4j - slf4j-api - 2.0.12 - - - junit - junit - 4.13.1 - test - - - - - - ossrh - CentralRepository - https://oss.sonatype.org/content/repositories/snapshots - default - - true - - - - central - MavenCentral - default - https://repo1.maven.org/maven2 - - false - - - - danubetech-maven-public - https://repo.danubetech.com/repository/maven-public/ - - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - ${maven-assembly-plugin.version} - - - jar-with-dependencies - - false - - - - make-assembly - package - - single - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - - attach-javadocs - - jar - - - - - none - src/main/java - - - - maven-deploy-plugin - 3.1.2 - - - default-deploy - deploy - - deploy - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.14 - true - - - default-deploy - deploy - - deploy - - - - - ossrh - https://oss.sonatype.org/ - false - - - - - org.apache.maven.plugins - maven-source-plugin - true - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - --pinentry-mode - loopback - - - - - - - pl.project13.maven - git-commit-id-plugin - 3.0.1 - - - get-the-git-infos - - revision - - validate - - - - true - ${project.build.outputDirectory}/git.properties - - ^git.build.(time|version)$ - ^git.commit.id.(abbrev|full)$ - - full - ${project.basedir}/.git - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - false - false - - ${argLine} --add-opens - java.xml/jdk.xml.internal=ALL-UNNAMED - --illegal-access=permit - - - - - org.jacoco - jacoco-maven-plugin - ${maven.jacoco.version} - - - - prepare-agent - - - - report - prepare-package - - report - - - - - - org.apache.maven.plugins - maven-antrun-plugin - 3.1.0 - - - make-jar-executable - package - - run - - - - - - - - - - - - - \ No newline at end of file diff --git a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java deleted file mode 100644 index c76eb61..0000000 --- a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java +++ /dev/null @@ -1,141 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.certify.api.spi.DataProviderPlugin; -import io.mosip.certify.core.exception.CertifyException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import io.mosip.kernel.core.keymanager.spi.KeyStore; -import io.mosip.kernel.keymanagerservice.constant.KeymanagerConstant; -import io.mosip.kernel.keymanagerservice.entity.KeyAlias; -import io.mosip.kernel.keymanagerservice.helper.KeymanagerDBHelper; -import lombok.extern.slf4j.Slf4j; -import org.json.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -import javax.crypto.Cipher; -import java.security.Key; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.*; - -@ConditionalOnProperty(value = "mosip.certify.integration.data-provider-plugin", havingValue = "MockIdaDataProviderPlugin") -@Component -@Slf4j -public class MockIdaDataProviderPlugin implements DataProviderPlugin { - private static final String AES_CIPHER_FAILED = "aes_cipher_failed"; - private static final String NO_UNIQUE_ALIAS = "no_unique_alias"; - - private static final String ACCESS_TOKEN_HASH = "accessTokenHash"; - - public static final String UTC_DATETIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; - - public static final String CERTIFY_SERVICE_APP_ID = "CERTIFY_SERVICE"; - - @Autowired - private RestTemplate restTemplate; - - @Autowired - private KeyStore keyStore; - - @Autowired - private KeymanagerDBHelper dbHelper; - - @Autowired - private MockTransactionHelper mockTransactionHelper; - - @Value("${mosip.certify.mock.authenticator.get-identity-url}") - private String getIdentityUrl; - - @Value("${mosip.certify.cache.security.secretkey.reference-id}") - private String cacheSecretKeyRefId; - - @Value("${mosip.certify.cache.security.algorithm-name}") - private String aesECBTransformation; - - @Value("${mosip.certify.cache.secure.individual-id}") - private boolean isIndividualIDEncrypted; - - @Value("${mosip.certify.cache.store.individual-id}") - private boolean storeIndividualId; - - @Override - public JSONObject fetchData(Map identityDetails) throws DataProviderExchangeException { - try { - OIDCTransaction transaction = mockTransactionHelper.getUserInfoTransaction(identityDetails.get(ACCESS_TOKEN_HASH).toString()); - String individualId = getIndividualId(transaction); - if (individualId != null) { - Map res = restTemplate.getForObject( - getIdentityUrl + "/" + individualId, - HashMap.class); - res = (Map) res.get("response"); - JSONObject jsonRes = new JSONObject(); - jsonRes.put("vcVer", "VC-V1"); - jsonRes.put("id", getIdentityUrl + "/" + individualId); - jsonRes.put("UIN", individualId); - jsonRes.put("fullName", res.get("fullName")); - jsonRes.put("gender", res.get("gender")); - jsonRes.put("dateOfBirth", res.get("dateOfBirth")); - jsonRes.put("email", res.get("email")); - jsonRes.put("phone", res.get("phone")); - jsonRes.put("addressLine1", res.get("streetAddress")); - jsonRes.put("province", res.get("locality")); - jsonRes.put("region", res.get("region")); - jsonRes.put("postalCode", res.get("postalCode")); - jsonRes.put("face", res.get("encodedPhoto")); - return jsonRes; - } - } 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("INVALID_ACCESS_TOKEN"); - } - - protected String getIndividualId(OIDCTransaction transaction) { - if (!storeIndividualId) - return null; - return isIndividualIDEncrypted ? decryptIndividualId(transaction.getIndividualId()) : transaction.getIndividualId(); - } - - private String decryptIndividualId(String encryptedIndividualId) { - try { - Cipher cipher = Cipher.getInstance(aesECBTransformation); - byte[] decodedBytes = Base64.getUrlDecoder().decode(encryptedIndividualId); - cipher.init(Cipher.DECRYPT_MODE, getSecretKeyFromHSM()); - return new String(cipher.doFinal(decodedBytes, 0, decodedBytes.length)); - } catch (Exception e) { - log.error("Error Cipher Operations of provided secret data.", e); - throw new CertifyException(AES_CIPHER_FAILED); - } - } - - private Key getSecretKeyFromHSM() { - String keyAlias = getKeyAlias(CERTIFY_SERVICE_APP_ID, cacheSecretKeyRefId); - if (Objects.nonNull(keyAlias)) { - return keyStore.getSymmetricKey(keyAlias); - } - throw new CertifyException(NO_UNIQUE_ALIAS); - } - - private String getKeyAlias(String keyAppId, String keyRefId) { - Map> keyAliasMap = dbHelper.getKeyAliases(keyAppId, keyRefId, LocalDateTime.now(ZoneOffset.UTC)); - List currentKeyAliases = keyAliasMap.get(KeymanagerConstant.CURRENTKEYALIAS); - if (currentKeyAliases != null && currentKeyAliases.size() == 1) { - return currentKeyAliases.get(0).getAlias(); - } - log.error("CurrentKeyAlias is not unique. KeyAlias count: {}", currentKeyAliases.size()); - throw new CertifyException(NO_UNIQUE_ALIAS); - } - - private static String getUTCDateTime() { - return ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(UTC_DATETIME_PATTERN)); - } -} \ No newline at end of file diff --git a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java deleted file mode 100644 index bf52c42..0000000 --- a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java +++ /dev/null @@ -1,25 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cache.CacheManager; -import org.springframework.stereotype.Component; - -@Component -public class MockTransactionHelper { - @Value("${mosip.certify.mock.vci-user-info-cache:userinfo}") - private String userinfoCache; - - @Autowired - private CacheManager cacheManager; - - public OIDCTransaction getUserInfoTransaction(String accessTokenHash) throws DataProviderExchangeException { - if(cacheManager.getCache(userinfoCache) != null) { - return cacheManager.getCache(userinfoCache).get(accessTokenHash, OIDCTransaction.class); - } - - throw new DataProviderExchangeException("CACHE_MISSING"); - } -} diff --git a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java b/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java deleted file mode 100644 index 9b7631a..0000000 --- a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.esignet.core.dto.OIDCTransaction; -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.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.support.NoOpCache; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.web.client.RestTemplate; - -import java.util.HashMap; -import java.util.Map; - -@RunWith(MockitoJUnitRunner.class) -public class MockIdaDataProviderPluginTest { - @Mock - CacheManager cacheManager; - - @Mock - Cache cache=new NoOpCache("test"); - - @Mock - MockTransactionHelper mockTransactionHelper; - - @Mock - RestTemplate restTemplate; - - @InjectMocks - MockIdaDataProviderPlugin mockDataProviderPlugin; - - @Before - public void setup() throws DataProviderExchangeException { - ReflectionTestUtils.setField(mockDataProviderPlugin,"getIdentityUrl","http://example.com"); - ReflectionTestUtils.setField(mockDataProviderPlugin,"cacheSecretKeyRefId","cacheSecretKeyRefId"); - ReflectionTestUtils.setField(mockDataProviderPlugin,"aesECBTransformation","AES/ECB/PKCS5Padding"); - ReflectionTestUtils.setField(mockDataProviderPlugin,"storeIndividualId",true); - ReflectionTestUtils.setField(mockDataProviderPlugin,"isIndividualIDEncrypted",false); - - OIDCTransaction oidcTransaction = new OIDCTransaction(); - oidcTransaction.setTransactionId("test"); - oidcTransaction.setIndividualId("individualId"); - oidcTransaction.setKycToken("kycToken"); - oidcTransaction.setAuthTransactionId("authTransactionId"); - oidcTransaction.setRelyingPartyId("relyingPartyId"); - oidcTransaction.setClaimsLocales(new String[]{"en-US", "en", "en-CA", "fr-FR", "fr-CA"}); - Mockito.when(mockTransactionHelper.getUserInfoTransaction("ACCESS_TOKEN_HASH")).thenReturn(oidcTransaction); - - Map identityJson = new HashMap<>(); - identityJson.put("fullName", "fullName"); - identityJson.put("gender", "gender"); - identityJson.put("dateOfBirth", "dateOfBirth"); - identityJson.put("email", "email"); - identityJson.put("phone", "phone"); - identityJson.put("streetAddress", "streetAddress"); - identityJson.put("locality", "locality"); - identityJson.put("region", "region"); - identityJson.put("postalCode", "postalCode"); - identityJson.put("encodedPhoto", "encodedPhoto"); - Map response = new HashMap<>(); - response.put("response", identityJson); - Mockito.when(restTemplate.getForObject(Mockito.anyString(), Mockito.any())).thenReturn(response); - } - - @Test - public void getJSONDataWithValidDetails_thenPass() throws DataProviderExchangeException, JSONException { - JSONObject jsonData = mockDataProviderPlugin.fetchData(Map.of("accessTokenHash","ACCESS_TOKEN_HASH","client_id","CLIENT_ID")); - Assert.assertNotNull(jsonData); - Assert.assertNotNull(jsonData.get("fullName")); - Assert.assertEquals("fullName" ,jsonData.get("fullName")); - Assert.assertNotNull(jsonData.get("UIN")); - Assert.assertEquals("individualId", jsonData.get("UIN")); - Assert.assertNotNull(jsonData.get("id")); - Assert.assertEquals("http://example.com/individualId", jsonData.get("id")); - } - - @Test - public void getJSONDataWithInValidDetails_thenFail() { - try { - mockDataProviderPlugin.fetchData(Map.of("accessTokenHash","test","client_id","CLIENT_ID")); - } catch (DataProviderExchangeException e) { - Assert.assertEquals("ERROR_FETCHING_IDENTITY_DATA", e.getMessage()); - } - } -} \ No newline at end of file diff --git a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java b/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java deleted file mode 100644 index dac2b2e..0000000 --- a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.support.NoOpCache; -import org.springframework.test.util.ReflectionTestUtils; - -@RunWith(MockitoJUnitRunner.class) -public class MockTransactionHelperTest { - @Mock - CacheManager cacheManager; - - @Mock - Cache cache=new NoOpCache("test"); - - @InjectMocks - MockTransactionHelper mockTransactionHelper; - - @Before - public void setup() { - ReflectionTestUtils.setField(mockTransactionHelper, "userinfoCache", "test"); - OIDCTransaction oidcTransaction = new OIDCTransaction(); - oidcTransaction.setTransactionId("test"); - oidcTransaction.setIndividualId("individualId"); - oidcTransaction.setKycToken("kycToken"); - oidcTransaction.setAuthTransactionId("authTransactionId"); - oidcTransaction.setRelyingPartyId("relyingPartyId"); - oidcTransaction.setClaimsLocales(new String[]{"en-US", "en", "en-CA", "fr-FR", "fr-CA"}); - - Mockito.when(cacheManager.getCache(Mockito.anyString())).thenReturn(cache); - Mockito.when(cache.get("test", OIDCTransaction.class)).thenReturn(oidcTransaction); - } - - @Test - public void getOIDCTransactionWithValidDetails_thenPass() throws DataProviderExchangeException { - OIDCTransaction transaction = mockTransactionHelper.getUserInfoTransaction("test"); - Assert.assertNotNull(transaction); - Assert.assertEquals("test", transaction.getTransactionId()); - Assert.assertEquals("individualId", transaction.getIndividualId()); - } - - @Test - public void getOIDCTransactionWithInValidUserinfo_thenFail() throws DataProviderExchangeException { - OIDCTransaction transaction = mockTransactionHelper.getUserInfoTransaction("ACCESS_TOKEN_HASH"); - Assert.assertNull(transaction); - } -}