All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pronoia.hapi.hamcrest.message.MessageContainsSegment Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
/**
 * 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 ca.uhn.hl7v2.model.Segment;

import com.pronoia.hapi.HapiTestUtil;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;


public class MessageContainsSegment extends TypeSafeDiagnosingMatcher {
    final boolean ignoreMshTimestamp;
    final String exectedSegmentName;
    final String expected;

    public MessageContainsSegment(Segment expected, boolean ignoreMshTimestamp) {
        this.ignoreMshTimestamp = ignoreMshTimestamp;
        this.exectedSegmentName = expected.getName();

        this.expected = ignoreMshTimestamp
            ? HapiTestUtil.stripMshTimestamp(HapiTestUtil.encode(expected))
            : HapiTestUtil.encode(expected);
    }

    public MessageContainsSegment(String expected, boolean ignoreMshTimestamp) {
        this.ignoreMshTimestamp = ignoreMshTimestamp;
        this.exectedSegmentName = HapiTestUtil.extractSegmentName(expected);

        this.expected = ignoreMshTimestamp
            ? HapiTestUtil.stripMshTimestamp(expected)
            : expected;
    }

    @Override
    protected boolean matchesSafely(Message actualMessage, Description mismatchDescription) {
        String actual = HapiTestUtil.encodeSegment(actualMessage, exectedSegmentName);

        if (ignoreMshTimestamp) {
            actual = HapiTestUtil.stripMshTimestamp(actual);
        }

        int indexOfDifference = HapiTestUtil.indexOfDifference(expected, actual);
        if (indexOfDifference >= 0) {
            mismatchDescription.appendText(exectedSegmentName)
                .appendText(" segment = ")
                .appendValue(actual)
                .appendText("\n\tMismatch starting at index ")
                .appendText(Integer.toString(indexOfDifference))
                .appendText(" (zero-based)");
            return false;
        }

        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(exectedSegmentName)
            .appendText(" segment = ")
            .appendValue(expected);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy