Skip to content

Commit 5b99e94

Browse files
committed
CYBS-737: Modified unit test case
1 parent fa7838e commit 5b99e94

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

java/src/main/java/com/cybersource/ws/client/Identity.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ public class Identity {
3939

4040
private long lastModifiedDate;
4141

42-
private static final String SERVER_ALIAS = "CyberSource_SJC_US";
43-
44-
private static final String CYBS_CERT_AUTH = "CyberSourceCertAuth";
45-
4642
private char[] pswd;
4743

4844
/**
@@ -69,10 +65,10 @@ private void setupJdkServerCerts() throws SignException {
6965
String subjectDN = x509Cert.getSubjectDN().getName();
7066
if (subjectDN != null) {
7167
String[] subjectDNArray = subjectDN.split("SERIALNUMBER=");
72-
if (subjectDNArray.length == 1 && subjectDNArray[0].toLowerCase().contains(CYBS_CERT_AUTH.toLowerCase())){
68+
if (subjectDNArray.length == 1 && subjectDNArray[0].toLowerCase().contains(Utility.CYBS_CERT_AUTH.toLowerCase())){
7369
name = keyAlias = subjectDNArray[0].split("=")[1];
7470
}
75-
else if (subjectDNArray.length == 2 && subjectDNArray[1].toLowerCase().contains(SERVER_ALIAS.toLowerCase())) {
71+
else if (subjectDNArray.length == 2 && subjectDNArray[1].toLowerCase().contains(Utility.SERVER_ALIAS.toLowerCase())) {
7672
name = subjectDNArray[1].split("=")[1];
7773
serialNumber = subjectDNArray[1].split(",")[0];
7874
keyAlias = "serialNumber=" + serialNumber + ",CN=" + name;
@@ -150,10 +146,10 @@ private void setUpServer() throws SignException {
150146
String subjectDN = x509Cert.getSubjectDN().getName();
151147
if (subjectDN != null) {
152148
String[] subjectDNArray = subjectDN.split("SERIALNUMBER=");
153-
if (subjectDNArray.length == 1 && subjectDNArray[0].toLowerCase().contains(CYBS_CERT_AUTH.toLowerCase())){
149+
if (subjectDNArray.length == 1 && subjectDNArray[0].toLowerCase().contains(Utility.CYBS_CERT_AUTH.toLowerCase())){
154150
name = keyAlias = subjectDNArray[0].split("=")[1];
155151
}
156-
else if (subjectDNArray.length == 2 && subjectDNArray[0].toLowerCase().contains(SERVER_ALIAS.toLowerCase())) {
152+
else if (subjectDNArray.length == 2 && subjectDNArray[0].toLowerCase().contains(Utility.SERVER_ALIAS.toLowerCase())) {
157153
String subjectDName = subjectDNArray[0].split("=")[1];
158154
name = subjectDName.substring(0, subjectDName.length()-1);
159155
serialNumber = subjectDNArray[1];

java/src/main/java/com/cybersource/ws/client/SecurityUtil.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.security.cert.X509Certificate;
1919
import java.util.Collections;
2020
import java.util.Enumeration;
21+
import java.util.Map;
2122
import java.util.concurrent.ConcurrentHashMap;
2223

2324
/**
@@ -28,7 +29,6 @@ public class SecurityUtil {
2829

2930
private static final String KEY_FILE_TYPE = "PKCS12";
3031

31-
private static final String SERVER_ALIAS = "CyberSource_SJC_US";
3232
private static final String FAILED_TO_LOAD_KEY_STORE = "Exception while loading KeyStore";
3333
private static final String FAILED_TO_OBTAIN_PRIVATE_KEY = "Exception while obtaining private key from KeyStore with alias";
3434

@@ -202,7 +202,7 @@ public static Document handleMessageCreation(Document signedDoc, String merchant
202202
WSSecEncrypt encrBuilder = new WSSecEncrypt();
203203
//Set the user name to get the encryption certificate.
204204
//The public key of this certificate is used, thus no password necessary. The user name is a keystore alias usually.
205-
String serverAlias = getServerAlias();
205+
String serverAlias = getServerAlias(identities);
206206
encrBuilder.setUserInfo(identities.get(serverAlias).getKeyAlias());
207207
/*This is to reference a public key or certificate when signing or encrypting a SOAP message.
208208
*The following valid values for these configuration items are:
@@ -382,7 +382,7 @@ private static void loadJavaKeystore(MerchantConfig merchantConfig, Logger logge
382382
identities.put(identity.getName(), identity);
383383
}
384384
}
385-
java.security.cert.Certificate serverCert = keystore.getCertificate(getServerAlias());
385+
java.security.cert.Certificate serverCert = keystore.getCertificate(getServerAlias(identities));
386386
if (serverCert == null) {
387387
throw new SignException("Missing Server Certificate ");
388388
}
@@ -420,15 +420,15 @@ private static void loadJavaKeystore(MerchantConfig merchantConfig, Logger logge
420420

421421
}
422422

423-
protected static String getServerAlias() {
424-
String serverAlias = SERVER_ALIAS;
425-
if(!identities.containsKey(serverAlias)) {
426-
if(identities.containsKey(serverAlias.toLowerCase())) {
423+
protected static String getServerAlias(Map<String, Identity> identitiesMapper) {
424+
String serverAlias = Utility.SERVER_ALIAS;
425+
if(!identitiesMapper.containsKey(serverAlias)) {
426+
if(identitiesMapper.containsKey(serverAlias.toLowerCase())) {
427427
serverAlias = serverAlias.toLowerCase();
428-
} else if(identities.containsKey(serverAlias.toUpperCase())) {
428+
} else if(identitiesMapper.containsKey(serverAlias.toUpperCase())) {
429429
serverAlias = serverAlias.toUpperCase();
430430
} else {
431-
for(String identityKey :identities.keySet()) {
431+
for(String identityKey :identitiesMapper.keySet()) {
432432
if(identityKey.equalsIgnoreCase(serverAlias)) {
433433
serverAlias = identityKey;
434434
break;

java/src/main/java/com/cybersource/ws/client/Utility.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ private Utility() {
4646
* Version number of this release.
4747
*/
4848
public static final String VERSION = "6.2.12";
49+
public static final String SERVER_ALIAS = "CyberSource_SJC_US";
50+
public static final String CYBS_CERT_AUTH = "CyberSourceCertAuth";
4951
public static final String ORIGIN_TIMESTAMP = "v-c-client-iat";
5052
public static final String SDK_ELAPSED_TIMESTAMP = "v-c-client-computetime";
5153
public static final String RESPONSE_TIME_REPLY = "v-c-response-time";

java/src/test/java/com/cybersource/ws/client/SecurityUtilSystemTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
package com.cybersource.ws.client;
2020

2121
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertFalse;
23-
import static org.junit.Assert.assertTrue;
24-
import static org.mockito.Mockito.times;
22+
import static org.mockito.Mockito.times;
2523
import static org.mockito.Mockito.verify;
2624

2725
import org.junit.Assert;
@@ -88,7 +86,7 @@ public void setup() throws Exception{
8886
requestMap.put("purchaseTotals_currency", "USD");
8987
requestMap.put("item_0_unitPrice", "12.34");
9088
requestMap.put("item_1_unitPrice", "56.78");
91-
requestMap.put("merchant_id", "smccfep");
89+
requestMap.put("merchant_id", "<merchantID>");
9290

9391
//Loading the properties file from src/test/resources
9492
merchantProperties = new Properties();
@@ -206,9 +204,20 @@ public void testCertificateCacheEnabled() throws Exception{
206204
}
207205

208206
@Test
209-
// change expected result as per the P12 cert you are using for testing
210207
public void testServerAlias() {
211-
Assert.assertEquals("CyberSource_SJC_US".toLowerCase(), SecurityUtil.getServerAlias());
208+
Map<String, Identity> customIdentities = new HashMap<String, Identity>();
209+
Identity identity = Mockito.mock(Identity.class);
210+
customIdentities.put(Utility.SERVER_ALIAS, identity);
211+
Assert.assertEquals(Utility.SERVER_ALIAS, SecurityUtil.getServerAlias(customIdentities));
212+
customIdentities.clear();
213+
customIdentities.put(Utility.SERVER_ALIAS.toLowerCase(), identity);
214+
Assert.assertEquals(Utility.SERVER_ALIAS.toLowerCase(), SecurityUtil.getServerAlias(customIdentities));
215+
customIdentities.clear();
216+
customIdentities.put(Utility.SERVER_ALIAS.toUpperCase(), identity);
217+
Assert.assertEquals(Utility.SERVER_ALIAS.toUpperCase(), SecurityUtil.getServerAlias(customIdentities));
218+
customIdentities.clear();
219+
customIdentities.put("CYBERSOURCE_Sjc_Us", identity);
220+
Assert.assertEquals("CYBERSOURCE_Sjc_Us", SecurityUtil.getServerAlias(customIdentities));
212221
}
213222

214223
private static PrivateKey instPrivateKey() throws Exception{

0 commit comments

Comments
 (0)