/*************************************************
* Copyright (C) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*************************************************/
package com.google.checkout.sample.protocol;
import com.google.checkout.schema._2.CouponResult;
import com.google.checkout.schema._2.GiftCertificateResult;
import junit.framework.TestCase;
import org.w3c.dom.Document;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0 beta
*/
public class MerchantCalculationResultBuilderTest extends TestCase {
private String merchantResultByGift = "<?xml version=\"1.0\" " +
"encoding=\"UTF-8\"?><merchant-calculation-results " +
"xmlns=\"http://checkout.google.com/schema/2\">" +
"<results><result address-id=\"address-id\">" +
"<merchant-code-results><gift-certificate-result>" +
"<calculated-amount currency=\"USD\">1.00</calculated-amount>" +
"<code>G100001A</code><message>gift certA</message>" +
"<valid>true</valid></gift-certificate-result>" +
"<gift-certificate-result>" +
"<calculated-amount currency=\"USD\">1.00</calculated-amount>" +
"<code>G10023343B</code><message>gift certB</message>" +
"<valid>true</valid></gift-certificate-result>" +
"</merchant-code-results><shippable>true</shippable>" +
"<shipping-rate currency=\"USD\">0.10</shipping-rate>" +
"<total-tax currency=\"USD\">112.00</total-tax></result></results>" +
"</merchant-calculation-results>";
private String merchantResultByCoupon = "<?xml version=\"1.0\" " +
"encoding=\"UTF-8\"?><merchant-calculation-results " +
"xmlns=\"http://checkout.google.com/schema/2\"><results>" +
"<result address-id=\"address-id\"><merchant-code-results>" +
"<coupon-result><calculated-amount currency=\"USD\">10.00" +
"</calculated-amount><code>C0000111A</code><message>normal coupon" +
"</message><valid>true</valid></coupon-result><coupon-result>" +
"<calculated-amount currency=\"USD\">100.00</calculated-amount>" +
"<code>C123!###334A</code><message>coupon alpha </message>" +
"<valid>false</valid></coupon-result></merchant-code-results>" +
"<shippable>true</shippable><shipping-rate currency=\"USD\">0.12" +
"</shipping-rate><total-tax currency=\"USD\">49.99</total-tax>" +
"</result></results></merchant-calculation-results>";
private String merchantCalResult = "<?xml version=\"1.0\" " +
"encoding=\"UTF-8\"?><merchant-calculation-results " +
"xmlns=\"http://checkout.google.com/schema/2\"><results>" +
"<result address-id=\"address-id\"><merchant-code-results>" +
"<coupon-result><calculated-amount currency=\"USD\">10.00" +
"</calculated-amount><code>C0000111A</code><message>normal coupon" +
"</message><valid>true</valid></coupon-result>" +
"<gift-certificate-result><calculated-amount currency=\"USD\">1.00" +
"</calculated-amount><code>G10023343B</code>" +
"<message>gift certB</message><valid>true</valid>" +
"</gift-certificate-result></merchant-code-results>" +
"<shippable>true</shippable><shipping-rate " +
"currency=\"USD\">0.15</shipping-rate><total-tax " +
"currency=\"USD\">249.99</total-tax></result></results>" +
"</merchant-calculation-results>";
/*
* Test method for 'com.google.checkout.sample.protocol.MerchantCalculationResultBuilder.createCouponResult(boolean, float, String, String)'
*/
public void testCreateCouponResult() throws Exception {
MerchantCalculationResultBuilder builder
= MerchantCalculationResultBuilder.getInstance();
CouponResult result1 = builder.createCouponResult(true, 10.00F,
"C10983488", "coupon 10");
assertEquals("C10983488", result1.getCode());
try {
CouponResult invalidCoupon
= builder.createCouponResult(false, -100.00F, "X1093432222",
"coupon -100");
} catch (ProtocolException protoEx) {
assertContains(protoEx.getMessage(), "negative");
}
}
private boolean assertContains(String givenString, String toLookFor) {
return (givenString.indexOf(toLookFor) >= 0);
}
/*
* Test method for 'com.google.checkout.sample.protocol.MerchantCalculationResultBuilder.createGiftCertResult(boolean, float, String, String)'
*/
public void testCreateGiftCertResult() throws Exception {
MerchantCalculationResultBuilder builder
= MerchantCalculationResultBuilder.getInstance();
GiftCertificateResult gift1 = builder.createGiftCertResult(true, 100F,
"GIFT100", "100$USD value");
assertEquals("GIFT100", gift1.getCode());
try {
GiftCertificateResult invalidGiftCert
= builder.createGiftCertResult(false, -100.00F, "X1093432222",
"negative gift value");
} catch (ProtocolException protoEx) {
assertContains(protoEx.getMessage(), "negative");
}
}
/*
* Test method for 'com.google.checkout.sample.protocol.MerchantCalculationResultBuilder.createMerchantCalResultsByGiftCert(List, float, float, boolean, String)'
*/
public void testCreateMerchantCalResultsByGiftCert() throws Exception {
MerchantCalculationResultBuilder builder
= MerchantCalculationResultBuilder.getInstance();
GiftCertificateResult giftCert1 = builder.createGiftCertResult(true, 1F,
"G100001A", "gift certA");
GiftCertificateResult giftCert2 = builder.createGiftCertResult(true, 1F,
"G10023343B", "gift certB");
List giftList = new ArrayList();
giftList.add(giftCert1);
giftList.add(giftCert2);
Document dom = builder.createMerchantCalResultsByGiftCert(giftList, 112F,
0.1F, true, "address-id");
String merchantCalResultByGiftCertStr = builder.unmarshal(dom);
System.out.println(merchantCalResultByGiftCertStr);
assertEquals(merchantResultByGift, merchantCalResultByGiftCertStr);
}
/*
* Test method for 'com.google.checkout.sample.protocol.MerchantCalculationResultBuilder.createMerchantCalResultsByCoupon(List, float, float, boolean, String)'
*/
public void testCreateMerchantCalResultsByCoupon() throws Exception {
MerchantCalculationResultBuilder builder
= MerchantCalculationResultBuilder.getInstance();
CouponResult coupon1 = builder.createCouponResult(true, 10.00F,
"C0000111A", "normal coupon");
CouponResult coupon2 = builder.createCouponResult(false, 100.00F,
"C123!###334A", "coupon alpha ");
List couponList = new ArrayList();
couponList.add(coupon1);
couponList.add(coupon2);
Document dom = builder.createMerchantCalResultsByCoupon(couponList, 49.99F,
0.1222F, true, "address-id");
String merchantCalResultByCouponStr = builder.unmarshal(dom);
assertEquals(merchantResultByCoupon, merchantCalResultByCouponStr);
}
/*
* Test method for 'com.google.checkout.sample.protocol.MerchantCalculationResultBuilder.createMerchantCalResults(List, List, float, float, boolean, String)'
*/
public void testCreateMerchantCalResults() throws Exception {
MerchantCalculationResultBuilder builder
= MerchantCalculationResultBuilder.getInstance();
CouponResult coupon = builder.createCouponResult(true, 10.00F,
"C0000111A", "normal coupon");
GiftCertificateResult giftCert = builder.createGiftCertResult(true, 1F,
"G10023343B", "gift certB");
List couponList = new ArrayList();
couponList.add(coupon);
List giftList = new ArrayList();
giftList.add(giftCert);
Document dom = builder.createMerchantCalResults(couponList, giftList,
249.99F, 0.15F, true, "address-id");
String merchantCalResultStr = builder.unmarshal(dom);
assertEquals(merchantCalResult, merchantCalResultStr);
}
}