1+ /*
2+ * This Source Code Form is subject to the terms of the Mozilla Public
3+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+ */
6+ package io .mosip .signup .plugin .mosipid .service ;
7+
8+ import com .fasterxml .jackson .databind .JsonNode ;
9+ import com .fasterxml .jackson .databind .ObjectMapper ;
10+ import io .mosip .signup .api .dto .*;
11+ import io .mosip .signup .api .exception .IdentityVerifierException ;
12+ import io .mosip .signup .api .spi .IdentityVerifierPlugin ;
13+ import io .mosip .signup .api .util .ProcessType ;
14+ import io .mosip .signup .api .util .VerificationStatus ;
15+ import lombok .Data ;
16+ import lombok .extern .slf4j .Slf4j ;
17+ import org .springframework .beans .factory .annotation .Autowired ;
18+ import org .springframework .beans .factory .annotation .Value ;
19+ import org .springframework .core .io .Resource ;
20+ import org .springframework .core .io .ResourceLoader ;
21+ import org .springframework .stereotype .Component ;
22+
23+ import java .io .BufferedReader ;
24+ import java .io .IOException ;
25+ import java .io .InputStreamReader ;
26+ import java .nio .charset .StandardCharsets ;
27+ import java .util .List ;
28+ import java .util .Objects ;
29+ import java .util .Optional ;
30+ import java .util .stream .Collectors ;
31+
32+ import static io .mosip .signup .api .util .ProcessType .VIDEO ;
33+
34+ @ Data
35+ class MockScene {
36+ private int frameNumber ;
37+ private String stepCode ;
38+ private IDVProcessStepDetail step ;
39+ private IDVProcessFeedback feedback ;
40+ }
41+
42+ @ Data
43+ class MockUserStory {
44+ private List <MockScene > scenes ;
45+ private JsonNode verificationResult ;
46+ }
47+
48+
49+
50+ @ Slf4j
51+ @ Component
52+ public class MockIdentityVerifierPluginImpl extends IdentityVerifierPlugin {
53+
54+ @ Value ("${mosip.signup.mock.identity-verification.story-name}" )
55+ private String storyName ;
56+
57+ @ Value ("${mosip.signup.mock.config-server-url}" )
58+ private String configServerUrl ;
59+
60+ @ Autowired
61+ private ResourceLoader resourceLoader ;
62+
63+ @ Autowired
64+ private ObjectMapper objectMapper ;
65+
66+ @ Override
67+ public String getVerifierId () {
68+ return "mock-identity-verifier" ;
69+ }
70+
71+ @ Override
72+ public List <ProcessType > getSupportedProcessTypes () {
73+ return List .of (VIDEO );
74+ }
75+
76+ @ Override
77+ public void initialize (String transactionId , IdentityVerificationInitDto identityVerificationInitDto ) {
78+ log .info ("Transaction is initialized for transactionId : {} and disabilityType: {}" ,
79+ transactionId , identityVerificationInitDto .getDisabilityType ());
80+ log .info ("**** Nothing to initialize as its mock identity verification plugin ****" );
81+ }
82+
83+ @ Override
84+ public void verify (String transactionId , IdentityVerificationDto identityVerificationDto ) throws IdentityVerifierException {
85+ MockUserStory mockUserStory = getResource (configServerUrl +storyName , MockUserStory .class );
86+ log .info ("Loaded user story : {} for transaction: {}" , storyName , transactionId );
87+
88+ IdentityVerificationResult identityVerificationResult = new IdentityVerificationResult ();
89+ identityVerificationResult .setId (transactionId );
90+ identityVerificationResult .setVerifierId (getVerifierId ());
91+
92+ log .info ("input message step code : {} for transaction: {}" , identityVerificationDto .getStepCode (), transactionId );
93+ if (isStartStep (identityVerificationDto .getStepCode ())) {
94+ Optional <MockScene > result = Objects .requireNonNull (mockUserStory ).getScenes ().stream ()
95+ .filter (scene -> scene .getFrameNumber () == 0 && scene .getStepCode ().equals (identityVerificationDto .getStepCode ()))
96+ .findFirst ();
97+
98+ if (result .isPresent ()) {
99+ identityVerificationResult .setStep (result .get ().getStep ());
100+ identityVerificationResult .setFeedback (result .get ().getFeedback ());
101+ publishAnalysisResult (identityVerificationResult );
102+ }
103+ }
104+
105+ if (identityVerificationDto .getFrames () == null || identityVerificationDto .getFrames ().isEmpty ()) {
106+ log .info ("No Frames found in the request {}, nothing to do" , transactionId );
107+ return ;
108+ }
109+
110+ for (FrameDetail frameDetail : identityVerificationDto .getFrames ()) {
111+ Optional <MockScene > matchedScene = Objects .requireNonNull (mockUserStory ).getScenes ().stream ()
112+ .filter (scene -> scene .getFrameNumber () == frameDetail .getOrder () &&
113+ scene .getStepCode ().equals (identityVerificationDto .getStepCode ()))
114+ .findFirst ();
115+ log .info ("{} Search match for current frame {} in the story for transaction: {}" , identityVerificationDto .getStepCode (),
116+ frameDetail .getOrder (), transactionId );
117+ if (matchedScene .isPresent ()) {
118+ log .info ("Match found in the story : {} for transaction: {}" , matchedScene .get (), transactionId );
119+ identityVerificationResult .setStep (matchedScene .get ().getStep ());
120+ identityVerificationResult .setFeedback (matchedScene .get ().getFeedback ());
121+ publishAnalysisResult (identityVerificationResult );
122+ }
123+ }
124+ }
125+
126+ @ Override
127+ public VerificationResult getVerificationResult (String transactionId ) throws IdentityVerifierException {
128+ MockUserStory mockUserStory = getResource (configServerUrl +storyName , MockUserStory .class );
129+ if (mockUserStory != null && mockUserStory .getVerificationResult () != null ) {
130+ try {
131+ return objectMapper .treeToValue (mockUserStory .getVerificationResult (), VerificationResult .class );
132+ } catch (Exception e ) {
133+ log .error ("Failed to parse verified attributes in the mock user story: {}" , storyName , e );
134+ }
135+ }
136+ VerificationResult verificationResult = new VerificationResult ();
137+ verificationResult .setStatus (VerificationStatus .FAILED );
138+ verificationResult .setErrorCode ("mock_verification_failed" );
139+ return verificationResult ;
140+ }
141+
142+ private <T > T getResource (String url , Class <T > clazz ) {
143+ Resource resource = resourceLoader .getResource (url );
144+ try (BufferedReader reader = new BufferedReader (
145+ new InputStreamReader (resource .getInputStream (), StandardCharsets .UTF_8 ))) {
146+ String content = reader .lines ().collect (Collectors .joining ("\n " ));
147+ return objectMapper .readValue (content , clazz );
148+ } catch (IOException e ) {
149+ log .error ("Failed to parse data: {}" , url , e );
150+ }
151+ throw new IdentityVerifierException ("invalid_configuration" );
152+ }
153+
154+ }
0 commit comments