<commit_before>/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed 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 org.openehealth.ipf.modules.hl7.extend

import ca.uhn.hl7v2.parser.*
import ca.uhn.hl7v2.model.*
import ca.uhn.hl7v2.model.v25.segment.NK1

import org.openehealth.ipf.commons.core.extend.DefaultActivator
import org.openehealth.ipf.commons.map.BidiMappingService
import org.openehealth.ipf.commons.map.extend.MappingExtension
import org.openehealth.ipf.modules.hl7.AckTypeCode
import org.openehealth.ipf.modules.hl7.AbstractHL7v2Exception
import org.openehealth.ipf.modules.hl7.HL7v2Exception

import org.springframework.core.io.ClassPathResource

/**
 * @author <NAME>
 * @author <NAME>
 */
public class HapiModelExtensionTest extends GroovyTestCase {
	
    static def mappingService
	static def defMappingExtension
    static def hl7MappingExtension

    static {
        ExpandoMetaClass.enableGlobally()
    }
    
    void setUp() {
        if (mappingService) return
        mappingService = new BidiMappingService()
        mappingService.setMappingScript(new ClassPathResource("example2.map"))
        defMappingExtension = new MappingExtension()
        hl7MappingExtension = new HapiModelExtension()
        defMappingExtension.mappingService = mappingService
        hl7MappingExtension.mappingService = mappingService
        DefaultActivator activator = new DefaultActivator()
        activator.activate(defMappingExtension)
        activator.activate(hl7MappingExtension)
    }
	
    void testMatches() {
        def msgText = this.class.classLoader.getResource('msg-01.hl7')?.text
        def msg = new GenericParser().parse(msgText)        
    	assert msg.matches('*', '*', '*')
    	assert msg.matches('ADT', 'A01', '*')
    	assert msg.matches('ADT', 'A01', '2.2')
    	assert msg.matches('ADT', 'A02', '*') == false
    	assert msg.matches('ADT', 'A01', '2.3') == false
        msgText = this.class.classLoader.getResource('msg-02.hl7')?.text
        msg = new GenericParser().parse(msgText)        
    	assert msg.matches('*', '*', '*')
    	assert msg.matches('ORU', 'R01', '*')
    	assert msg.matches('ORU', 'R01', '2.4')
    	assert msg.matches('ORU', 'A02', '*') == false
    	assert msg.matches('ORU', 'R01', '2.3') == false<commit_msg>use addMappingScript if not used within Spring application context.<commit_after>/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed 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 org.openehealth.ipf.modules.hl7.extend

import ca.uhn.hl7v2.parser.*
import ca.uhn.hl7v2.model.*
import ca.uhn.hl7v2.model.v25.segment.NK1

import org.openehealth.ipf.commons.core.extend.DefaultActivator
import org.openehealth.ipf.commons.map.BidiMappingService
import org.openehealth.ipf.commons.map.extend.MappingExtension
import org.openehealth.ipf.modules.hl7.AckTypeCode
import org.openehealth.ipf.modules.hl7.AbstractHL7v2Exception
import org.openehealth.ipf.modules.hl7.HL7v2Exception

import org.springframework.core.io.ClassPathResource

/**
 * @author <NAME>
 * @author <NAME>
 */
public class HapiModelExtensionTest extends GroovyTestCase {
	
    static def mappingService
	static def defMappingExtension
    static def hl7MappingExtension

    static {
        ExpandoMetaClass.enableGlobally()
    }
    
    void setUp() {
        if (mappingService) return
        mappingService = new BidiMappingService()
        mappingService.addMappingScript(new ClassPathResource("example2.map"))
        defMappingExtension = new MappingExtension()
        hl7MappingExtension = new HapiModelExtension()
        defMappingExtension.mappingService = mappingService
        hl7MappingExtension.mappingService = mappingService
        DefaultActivator activator = new DefaultActivator()
        activator.activate(defMappingExtension)
        activator.activate(hl7MappingExtension)
    }
	
    void testMatches() {
        def msgText = this.class.classLoader.getResource('msg-01.hl7')?.text
        def msg = new GenericParser().parse(msgText)        
    	assert msg.matches('*', '*', '*')
    	assert msg.matches('ADT', 'A01', '*')
    	assert msg.matches('ADT', 'A01', '2.2')
    	assert msg.matches('ADT', 'A02', '*') == false
    	assert msg.matches('ADT', 'A01', '2.3') == false
        msgText = this.class.classLoader.getResource('msg-02.hl7')?.text
        msg = new GenericParser().parse(msgText)        
    	assert msg.matches('*', '*', '*')
    	assert msg.matches('ORU', 'R01', '*')
    	assert msg.matches('ORU', 'R01', '2.4')
    	assert msg.matches('ORU', 'A02', '*') == false
    	assert msg.matches('ORU', 'R01', '2.3') == false
{'fraction_non_alphanumeric': 0.11591390261115032, 'fraction_numerical': 0.02117148906139732, 'mean_word_length': 3.594003241491086, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6821541', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1672, 'n_words': 415}