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

com.pronoia.hapi.junit.HapiTerserAssert 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.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.util.Terser;

import static com.pronoia.hapi.HapiTestUtil.createGenericHapiContext;
import static org.junit.Assert.assertEquals;


/**
 * Assertions for HAPI Message components identified using a Terser Specification.
 */
public final class HapiTerserAssert {
    static final String ASSERTION_MESSAGE_FORMAT = "HAPI Terser Specification '%s' comparison failed";

    static DefaultHapiContext genericHapiContext = createGenericHapiContext();

    static boolean useGenericMessage;

    private HapiTerserAssert() {
    }

    public static boolean isUseGenericMessage() {
        return useGenericMessage;
    }

    public static void setUseGenericMessage(boolean useGenericMessage) {
        HapiTerserAssert.useGenericMessage = useGenericMessage;
    }

    public static void assertTerserEquals(final String spec, Terser expected, Terser actual) throws HL7Exception {
        if (isUseGenericMessage()) {
            Message expectedMessage = expected.getSegment("MSH").getMessage();
            Message actualMessage = actual.getSegment("MSH").getMessage();

            expected = new Terser(genericHapiContext.getPipeParser().parse(expectedMessage.encode()));
            actual = new Terser(genericHapiContext.getPipeParser().parse(actualMessage.encode()));
        }

        assertEquals(String.format(ASSERTION_MESSAGE_FORMAT, spec), expected.get(spec), actual.get(spec));
    }

    public static void assertTerserEquals(final String spec, final String expected, Terser actual) throws HL7Exception {
        if (isUseGenericMessage()) {
            Message actualMessage = actual.getSegment("MSH").getMessage();

            actual = new Terser(genericHapiContext.getPipeParser().parse(actualMessage.encode()));
        }

        assertEquals(String.format(ASSERTION_MESSAGE_FORMAT, spec), expected, actual.get(spec));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy