<commit_before>/*
 * Copyright 2011-2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.snowowl.validation.snomed;

import static com.b2international.snowowl.test.commons.snomed.RandomSnomedIdentiferGenerator.generateDescriptionId;

import java.util.*;

import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.serializer.ISerializer;
import org.eclipse.xtext.validation.IResourceValidator;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import com.b2international.snomed.ecl.EclStandaloneSetup;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.internal.validation.ValidationConfiguration;
import com.b2international.snowowl.core.validation.ValidateRequestBuilder;
import com.b2international.snowowl.snomed.common.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.constraint.HierarchyInclusionType;
import com.b2international.snowowl.snomed.core.ecl.DefaultEclParser;
import com.b2international.snowowl.snomed.core.ecl.DefaultEclSerializer;
import com.b2international.snowowl.snomed.core.ecl.EclParser;
import com.b2international.snowowl.snomed.core.ecl.EclSerializer;
import com.b2international.snowowl.snomed.datastore.index.constraint.*;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry;
import com.b2international.snowowl.test.commons.snomed.DocumentBuilders;
import com.b2international.snowowl.test.commons.snomed.TestBranchContext.Builder;
import com.b2international.snowowl.test.commons.validation.BaseValidationTest;
import com.google.inject.Injector;

/**
 * 
 * @since 6.4
 */
public abstract class BaseGenericValidationRuleTest extends BaseValidationTest {

	private static final Injector ECL_INJECTOR = new EclStandaloneSetup().createInjectorAndDoEMFRegistration();
	
	protected static final String CODESYSTEM = "SNOMEDCT";
	
	private static final String ATTRIBUTE = "246061005";
	private static final Long ATTRIBUTEL = Long.parseLong(ATTRIBUTE);
	private static final Long ROOT_CONCEPTL = Long.parseLong(Concepts.ROOT_CONCEPT);
	private static final long CASE_SIGNIFICANCEL = Long.parseLong(Concepts.CASE_SIGNIFICANCE_ROOT_CONCEPT);
	private static final long DESCRIPTION_TYPEL = Long.parseLong(Concepts.DESCRIPTION_TYPE_ROOT_CONCEPT);
	private static final String HISTORICAL_ASSOCIATION = "900000000000522004";
	private static final String EPRESCRIBING_ROUTE_SIMPLE_REFSET = "999000051000001100";
	private static final Long CONCEPT_MODEL_ATTRIBUTEL = Long.parseLong(Concepts.CONCEPT_MODEL_ATTRIBUTE);
	private static final Long REFSET_ROOTL = Long.parseLong(Concepts.REFSET_ROOT_CONCEPT);

	@Parameter
	public long effectiveTime;
	
	@Parameters(name = "EffectiveTime: {0}")
	public static Iterable<? extends Object> data() {
	    return Arrays.asList(EffectiveTimes.UNSET_EFFECTIVE_TIME, 1);
	}
	
	public BaseGenericValidationRuleTest() {
		super("validation-rules-common.json");
	}
	
	@Override
	protected Map<String, String> getTestCodeSystemPathMap() {
		return Map.of(CODESYSTEM, Branch.MAIN_PATH);
	}
	
	@Override
	protected void configureContext(Builder context) {
		context
			.with(EclParser.class, new DefaultEclParser(ECL_INJECTOR.getInstance(IParser.class), ECL_INJECTOR.getInstance(IResourceValidator.class)))
			.with(EclSerializer.class, new DefaultEclSerializer(ECL_INJECTOR.getInstance(ISerializer.class)));
	}
	
	@Override
	protected void configureValidationRequest(ValidateRequestBuilder req) {
		req.setRuleParameters(Map.of(
			ValidationConfiguration.IS_UNPUBLISHED_ONLY, effectiveTime == EffectiveTimes.UNSET_EFFECTIVE_TIME,
			ValidationConfiguration.WORKING_MODULES, List.of(Concepts.UK_DRUG_EXTENSION_MODULE)
		));
	}
	
	@Override
	protected Collection<Class<?>> getAdditionalTypes() {
		return List.of(
			SnomedConceptDocument.class, 
			SnomedConstraintDocument.class, 
			SnomedRelationshipIndexEntry.class, 
			SnomedDescriptionIndexEntry.class,
			SnomedRefSetMemberIndexEntry.class
		);
	}
	
	@Override
	protected void initializeData() {
		// index common required SNOMED CT Concepts
		index()
			.prepareCommit(MAIN)
			.stageNew(concept(Concepts.ROOT_CONCEPT).build())
			// Attributes
			.stageNew(concept(ATTRIBUTE).parents(ROOT_CONCEPTL).build())
			.stageNew(concept(Concepts.IS_A).parents(ATTRIBUTEL).build())
			.stageNew(concept(Concepts.CONCEPT_MODEL_ATTRIBUTE).parents(ATTRIBUTEL).build())<commit_msg>SO-4798: fix rule configuration<commit_after>/*
 * Copyright 2011-2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.snowowl.validation.snomed;

import static com.b2international.snowowl.test.commons.snomed.RandomSnomedIdentiferGenerator.generateDescriptionId;

import java.util.*;

import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.serializer.ISerializer;
import org.eclipse.xtext.validation.IResourceValidator;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import com.b2international.snomed.ecl.EclStandaloneSetup;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.internal.validation.ValidationConfiguration;
import com.b2international.snowowl.core.validation.ValidateRequestBuilder;
import com.b2international.snowowl.snomed.common.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.constraint.HierarchyInclusionType;
import com.b2international.snowowl.snomed.core.ecl.DefaultEclParser;
import com.b2international.snowowl.snomed.core.ecl.DefaultEclSerializer;
import com.b2international.snowowl.snomed.core.ecl.EclParser;
import com.b2international.snowowl.snomed.core.ecl.EclSerializer;
import com.b2international.snowowl.snomed.datastore.index.constraint.*;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry;
import com.b2international.snowowl.test.commons.snomed.DocumentBuilders;
import com.b2international.snowowl.test.commons.snomed.TestBranchContext.Builder;
import com.b2international.snowowl.test.commons.validation.BaseValidationTest;
import com.google.inject.Injector;

/**
 * 
 * @since 6.4
 */
public abstract class BaseGenericValidationRuleTest extends BaseValidationTest {

	private static final Injector ECL_INJECTOR = new EclStandaloneSetup().createInjectorAndDoEMFRegistration();
	
	protected static final String CODESYSTEM = "SNOMEDCT";
	
	private static final String ATTRIBUTE = "246061005";
	private static final Long ATTRIBUTEL = Long.parseLong(ATTRIBUTE);
	private static final Long ROOT_CONCEPTL = Long.parseLong(Concepts.ROOT_CONCEPT);
	private static final long CASE_SIGNIFICANCEL = Long.parseLong(Concepts.CASE_SIGNIFICANCE_ROOT_CONCEPT);
	private static final long DESCRIPTION_TYPEL = Long.parseLong(Concepts.DESCRIPTION_TYPE_ROOT_CONCEPT);
	private static final String HISTORICAL_ASSOCIATION = "900000000000522004";
	private static final String EPRESCRIBING_ROUTE_SIMPLE_REFSET = "999000051000001100";
	private static final Long CONCEPT_MODEL_ATTRIBUTEL = Long.parseLong(Concepts.CONCEPT_MODEL_ATTRIBUTE);
	private static final Long REFSET_ROOTL = Long.parseLong(Concepts.REFSET_ROOT_CONCEPT);

	@Parameter
	public long effectiveTime;
	
	@Parameters(name = "EffectiveTime: {0}")
	public static Iterable<? extends Object> data() {
	    return Arrays.asList(EffectiveTimes.UNSET_EFFECTIVE_TIME, 1);
	}
	
	public BaseGenericValidationRuleTest() {
		super("validation-rules-common.json");
	}
	
	@Override
	protected Map<String, String> getTestCodeSystemPathMap() {
		return Map.of(CODESYSTEM, Branch.MAIN_PATH);
	}
	
	@Override
	protected void configureContext(Builder context) {
		context
			.with(EclParser.class, new DefaultEclParser(ECL_INJECTOR.getInstance(IParser.class), ECL_INJECTOR.getInstance(IResourceValidator.class)))
			.with(EclSerializer.class, new DefaultEclSerializer(ECL_INJECTOR.getInstance(ISerializer.class)));
	}
	
	@Override
	protected void configureValidationRequest(ValidateRequestBuilder req) {
		req.setRuleParameters(Map.of(
			ValidationConfiguration.IS_UNPUBLISHED_ONLY, effectiveTime == EffectiveTimes.UNSET_EFFECTIVE_TIME,
			ValidationConfiguration.WORKING_MODULES, "<<" + Concepts.UK_DRUG_EXTENSION_MODULE
		));
	}
	
	@Override
	protected Collection<Class<?>> getAdditionalTypes() {
		return List.of(
			SnomedConceptDocument.class, 
			SnomedConstraintDocument.class, 
			SnomedRelationshipIndexEntry.class, 
			SnomedDescriptionIndexEntry.class,
			SnomedRefSetMemberIndexEntry.class
		);
	}
	
	@Override
	protected void initializeData() {
		// index common required SNOMED CT Concepts
		index()
			.prepareCommit(MAIN)
			.stageNew(concept(Concepts.ROOT_CONCEPT).build())
			// Attributes
			.stageNew(concept(ATTRIBUTE).parents(ROOT_CONCEPTL).build())
			.stageNew(concept(Concepts.IS_A).parents(ATTRIBUTEL).build())
			.stageNew(concept(Concepts.CONCEPT_MODEL_ATTRIBUTE).parents(ATTRIBUTEL).build())
{'fraction_non_alphanumeric': 0.09075907590759076, 'fraction_numerical': 0.01595159515951595, 'mean_word_length': 6.360998650472335, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '266252', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3351, 'n_words': 600}