View Javadoc

1   package ca.uhn.hl7v2.testpanel.util.compare;
2   
3   import java.util.ArrayList;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Set;
8   import java.util.logging.Logger;
9   
10  import ca.uhn.hl7v2.HL7Exception;
11  import ca.uhn.hl7v2.model.Message;
12  import ca.uhn.hl7v2.parser.PipeParser;
13  import ca.uhn.hl7v2.testpanel.api.IMessageTransformer;
14  import ca.uhn.hl7v2.testpanel.ex.UnexpectedTestFailureException;
15  import ca.uhn.hl7v2.util.Terser;
16  
17  public class BulkHl7V2Comparison {
18  
19  	static final String ACTUAL_DESC = "Actual";
20  	static final String EXPECTED_DESC = "Expected";
21  
22  	private static final Logger ourLog = Logger.getLogger(BulkHl7V2Comparison.class.getName());
23  	
24  	private Iterator<Message> myActualMessages;
25  	private List<IMessageTransformer<Message>> myActualMessageTransformers = new ArrayList<IMessageTransformer<Message>>();
26  	private boolean myCancelled;
27  	private Iterator<Message> myExpectedMessages;
28  	private List<IMessageTransformer<Message>> myExpectedMessageTransformers = new ArrayList<IMessageTransformer<Message>>();
29  	private Set<String> myFieldsToIgnore = new HashSet<String>();
30  	private List<IComparisonListener> myListeners = new ArrayList<IComparisonListener>();
31  	private PipeParser myParser;
32  	private boolean myStopOnFirstFailure;
33  	private int myTotalMessages = -1;
34  	private String myExpectedDesc = EXPECTED_DESC;
35  	private String myActualDesc = ACTUAL_DESC;
36  	private boolean myHaveFailure;
37  
38  	public BulkHl7V2Comparison() {
39  		myParser = PipeParser.getInstanceWithNoValidation();
40  	}
41  
42  	public void addActualMessageTransformer(IMessageTransformer<Message> theTransformer) {
43  		myActualMessageTransformers.add(theTransformer);
44  	}
45  	
46  	public void addExpectedMessageTransformer(IMessageTransformer<Message> theTransformer) {
47  		myExpectedMessageTransformers.add(theTransformer);
48  	}
49  	/**
50  	 * @param theFieldToIgnore
51  	 *            the terserPathsToIgnore to set
52  	 */
53  	public void addFieldToIgnore(String theFieldToIgnore) {
54  		myFieldsToIgnore.add(theFieldToIgnore);
55  	}
56  	
57  	/**
58  	 * Request that the comparison stop running
59  	 */
60  	public void cancel() {
61  		myCancelled = true;
62  	}
63  
64  	public void compare() throws UnexpectedTestFailureException {
65  		myHaveFailure = false;
66  
67  		int actualIndex = 0;
68  		int expectedIndex = 0;
69  		
70  		while (myActualMessages.hasNext() && myExpectedMessages.hasNext() && !myCancelled) {
71  						
72  			Message actualMessage = myActualMessages.next();
73  			Message expectedMessage = myExpectedMessages.next();
74  
75  			for (IMessageTransformer<Message> next : myExpectedMessageTransformers) {
76  				expectedMessage = next.transform(expectedMessage);
77  			}
78  			for (IMessageTransformer<Message> next : myActualMessageTransformers) {
79  				actualMessage = next.transform(actualMessage);
80  			}
81  			
82  			Terser aTerser = new Terser(actualMessage);
83  			Terser eTerser = new Terser(expectedMessage);
84  			
85  			StringBuilder msg = new StringBuilder();
86  			msg.append("Comparing message " + (actualIndex + 1));
87  			if (myTotalMessages != -1) {
88  				msg.append((actualIndex + 1) + "/" + myTotalMessages);
89  			}
90  			try {
91  	            msg.append(" - MSH-10 " + myExpectedDesc + "[" + eTerser.get("/.MSH-10") + "] " + myActualDesc + "[" + aTerser.get("/.MSH-10") + "]");
92              } catch (HL7Exception e) {
93              	// ignore, just for logging
94              }
95              String msgLine = msg.toString();
96  			ourLog.info(msgLine);
97              
98  			for (IComparisonListener next : myListeners) {
99  				next.progressLog(msgLine);
100 			}
101 			
102 			Hl7V2MessageCompare comparison = new Hl7V2MessageCompare(myParser);
103 			comparison.setFieldsToIgnore(myFieldsToIgnore);
104 			comparison.setExpectedAndActualDescription(myExpectedDesc, myActualDesc);
105 			comparison.compare(expectedMessage, actualMessage);
106 			
107 			notyfyListeners(comparison);
108 
109 			if (!comparison.isSame()) {
110 				myHaveFailure = true;
111 				if (myStopOnFirstFailure) {
112 					break;
113 				}
114 			}
115 			
116 			actualIndex++;
117 			expectedIndex++;
118 		}
119 		
120 	}
121 	
122 	
123 	/**
124      * @return the haveFailure
125      */
126     public boolean isHaveFailure() {
127     	return myHaveFailure;
128     }
129 
130 	/**
131 	 * @return the stopOnFirstFailure
132 	 */
133 	public boolean isStopOnFirstFailure() {
134 		return myStopOnFirstFailure;
135 	}
136 
137 	private void notyfyListeners(Hl7V2MessageCompare theCompare) {
138 		if (theCompare.isSame()) {
139 			for (IComparisonListener next : myListeners) {
140 				next.success(theCompare);
141 			}
142 		}else {
143 			for (IComparisonListener next : myListeners) {
144 				next.failure(theCompare);
145 			}
146 		}
147 	}
148 
149 	/**
150 	 * @param theActualMessages
151 	 *            the actualMessages to set
152 	 */
153 	public void setActualMessages(Iterator<Message> theActualMessages) {
154 		myTotalMessages = -1;
155 		myActualMessages = theActualMessages;
156 	}
157 
158 	/**
159 	 * @param theActualMessages
160 	 *            the actualMessages to set
161 	 */
162 	public void setActualMessages(List<Message> theActualMessages) {
163 		myTotalMessages = theActualMessages.size();
164 		myActualMessages = theActualMessages.iterator();
165 	}
166 	
167 	/**
168 	 * @param theExpectedMessages
169 	 *            the expectedMessages to set
170 	 */
171 	public void setExpectedMessages(Iterator<Message> theExpectedMessages) {
172 		myExpectedMessages = theExpectedMessages;
173 	}
174 
175 	/**
176 	 * @param theExpectedMessages
177 	 *            the expectedMessages to set
178 	 */
179 	public void setExpectedMessages(List<Message> theExpectedMessages) {
180 		myExpectedMessages = theExpectedMessages.iterator();
181 	}
182 
183 
184 	/**
185 	 * @param theStopOnFirstFailure
186 	 *            the stopOnFirstFailure to set
187 	 */
188 	public void setStopOnFirstFailure(boolean theStopOnFirstFailure) {
189 		myStopOnFirstFailure = theStopOnFirstFailure;
190 	}
191 
192 	public void addComparisonListener(IComparisonListener theListeners) {
193 		myListeners.add(theListeners);
194 	}
195 
196 	public void setActualAndExpectedDescription(String theString, String theString2) {
197 		// TODO Auto-generated method stub
198 		
199 	}
200 
201 	/**
202 	 * By default the tools will print the words "expected" and "actual" in descriptions
203 	 * but this can be overridden with alternate phrases.
204 	 */
205 	public void setExpectedAndActualDescription(String theExpectedDesc, String theActualDesc) {
206 		myExpectedDesc = theExpectedDesc;
207 		myActualDesc = theActualDesc;
208 	}
209 	
210 }