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

com.pronoia.hapi.junit.HapiSegmentAssert 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.junit;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Segment;

import com.pronoia.hapi.HapiTestUtil;

import static com.pronoia.hapi.HapiTestUtil.encode;
import static com.pronoia.hapi.HapiTestUtil.encodeSegment;
import static com.pronoia.hapi.HapiTestUtil.findSegment;
import static com.pronoia.hapi.HapiTestUtil.stripMshTimestamp;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;

import static org.junit.Assert.assertEquals;


/**
 * Assertions for HAPI Segments.
 */
public final class HapiSegmentAssert {
    static boolean ignoreMshTimestamp;

    private HapiSegmentAssert() {
    }

    public static boolean isIgnoreMshTimestamp() {
        return ignoreMshTimestamp;
    }

    public static void setIgnoreMshTimestamp(boolean ignoreMshTimestamp) {
        HapiSegmentAssert.ignoreMshTimestamp = ignoreMshTimestamp;
    }

    public static void assertSegmentEquals(final Segment expected, final Message actual) throws HL7Exception {
        String expectedSegmentName = expected.getName();

        assertSegmentStringsEqual(expectedSegmentName, encode(expected), encode(findSegment(expectedSegmentName, actual)));
    }

    public static void assertSegmentEquals(final Segment expected, final Segment actual) throws HL7Exception {
        assertSegmentStringsEqual(expected.getName(), encode(expected), encode(actual));
    }

    public static void assertSegmentEquals(String expected, final Message actual) throws HL7Exception {
        String expectedSegmentName = HapiTestUtil.extractSegmentName(expected);

        assertSegmentStringsEqual(expectedSegmentName, expected, encode(findSegment(expectedSegmentName, actual)));
    }

    public static void assertSegmentEquals(String expected, final Segment actual) throws HL7Exception {
        assertSegmentStringsEqual(HapiTestUtil.extractSegmentName(expected), expected, encode(actual));
    }


    public static void assertSegmentStartsWith(String expected, final Message actual) throws HL7Exception {
        String expectedSegmentName = HapiTestUtil.extractSegmentName(expected);

        assertThat("Start of Segment '" + expectedSegmentName + "' not equal", encodeSegment(actual, expectedSegmentName), startsWith(expected));
    }

    public static void assertSegmentStartsWith(String expected, final Segment actual) throws HL7Exception {
        String expectedSegmentName = HapiTestUtil.extractSegmentName(expected);

        assertThat("Start of Segment '" + expectedSegmentName + "' not equal", encode(actual), startsWith(expected));
    }

    public static void assertSegmentEndsWith(String expected, final Segment actual) throws HL7Exception {
        String expectedSegmentName = HapiTestUtil.extractSegmentName(expected);

        assertThat("End of Segment '" + expectedSegmentName + "' not equal", encode(actual), endsWith(expected));
    }

    public static void assertSegmentContains(String expected, final Segment actual) throws HL7Exception {
        String expectedSegmentName = HapiTestUtil.extractSegmentName(expected);

        assertThat("Segment '" + expectedSegmentName + "' does not contain", encode(actual), containsString(expected));
    }

    public static void assertSegmentStringsEqual(String segmentName, String expected, String actual) {
        if (isIgnoreMshTimestamp() && "MSH".equals(segmentName)) {
            expected = stripMshTimestamp(expected);
            actual = stripMshTimestamp(actual);
            assertEquals("MSH Segment excluding timestamp not equal", expected, actual);
        } else {
            assertEquals("Segment '" + segmentName + "' not equal", expected, actual);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy