11package com .cybersource .ws .client ;
22
3- import org .junit .Before ;
43import org .junit .Test ;
54import org .mockito .Mockito ;
65
76import static org .junit .Assert .*;
87
98import java .io .File ;
9+ import java .io .IOException ;
1010import java .io .InputStream ;
1111import java .security .Principal ;
1212import java .security .PrivateKey ;
1313import java .security .cert .X509Certificate ;
1414import java .util .*;
1515
16- public class IdentityTest {
16+ public class IdentityTest {
1717
18- private MerchantConfig config ;
19-
20- @ Before
21- public void setUp () throws Exception {
22- //Loading the properties file from src/test/resources
23- Properties merchantProperties = new Properties ();
24- InputStream in = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("test_cybs.properties" );
25- if (in == null ) {
26- throw new RuntimeException ("Unable to load test_cybs.properties file" );
27- }
28- merchantProperties .load (in );
29- config = new MerchantConfig (merchantProperties , merchantProperties .getProperty ("merchantID" ));
30- }
18+ @ Test
19+ public void testSetUpMerchant () throws SignException , ConfigException {
20+ File p12file = Mockito .mock (File .class );
21+ MerchantConfig mc = Mockito .mock (MerchantConfig .class );
22+
23+ String keyAlias = "CN=" +mc .getMerchantID ()+",SERIALNUMBER=400000009910179089277" ;
24+ X509Certificate x509Cert = Mockito .mock (X509Certificate .class );
25+ Principal principal = Mockito .mock (Principal .class );
26+ PrivateKey pkey = Mockito .mock (PrivateKey .class );
27+ Logger logger = Mockito .mock (Logger .class );
28+ Mockito .when (x509Cert .getSubjectDN ()).thenReturn (principal );
29+ Mockito .when (principal .getName ()).thenReturn (keyAlias );
3130
32- @ Test
33- public void testSetUpMerchant () throws SignException , ConfigException {
34- File p12file = Mockito .mock (File .class );
35- MerchantConfig mc = Mockito .mock (MerchantConfig .class );
36-
37- String keyAlias = "CN=" +mc .getMerchantID ()+",SERIALNUMBER=400000009910179089277" ;
38- X509Certificate x509Cert = Mockito .mock (X509Certificate .class );
39- Principal principal = Mockito .mock (Principal .class );
40- PrivateKey pkey = Mockito .mock (PrivateKey .class );
41- Logger logger = Mockito .mock (Logger .class );
42- Mockito .when (x509Cert .getSubjectDN ()).thenReturn (principal );
43- Mockito .when (principal .getName ()).thenReturn (keyAlias );
44-
45- Mockito .when (mc .getKeyFile ()).thenReturn (p12file );
31+ Mockito .when (mc .getKeyFile ()).thenReturn (p12file );
4632 Mockito .when (mc .getKeyPassword ()).thenReturn ("testPwd" );
47- Identity identity = new Identity (mc ,x509Cert ,pkey ,logger );
48- assertEquals (identity .getName (), mc .getMerchantID ());
49- assertEquals (identity .getSerialNumber (), "400000009910179089277" );
33+ Identity identity = new Identity (mc ,x509Cert ,pkey ,logger );
34+ assertEquals (identity .getName (), mc .getMerchantID ());
35+ assertEquals (identity .getSerialNumber (), "400000009910179089277" );
5036 assertEquals (String .valueOf (identity .getPswd ()), "testPwd" );
51- assertNotNull (identity .getPrivateKey ());
52- }
53-
54- @ Test
55- public void testsetUpServer () throws SignException {
56- String keyAlias ;
57- if (config .isJdkCertEnabled () || config .isCacertEnabled ()) {
58- keyAlias = "SERIALNUMBER=400000009910179089277,CN=CyberSource_SJC_US" ;
59- } else {
60- keyAlias = "CN=CyberSource_SJC_US,SERIALNUMBER=400000009910179089277" ;
61- }
37+ assertNotNull (identity .getPrivateKey ());
38+ }
6239
63- X509Certificate x509Cert = Mockito .mock (X509Certificate .class );
64- Principal principal = Mockito .mock (Principal .class );
65- Logger logger = Mockito .mock (Logger .class );
66- Mockito .when (x509Cert .getSubjectDN ()).thenReturn (principal );
67- Mockito .when (principal .getName ()).thenReturn (keyAlias );
68- Identity identity = new Identity (config ,x509Cert ,logger );
69- assertEquals (identity .getName (), "CyberSource_SJC_US" );
70- assertEquals (identity .getSerialNumber (), "400000009910179089277" );
71- assertNull (identity .getPrivateKey ());
72- }
40+ @ Test
41+ public void testSetUpServerForP12Certs () throws SignException , IOException , ConfigException {
42+ Properties merchantProps = getConfigProps ();
43+ merchantProps .setProperty ("enableCacert" , "false" );
44+ MerchantConfig customConfig = new MerchantConfig (merchantProps , merchantProps .getProperty ("merchantID" ));
45+ String keyAlias = "CN=CyberSource_SJC_US,SERIALNUMBER=400000009910179089277" ;
46+ X509Certificate x509Cert = Mockito .mock (X509Certificate .class );
47+ Principal principal = Mockito .mock (Principal .class );
48+ Mockito .when (x509Cert .getSubjectDN ()).thenReturn (principal );
49+ Mockito .when (principal .getName ()).thenReturn (keyAlias );
50+ Identity identity = new Identity (customConfig , x509Cert );
51+ assertEquals (identity .getName (), Utility .SERVER_ALIAS );
52+ assertEquals (identity .getSerialNumber (), "400000009910179089277" );
53+ assertNull (identity .getPrivateKey ());
54+ }
55+
56+ @ Test
57+ public void testSetUpServerForCaCerts () throws SignException , IOException , ConfigException {
58+ Properties merchantProps = getConfigProps ();
59+ merchantProps .setProperty ("enableCacert" , "true" );
60+ MerchantConfig customConfig = new MerchantConfig (merchantProps , merchantProps .getProperty ("merchantID" ));
61+ String keyAlias = "SERIALNUMBER=400000009910179089277,CN=CyberSource_SJC_US" ;
62+ X509Certificate x509Cert = Mockito .mock (X509Certificate .class );
63+ Principal principal = Mockito .mock (Principal .class );
64+ Mockito .when (x509Cert .getSubjectDN ()).thenReturn (principal );
65+ Mockito .when (principal .getName ()).thenReturn (keyAlias );
66+ Identity identity = new Identity (customConfig , x509Cert );
67+ assertEquals (identity .getName (), Utility .SERVER_ALIAS );
68+ assertEquals (identity .getSerialNumber (), "400000009910179089277" );
69+ assertNull (identity .getPrivateKey ());
70+ }
71+
72+ private Properties getConfigProps () throws IOException {
73+ Properties merchantProperties = new Properties ();
74+ InputStream in = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("test_cybs.properties" );
75+ if (in == null ) {
76+ throw new RuntimeException ("Unable to load test_cybs.properties file" );
77+ }
78+ merchantProperties .load (in );
79+ return merchantProperties ;
80+ }
7381
74- }
82+ }
0 commit comments