001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.shipment.thirdparty.usps;
019:
020: import java.util.List;
021: import java.util.Map;
022: import java.util.HashMap;
023:
024: import junit.framework.TestCase;
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.base.util.UtilMisc;
027: import org.ofbiz.entity.GenericDelegator;
028: import org.ofbiz.service.GenericDispatcher;
029: import org.ofbiz.service.LocalDispatcher;
030: import org.ofbiz.service.ModelService;
031:
032: /**
033: * Tests for USPS Webtools API services
034: *
035: * These were created for simple validation only.
036: */
037: public class UspsServicesTests extends TestCase {
038:
039: public static String module = UspsServicesTests.class.getName();
040:
041: public static final String DELEGATOR_NAME = "test";
042: public static final String DISPATCHER_NAME = "test-dispatcher";
043:
044: private GenericDelegator delegator = null;
045: private LocalDispatcher dispatcher = null;
046:
047: public UspsServicesTests(String name) {
048: super (name);
049: }
050:
051: protected void setUp() throws Exception {
052: delegator = GenericDelegator
053: .getGenericDelegator(DELEGATOR_NAME);
054: dispatcher = GenericDispatcher.getLocalDispatcher(
055: DISPATCHER_NAME, delegator);
056: }
057:
058: protected void tearDown() throws Exception {
059: dispatcher.deregister();
060: }
061:
062: public void testUspsTrackConfirm() throws Exception {
063:
064: // run the service
065: Map result = dispatcher.runSync("uspsTrackConfirm", UtilMisc
066: .toMap("trackingId", "EJ958083578US"));
067:
068: // verify the results
069: String responseMessage = (String) result
070: .get(ModelService.RESPONSE_MESSAGE);
071: Debug.log("[testUspsTrackConfirm] responseMessage: "
072: + responseMessage, module);
073: assertEquals("Service result is success",
074: ModelService.RESPOND_SUCCESS, responseMessage);
075:
076: String trackingSummary = (String) result.get("trackingSummary");
077: Debug.log("[testUspsTrackConfirm] trackingSummary: "
078: + trackingSummary, module);
079: assertEquals(
080: "trackingSummary is correct",
081: "Your item was delivered at 8:10 am on June 1 in Wilmington DE 19801.",
082: trackingSummary);
083:
084: List trackingDetailList = (List) result
085: .get("trackingDetailList");
086: assertEquals("trackingDetailList has 3 elements", 3,
087: trackingDetailList.size());
088:
089: Debug.log("[testUspsTrackConfirm] trackingDetailList[0]: "
090: + trackingDetailList.get(0), module);
091: assertEquals("trackingDetailList element 0 is correct",
092: "May 30 11:07 am NOTICE LEFT WILMINGTON DE 19801.",
093: trackingDetailList.get(0));
094:
095: Debug.log("[testUspsTrackConfirm] trackingDetailList[1]: "
096: + trackingDetailList.get(1), module);
097: assertEquals("trackingDetailList element 0 is correct",
098: "May 30 10:08 am ARRIVAL AT UNIT WILMINGTON DE 19850.",
099: trackingDetailList.get(1));
100:
101: Debug.log("[testUspsTrackConfirm] trackingDetailList[2]: "
102: + trackingDetailList.get(2), module);
103: assertEquals("trackingDetailList element 0 is correct",
104: "May 29 9:55 am ACCEPT OR PICKUP EDGEWATER NJ 07020.",
105: trackingDetailList.get(2));
106: }
107:
108: public void testUspsAddressValidation() throws Exception {
109:
110: // run the service
111: Map result = dispatcher.runSync("uspsAddressValidation",
112: UtilMisc.toMap("address1", "6406 Ivy Lane", "city",
113: "Greenbelt", "state", "MD"));
114:
115: // verify the results
116: String responseMessage = (String) result
117: .get(ModelService.RESPONSE_MESSAGE);
118: Debug.log("[testUspsAddressValidation] responseMessage: "
119: + responseMessage, module);
120: assertEquals("Service result is success",
121: ModelService.RESPOND_SUCCESS, responseMessage);
122:
123: String address1 = (String) result.get("address1");
124: Debug.log("[testUspsAddressValidation] address1: " + address1,
125: module);
126: assertEquals("address1 is correct", "6406 IVY LN", address1);
127:
128: String city = (String) result.get("city");
129: Debug.log("[testUspsAddressValidation] city: " + city, module);
130: assertEquals("city is correct", "GREENBELT", city);
131:
132: String state = (String) result.get("state");
133: Debug
134: .log("[testUspsAddressValidation] state: " + state,
135: module);
136: assertEquals("state is correct", "MD", state);
137:
138: String zip5 = (String) result.get("zip5");
139: Debug.log("[testUspsAddressValidation] zip5: " + zip5, module);
140: assertEquals("zip5 is correct", "20770", zip5);
141:
142: String zip4 = (String) result.get("zip4");
143: Debug.log("[testUspsAddressValidation] zip4: " + zip4, module);
144: assertEquals("zip4 is correct", "1440", zip4);
145: }
146:
147: public void testUspsCityStateLookup() throws Exception {
148:
149: // run the service
150: Map result = dispatcher.runSync("uspsCityStateLookup", UtilMisc
151: .toMap("zip5", "90210"));
152:
153: // verify the results
154: String responseMessage = (String) result
155: .get(ModelService.RESPONSE_MESSAGE);
156: Debug.log("[testUspsCityStateLookup] responseMessage: "
157: + responseMessage, module);
158: assertEquals("Service result is success",
159: ModelService.RESPOND_SUCCESS, responseMessage);
160:
161: String city = (String) result.get("city");
162: Debug.log("[testUspsCityStateLookup] city: " + city, module);
163: assertEquals("city is correct", "BEVERLY HILLS", city);
164:
165: String state = (String) result.get("state");
166: Debug.log("[testUspsCityStateLookup] state: " + state, module);
167: assertEquals("state is correct", "CA", state);
168: }
169:
170: public void testUspsPriorityMailStandard() throws Exception {
171:
172: // run the service
173: Map result = dispatcher
174: .runSync("uspsPriorityMailStandard", UtilMisc.toMap(
175: "originZip", "4", "destinationZip", "4"));
176:
177: // verify the results
178: String responseMessage = (String) result
179: .get(ModelService.RESPONSE_MESSAGE);
180: Debug.log("[testUspsPriorityMailStandard] responseMessage: "
181: + responseMessage, module);
182: assertEquals("Service result is success",
183: ModelService.RESPOND_SUCCESS, responseMessage);
184:
185: String days = (String) result.get("days");
186: Debug.log("[testUspsPriorityMailStandard] days: " + days,
187: module);
188: assertEquals("days is correct", "1", days);
189: }
190:
191: public void testUspsPackageServicesStandard() throws Exception {
192:
193: // run the service
194: Map result = dispatcher
195: .runSync("uspsPackageServicesStandard", UtilMisc.toMap(
196: "originZip", "4", "destinationZip", "4"));
197:
198: // verify the results
199: String responseMessage = (String) result
200: .get(ModelService.RESPONSE_MESSAGE);
201: Debug.log("[testUspsPackageServicesStandard] responseMessage: "
202: + responseMessage, module);
203: assertEquals("Service result is success",
204: ModelService.RESPOND_SUCCESS, responseMessage);
205:
206: String days = (String) result.get("days");
207: Debug.log("[testUspsPackageServicesStandard] days: " + days,
208: module);
209: assertEquals("days is correct", "2", days);
210: }
211:
212: public void testUspsDomesticRate() throws Exception {
213:
214: // prepare the context
215: Map context = new HashMap();
216:
217: context.put("service", "Priority");
218: context.put("originZip", "20770");
219: context.put("destinationZip", "09021");
220: context.put("pounds", "5");
221: context.put("ounces", "1");
222: context.put("container", "None");
223: context.put("size", "Regular");
224: context.put("machinable", "False");
225:
226: // run the service
227: Map result = dispatcher.runSync("uspsDomesticRate", context);
228:
229: // verify the results
230: String responseMessage = (String) result
231: .get(ModelService.RESPONSE_MESSAGE);
232: Debug.log("[testUspsDomesticRate] responseMessage: "
233: + responseMessage, module);
234: assertEquals("Service result is success",
235: ModelService.RESPOND_SUCCESS, responseMessage);
236:
237: String postage = (String) result.get("postage");
238: Debug.log("[testUspsDomesticRate] postage: " + postage, module);
239: assertEquals("postage is correct", "7.90", postage);
240:
241: String restrictionCodes = (String) result
242: .get("restrictionCodes");
243: Debug.log("[testUspsDomesticRate] restrictionCodes: "
244: + restrictionCodes, module);
245: assertEquals("restrictionCodes is correct", "B-B1-C-D-U",
246: restrictionCodes);
247:
248: String restrictionDesc = (String) result.get("restrictionDesc");
249: Debug.log("[testUspsDomesticRate] restrictionDesc: "
250: + restrictionDesc, module);
251: assertEquals("restrictionDesc is correct", "B. Form 2976-A",
252: restrictionDesc.substring(0, 14));
253: }
254: }
|