
com.pronoia.hapi.hamcrest.message.MessageIsEqual Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hapi-junit Show documentation
Show all versions of hapi-junit Show documentation
A library of JUnit assertions to assist in testing with HAPI messages.
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.pronoia.hapi.hamcrest.message;
import ca.uhn.hl7v2.model.Message;
import com.pronoia.hapi.HapiTestUtil;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import static com.pronoia.hapi.HapiTestUtil.extractMessageType;
import static com.pronoia.hapi.HapiTestUtil.extractSegmentNames;
public class MessageIsEqual extends TypeSafeDiagnosingMatcher {
final boolean ignoreMshTimestamp;
final String[] expected;
int mismatchedSegmentIndex = -1;
public MessageIsEqual(Message expected, boolean ignoreMshTimestamp) {
this.ignoreMshTimestamp = ignoreMshTimestamp;
this.expected = HapiTestUtil.convertToArrayOfSegmentStrings(expected);
if (ignoreMshTimestamp) {
this.expected[0] = HapiTestUtil.stripMshTimestamp(this.expected[0]);
}
}
public MessageIsEqual(String[] expected, boolean ignoreMshTimestamp) {
this.ignoreMshTimestamp = ignoreMshTimestamp;
this.expected = expected;
if (ignoreMshTimestamp) {
this.expected[0] = HapiTestUtil.stripMshTimestamp(this.expected[0]);
}
}
public MessageIsEqual(String expected, boolean ignoreMshTimestamp) {
this.ignoreMshTimestamp = ignoreMshTimestamp;
this.expected = expected.split("\r");
if (ignoreMshTimestamp) {
this.expected[0] = HapiTestUtil.stripMshTimestamp(this.expected[0]);
}
}
@Override
protected boolean matchesSafely(Message actualMessage, Description mismatchDescription) {
String[] actual = HapiTestUtil.convertToArrayOfSegmentStrings(actualMessage);
if (ignoreMshTimestamp) {
actual[0] = HapiTestUtil.stripMshTimestamp(actual[0]);
}
if (expected.length != actual.length) {
String messageType = extractMessageType(actual);
if (messageType != null && !messageType.isEmpty()) {
mismatchDescription.appendText(messageType)
.appendText(" ");
}
mismatchDescription.appendText("Message containing ")
.appendText(actual.length + " Segments: " + extractSegmentNames(actual));
return false;
}
for (int segmentIndex = 0; segmentIndex < expected.length; ++segmentIndex) {
int indexOfDifference = HapiTestUtil.indexOfDifference(expected[segmentIndex], actual[segmentIndex]);
if (indexOfDifference >= 0) {
mismatchedSegmentIndex = segmentIndex;
mismatchDescription.appendText("Segment #")
.appendText(Integer.toString(segmentIndex + 1))
.appendText(" = ")
.appendValue(actual[segmentIndex])
.appendText("\n\tMismatch starting at index ")
.appendText(Integer.toString(indexOfDifference))
.appendText(" (zero-based)");
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
if (mismatchedSegmentIndex >= 0) {
description.appendText("Segment #")
.appendText(Integer.toString(mismatchedSegmentIndex + 1))
.appendText(" = ")
.appendValue(expected[mismatchedSegmentIndex]);
} else {
String messageType = extractMessageType(expected);
if (messageType != null && !messageType.isEmpty()) {
description.appendText(messageType)
.appendText(" ");
}
description.appendText("Message containing ")
.appendText(expected.length + " Segments: " + extractSegmentNames(expected));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy