summaryrefslogtreecommitdiffstats
path: root/src/org/uic/ticket/api/test/UperEncodeTicketTestV1.java
blob: 9fed854600463e7774628dd9c47d3a6168a5d74f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package org.uic.ticket.api.test;

import static org.junit.Assert.assertEquals;

import java.util.logging.Level;

import net.gcdc.asn1.datatypesimpl.SequenceOfStringUTF8;
import net.gcdc.asn1.uper.UperEncoder;

import org.junit.Test;
import org.uic.ticket.api.asn.omv1.CardReferenceType;
import org.uic.ticket.api.asn.omv1.ControlData;
import org.uic.ticket.api.asn.omv1.CustomerStatusType;
import org.uic.ticket.api.asn.omv1.DocumentData;
import org.uic.ticket.api.asn.omv1.ExtensionData;
import org.uic.ticket.api.asn.omv1.IssuingData;
import org.uic.ticket.api.asn.omv1.OpenTicketData;
import org.uic.ticket.api.asn.omv1.SequenceOfCardReferenceType;
import org.uic.ticket.api.asn.omv1.SequenceOfCustomerStatusType;
import org.uic.ticket.api.asn.omv1.SequenceOfDocumentData;
import org.uic.ticket.api.asn.omv1.SequenceOfExtensionData;
import org.uic.ticket.api.asn.omv1.SequenceOfTicketLinkType;
import org.uic.ticket.api.asn.omv1.SequenceOfTravelerType;
import org.uic.ticket.api.asn.omv1.StationPassageData;
import org.uic.ticket.api.asn.omv1.TicketDetailData;
import org.uic.ticket.api.asn.omv1.TicketLinkType;
import org.uic.ticket.api.asn.omv1.TokenType;
import org.uic.ticket.api.asn.omv1.TravelerData;
import org.uic.ticket.api.asn.omv1.TravelerType;
import org.uic.ticket.api.asn.omv1.UicRailTicketData;


public class UperEncodeTicketTestV1 {

    /**
     * Example from the Standard on UPER.
     <pre>
     value UicRailTicketData ::=  {     
       issuingDetail {
            issuingYear       2018
            issuingDay        1
       	    specimen          TRUE,
            securePaperTicket FALSE,
            activated         TRUE,
            issuerPNR         "issuerTestPNR",
       	    issuedOnLine      12
       }
       ,travelerDetail{
            traveler {
               {
                	firstName      "John"
          	     	,secondName    "Dow"
                	,idCard        "12345"
                	,ticketHolder  TRUE
          			,status        {{customerStatusDescr "senior" }}
               }
            }
           ,groupName "myGroup"
       }
       ,transportDocument {
          	{
	         	 token {tokenProviderIA5 "VDV", token '82DA'H }
	        	,ticket openTicket : {
	        			returnIncluded    FALSE
        				infoText          "openTicketInfo"
	        	 }
	       	}
	      	,{
	        	 ticket stationPassage : {
       					 productName       "passage"
       					,stationNameUTF8   { "Amsterdam" }       
       					,validFromDay      0
       					,numberOfDaysValid 123
 	        	}
	       	}		
       } 
       ,controlDetail {
          identificationByCardReference {
            { trailingCardIdNum 100 }
          }
   	      ,identificationByIdCard	    FALSE
    	  ,identificationByPassportId   FALSE
          ,passportValidationRequired  	FALSE
      	  ,onlineValidationRequired    	FALSE
          ,ageCheckRequired            	FALSE   	
    	  ,reductionCardCheckRequired  	FALSE          
          ,infoText                     "cd"
          ,includedTickets {
          	{ productOwnerIA5 "test" }
          }
       } 
       ,extension {
            { extensionId "1", extensionData '82DA'H }
           ,{ extensionId "2", extensionData '83DA'H }
        }			
     }
     
     
     Encoding to the file 'data.uper' using PER UNALIGNED encoding rule...
UicRailTicketData SEQUENCE [root fieldcount (not encoded) = 5]
  issuingDetail IssuingData SEQUENCE [root fieldcount (not encoded) = 7]
    issuingYear INTEGER [length (not encoded) = 1.0]
      2018
    issuingDay INTEGER [length (not encoded) = 1.1]
      1
    specimen BOOLEAN [length (not encoded) = 0.1]
      TRUE
    securePaperTicket BOOLEAN [length (not encoded) = 0.1]
      FALSE
    activated BOOLEAN [length (not encoded) = 0.1]
      TRUE
    issuerPNR IA5String [length = 13.0]
      "issuerTestPNR"
    issuedOnLine INTEGER [length = 1.0]
      12
  travelerDetail TravelerData SEQUENCE [root fieldcount (not encoded) = 2]
    traveler SEQUENCE OF [count = 1]
      TravelerType SEQUENCE [root fieldcount (not encoded) = 5]
        firstName UTF8String [length = 4.0]
          0x4a6f686e
        secondName UTF8String [length = 3.0]
          0x446f77
        idCard IA5String [length = 5.0]
          "12345"
        ticketHolder BOOLEAN [length (not encoded) = 0.1]
          TRUE
        status SEQUENCE OF [count = 1]
          CustomerStatusType SEQUENCE [fieldcount (not encoded) = 1]
            customerStatusDescr IA5String [length = 6.0]
              "senior"
    groupName UTF8String [length = 7.0]
      0x6d7947726f7570
  transportDocument SEQUENCE OF [count = 2]
    DocumentData SEQUENCE [root fieldcount (not encoded) = 2]
      token TokenType SEQUENCE [fieldcount (not encoded) = 2]
        tokenProviderIA5 IA5String [length = 3.0]
          "VDV"
        token OCTET STRING [length = 2.0]
          0x82da
      ticket CHOICE [index = 2]
        openTicket OpenTicketData SEQUENCE [root fieldcount (not encoded) = 2]
          returnIncluded BOOLEAN [length (not encoded) = 0.1]
            FALSE
          infoText UTF8String [length = 14.0]
            0x6f70656e5469636b6574496e666f
    DocumentData SEQUENCE [root fieldcount (not encoded) = 1]
      ticket CHOICE [index = 9]
        stationPassage StationPassageData SEQUENCE [root fieldcount (not encoded) = 4]
          productName UTF8String [length = 7.0]
            0x70617373616765
          stationNameUTF8 SEQUENCE OF [count = 1]
            UTF8String [length = 9.0]
              0x416d7374657264616d
          validFromDay INTEGER [length (not encoded) = 1.2]
            0
          numberOfDaysValid INTEGER [length = 1.0]
            123
  controlDetail ControlData SEQUENCE [root fieldcount (not encoded) = 9]
    identificationByCardReference SEQUENCE OF [count = 1]
      CardReferenceType SEQUENCE [root fieldcount (not encoded) = 1]
        trailingCardIdNum INTEGER [length = 1.0]
          100
    identificationByIdCard BOOLEAN [length (not encoded) = 0.1]
      FALSE
    identificationByPassportId BOOLEAN [length (not encoded) = 0.1]
      FALSE
    passportValidationRequired BOOLEAN [length (not encoded) = 0.1]
      FALSE
    onlineValidationRequired BOOLEAN [length (not encoded) = 0.1]
      FALSE
    ageCheckRequired BOOLEAN [length (not encoded) = 0.1]
      FALSE
    reductionCardCheckRequired BOOLEAN [length (not encoded) = 0.1]
      FALSE
    infoText UTF8String [length = 2.0]
      0x6364
    includedTickets SEQUENCE OF [count = 1]
      TicketLinkType SEQUENCE [root fieldcount (not encoded) = 1]
        productOwnerIA5 IA5String [length = 4.0]
          "test"
  extension SEQUENCE OF [count = 2]
    ExtensionData SEQUENCE [fieldcount (not encoded) = 2]
      extensionId IA5String [length = 1.0]
        "1"
      extensionData OCTET STRING [length = 2.0]
        0x82da
    ExtensionData SEQUENCE [fieldcount (not encoded) = 2]
      extensionId IA5String [length = 1.0]
        "2"
      extensionData OCTET STRING [length = 2.0]
        0x83da
Total encoded length = 133.7
Encoded successfully in 134 bytes:
78022020 050DD3CF 9F5CBCAA 65E7D284 EA40218A 02D00082 2537B437 01A237BB
82B164CD A3580883 73CBBB4E FE40EDAF 28EE4DEE AE004A03 AD12B014 16D08000
0000100E 6F70656E 5469636B 6574496E 666F1202 2040EE0C 2E6E6C2C ECA02128
2DAE6E8C AE4C8C2D A0080BDA 60100402 C800131B 20082027 4CBCFA01 00B10282
DA016405 07B4

 
			
    </pre>
     */



    @Test public void testEncodeTicket() throws IllegalArgumentException, IllegalAccessException {
    	UicRailTicketData ticket = new UicRailTicketData();
    	populateTicket(ticket);
        byte[] encoded = UperEncoder.encode(ticket);
        String hex = UperEncoder.hexStringFromBytes(encoded);
        UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex));
       
        String expectedHex = "78022020050DD3CF9F5CBCAA65E7D284EA40218A02D000822537B43701A237BB82B164CDA358088373CBBB4EFE40EDAF28EE4DEEAE004A03AD12B01416D080000000100E6F70656E5469636B6574496E666F12022040EE0C2E6E6C2CECA021282DAE6E8CAE4C8C2DA0080BDA60100402C800131B200820274CBCFA0100B10282DA01640507B4"; 
        UperEncoder.logger.debug(String.format("first difference at index: %d",hex.compareTo(expectedHex)));
        UperEncoder.logger.debug(String.format("data returned: %s",hex)); 
        UperEncoder.logger.debug(String.format("data expected: %s",expectedHex)); 
        assertEquals(hex,expectedHex);
    }
    
	@Test public void testDecodeTicket() throws IllegalArgumentException, IllegalAccessException {
    	UicRailTicketData ticket = new UicRailTicketData();
    	populateTicket(ticket);
        byte[] encoded = UperEncoder.encode(ticket);
        String hex = UperEncoder.hexStringFromBytes(encoded);
        UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex));
        String expectedHex = "78022020050DD3CF9F5CBCAA65E7D284EA40218A02D000822537B43701A237BB82B164CDA358088373CBBB4EFE40EDAF28EE4DEEAE004A03AD12B01416D080000000100E6F70656E5469636B6574496E666F12022040EE0C2E6E6C2CECA021282DAE6E8CAE4C8C2DA0080BDA60100402C800131B200820274CBCFA0100B10282DA01640507B4"; 
        assertEquals(hex,expectedHex);
        UicRailTicketData decodedTicket  = UperEncoder.decode(encoded, UicRailTicketData.class);
        assert(decodedTicket != null);
        assertEquals(decodedTicket.controlDetail.ageCheckRequired , false );
        assertEquals(decodedTicket.controlDetail.getInfoText() , "cd" );
        assertEquals(decodedTicket.controlDetail.getIncludedTickets().get(0).getProductOwnerIA5() , "test" );
        assert(decodedTicket.controlDetail.getIdentificationByCardReference().get(0).getTrailingCardIdNum() == 100L );
    }
    
    private void populateTicket(UicRailTicketData ticket) {
    	
    	ticket.setControlDetail(new ControlData());
    	populate(ticket.getControlDetail());
    	
     	
    	ticket.setIssuingDetail(new IssuingData());
    	populateIssuingData(ticket.getIssuingDetail());
    	
    	TravelerData td = new TravelerData();
    	populateTravelerData(td);
    	ticket.setTravelerDetail(td);    	
    	
    	SequenceOfDocumentData ds = new SequenceOfDocumentData();

    	
    	//OpenTicket
    	DocumentData do1 = new DocumentData();
    	addOpenTicketData(do1);
    	ds.add(do1);   	    	
    	
    	//StationPassage
    	DocumentData do2 = new DocumentData();    	
    	addStationPassage(do2);
    	ds.add(do2);   	    	
    	
    	ticket.setTransportDocument(ds);
    	
       	SequenceOfExtensionData ed = new SequenceOfExtensionData();
    	populateExtensionSequence(ed);
    	ticket.setExtension(ed);
    	
	}
    
    /*
	 ticket stationPassage : {
       	productName "passage"
       	,stationNameUTF8 { "Amsterdam" }       
       	,validFromDay 0
       	,numberOfDaysValid 123
 	 }
     */
    private void addStationPassage(DocumentData dd) {
    	TicketDetailData tdd = new TicketDetailData();
    	StationPassageData sp = new StationPassageData();  
    	sp.setProductName("passage");
    	sp.setValidFromDay(0L);
    	sp.setNumberOfDaysValid(123L);
    	SequenceOfStringUTF8 ss = new SequenceOfStringUTF8();
    	ss.add("Amsterdam");
    	sp.setStationNameUTF8(ss);   	
     	tdd.setStationPassage(sp);    	
    	dd.setTicket(tdd);
	}
    
    /*
     	{
	        token {tokenProviderIA5 "VDV", token '82DA'H }
	        ,ticket openTicket : {
	        	returnIncluded FALSE
        		infoText "openTicketInfo"
	        }
	    }
     */
    
	private void addOpenTicketData(DocumentData dd) {
    	TokenType to = new TokenType();
    	to.setTokenProviderIA5("VDV");
    	byte[] ba = { (byte) 0x82, (byte) 0xDA };
    	to.setToken(ba);
    	dd.setToken(to);   		
		
    	TicketDetailData tdd = new TicketDetailData();
    	OpenTicketData otd = new OpenTicketData();  
    	otd.setInfoText("openTicketInfo");
    	otd.setReturnIncluded(false);
    	tdd.setOpenTicket(otd);
    	dd.setTicket(tdd);
	}


	/*
       ,travelerDetail{
            traveler {
               {
                	firstName "John"
          	     	,secondName "Dow"
                	,idCard "12345"
                	,ticketHolder TRUE
          			,status {{customerStatusDescr "senior"  }}
               }
            }
           ,groupName "myGroup"
       }
     */
    
    private void populateTravelerData(TravelerData td) {
    	td.setGroupName("myGroup");
    	SequenceOfTravelerType trs = new SequenceOfTravelerType();
    	TravelerType tr = new TravelerType();
    	tr.setIdCard("12345");
    	tr.setFirstName("John");
    	tr.setSecondName("Dow");
    	tr.setTicketHolder(true);
    	SequenceOfCustomerStatusType ts = new SequenceOfCustomerStatusType();  	
    	CustomerStatusType cst = new CustomerStatusType();
    	cst.setCustomerStatusDescr("senior");
    	ts.add(cst);
    	tr.setStatus(ts);
    	trs.add(tr);
    	td.setTraveler(trs);
	}

	/*
       ,issuingDetail {
            issuingYear       2018
            issuingDay        1
       	    specimen  TRUE,
            securePaperTicket FALSE,
            activated TRUE,
            issuerPNR  "issuerTestPNR",
       	    issuedOnLine  12
       }
     */
    private void populateIssuingData(IssuingData issuingDetail) {
    	issuingDetail.setIssuingYear(2018L);
    	issuingDetail.setIssuingDay(1L);
    	issuingDetail.setIssuerPNR("issuerTestPNR");
    	issuingDetail.setSpecimen(true);
    	issuingDetail.setSecurePaperTicket(false);
    	issuingDetail.setActivated(true);
    	issuingDetail.setIssuedOnLine(12L);	
	}

	/*
       ,extension {
            { extensionId "1", extensionData '82DA'H }
           ,{ extensionId "2", extensionData '83DA'H }
        }
     */
    private void populateExtensionSequence(SequenceOfExtensionData ed) {
       	ExtensionData ed1 = new ExtensionData();
    	ed1.setExtensionId("1");
    	byte[] ba1 = { (byte) 0x82, (byte) 0xDA };
    	ed1.setExtensionData(ba1); 	
    	ExtensionData ed2 = new ExtensionData();    	
    	ed2.setExtensionId("2");
    	byte[] ba2 = { (byte) 0x83, (byte) 0xDA };
    	ed2.setExtensionData(ba2);     	
    	ed.add(ed1);
    	ed.add(ed2);
	}

    /*
       ,controlDetail {
          identificationByCardReference {
            { trailingCardIdNum 100 }
          }
   	      ,identificationByIdCard	    FALSE
    	  ,identificationByPassportId   FALSE
          ,passportValidationRequired  	FALSE
      	  ,onlineValidationRequired    	FALSE
          ,ageCheckRequired            	FALSE   	
    	  ,reductionCardCheckRequired  	FALSE          
          ,infoText "cd"
          ,includedTickets {
          	{ productOwnerIA5 "test" }
          }
       } 
     */
	private void populate(ControlData controlDetail) {
		controlDetail.infoText = "cd";
		controlDetail.setAgeCheckRequired(false);
		controlDetail.setIdentificationByIdCard(false);
		controlDetail.setIdentificationByPassportId(false);
		controlDetail.setOnlineValidationRequired(false);
		controlDetail.setPassportValidationRequired(false);
		controlDetail.setReductionCardCheckRequired(false);
		controlDetail.setIdentificationByCardReference(new SequenceOfCardReferenceType());
		controlDetail.getIdentificationByCardReference().add(populateCardRefrence());
		SequenceOfTicketLinkType sit = new SequenceOfTicketLinkType();
		populateIncludedTickets(sit);
		controlDetail.setIncludedTickets(sit);
	}

	
	/*
	 * 
	 */
	private void populateIncludedTickets(SequenceOfTicketLinkType sequenceOfTicketLinkType) {
		TicketLinkType tlt = new TicketLinkType();
		tlt.productOwnerIA5="test";
		sequenceOfTicketLinkType.add(tlt);
	}

	/*
	 {
	   trailingCardIdNum 100
	 }
	 */
	private CardReferenceType populateCardRefrence() {
		CardReferenceType cr = new CardReferenceType();
		cr.setTrailingCardIdNum(100L);
		return cr;
	}



    
    
    
    

}