<commit_before>package com.b2international.snowowl.snomed.api.rest.moduledependency;

import static com.b2international.snowowl.snomed.api.rest.CodeSystemRestRequests.createCodeSystem;
import static com.b2international.snowowl.snomed.api.rest.CodeSystemVersionRestRequests.createVersion;
import static com.b2international.snowowl.snomed.api.rest.CodeSystemVersionRestRequests.getNextAvailableEffectiveDateAsString;
import static com.b2international.snowowl.snomed.api.rest.CodeSystemVersionRestRequests.getVersion;
import static com.b2international.snowowl.snomed.api.rest.SnomedComponentRestRequests.createComponent;
import static com.b2international.snowowl.snomed.api.rest.SnomedRestFixtures.createConceptRequestBody;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.api.rest.AbstractSnomedApiTest;
import com.b2international.snowowl.snomed.api.rest.SnomedApiTestConstants;
import com.b2international.snowowl.snomed.api.rest.SnomedComponentType;
import com.b2international.snowowl.snomed.api.rest.SnomedRestFixtures;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.CharacteristicType;
import com.b2international.snowowl.snomed.core.domain.SnomedConcepts;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember;
import com.b2international.snowowl.snomed.datastore.SnomedDatastoreActivator;
import com.b2international.snowowl.snomed.datastore.request.SnomedRequests;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType;

public class SnomedModuleDependencyApiTest extends AbstractSnomedApiTest {

	private static final String MODULE_DEPENDECY_REFSET_ID = "900000000000534007";
	private static final String NORWEGIAN_MODULE_CONCEPT_ID = "51000202101";
	private static final String ROOT_CONCEPT = "138875005";

	@Test
	public void testForModuleDependencyMembers() {

		final String shortName = "SNOMEDCT-MODULEDEPENDENCY";
		createCodeSystem(branchPath, shortName).statusCode(201);
		
		// get number of module_dependecy_refset_member

		int sizeBeforeConceptCreation = SnomedRequests.prepareSearchRefSet()
				.all()
				.filterByActive(true)
				.filterByType(SnomedRefSetType.MODULE_DEPENDENCY)
				.setExpand("members()")
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync()
				.getItems().get(0).getMembers().getItems().size();
				

		Map<?, ?> norwegianModuleRequestBody = createConceptRequestBody(Concepts.MODULE_ROOT, NORWEGIAN_MODULE_CONCEPT_ID, SnomedApiTestConstants.UK_PREFERRED_MAP)
				.put("id", NORWEGIAN_MODULE_CONCEPT_ID)
				.put("commitComment", "Created norwegian module concept")
				.build();
		createComponent(branchPath, SnomedComponentType.CONCEPT, norwegianModuleRequestBody).statusCode(201);
		
		Map<?,?> inferredRelationshipRequestBody = SnomedRestFixtures.createRelationshipRequestBody(NORWEGIAN_MODULE_CONCEPT_ID, Concepts.IS_A, Concepts.MODULE_ROOT, NORWEGIAN_MODULE_CONCEPT_ID, CharacteristicType.INFERRED_RELATIONSHIP, 0)
				.put("commitComment", "Created inferred isa from the norwegian rule to SCT_MODULE_CORE")
				.build();
		
		createComponent(branchPath, SnomedComponentType.RELATIONSHIP, inferredRelationshipRequestBody).statusCode(201);
		
		Map<?, ?> norwegianConceptRequestBody = createConceptRequestBody(ROOT_CONCEPT, NORWEGIAN_MODULE_CONCEPT_ID, SnomedApiTestConstants.UK_PREFERRED_MAP)
			.put("commitComment", "Created concept in the norwegian module")
			.build();
		createComponent(branchPath, SnomedComponentType.CONCEPT, norwegianConceptRequestBody);
		
		SnomedConcepts concepts = SnomedRequests.prepareSearchConcept()
				.filterById(NORWEGIAN_MODULE_CONCEPT_ID)
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync();

		concepts.forEach(c -> assertEquals("Effective time must still be null", null, c.getEffectiveTime()));
		int sizeAfterConceptCreation = SnomedRequests.prepareSearchRefSet()
				.all()
				.filterByActive(true)
				.filterById(MODULE_DEPENDECY_REFSET_ID)
				.setExpand("members()")
				.filterByType(SnomedRefSetType.MODULE_DEPENDENCY)
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync()
				.getItems().get(0).getMembers().getItems().size();

		assertEquals("After creating module concept the moduleDependencyRefset shouldn't change", sizeBeforeConceptCreation, sizeAfterConceptCreation);

		// version branch
		final String effectiveDate = getNextAvailableEffectiveDateAsString(SnomedTerminologyComponentConstants.SNOMED_SHORT_NAME);
		final String versionId = "testForModuleDependencyMembers";
		createVersion(shortName, versionId, effectiveDate).statusCode(201);
		getVersion(shortName, versionId).statusCode(200);

		// get number of module_dependecy_refset_member after versioning
		List<SnomedReferenceSetMember> items = SnomedRequests.prepareSearchRefSet()
				.all()
				.filterByActive(true)
				.filterById(MODULE_DEPENDECY_REFSET_ID)
				.filterByType(SnomedRefSetType.MODULE_DEPENDENCY)
				.setExpand("members()")
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync()
				.getItems().get(0).getMembers().getItems();
		
		items.forEach(i -> System.out.println(i.getModuleId()));
		int sizeAfterVersioningAndConceptCreation = items.size();
		
		assertNotEquals("Csocsóa$ztalt megborító megamix", sizeAfterVersioningAndConceptCreation, sizeAfterConceptCreation);

	}

}<commit_msg>[moduledependency] removed sysout, changed prepareSearchRefSet to<commit_after>package com.b2international.snowowl.snomed.api.rest.components;

import static com.b2international.snowowl.snomed.api.rest.CodeSystemRestRequests.createCodeSystem;
import static com.b2international.snowowl.snomed.api.rest.CodeSystemVersionRestRequests.createVersion;
import static com.b2international.snowowl.snomed.api.rest.CodeSystemVersionRestRequests.getNextAvailableEffectiveDateAsString;
import static com.b2international.snowowl.snomed.api.rest.CodeSystemVersionRestRequests.getVersion;
import static com.b2international.snowowl.snomed.api.rest.SnomedComponentRestRequests.createComponent;
import static com.b2international.snowowl.snomed.api.rest.SnomedRestFixtures.createConceptRequestBody;
import static org.junit.Assert.assertEquals;

import java.util.Date;
import java.util.Map;

import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Tuples;
import org.junit.Test;

import com.b2international.snowowl.core.date.DateFormats;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.api.rest.AbstractSnomedApiTest;
import com.b2international.snowowl.snomed.api.rest.SnomedApiTestConstants;
import com.b2international.snowowl.snomed.api.rest.SnomedComponentType;
import com.b2international.snowowl.snomed.api.rest.SnomedRestFixtures;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.CharacteristicType;
import com.b2international.snowowl.snomed.core.domain.SnomedConcepts;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers;
import com.b2international.snowowl.snomed.datastore.SnomedDatastoreActivator;
import com.b2international.snowowl.snomed.datastore.request.SnomedRequests;
import com.google.common.collect.Maps;

public class SnomedModuleDependencyRefsetTest extends AbstractSnomedApiTest {

	private static final String NORWEGIAN_MODULE_CONCEPT_ID = "51000202101";
	

	@Test
	public void moduleDependecyDuplicateMemberTest() {

		final String shortName = "SNOMEDCT-MODULEDEPENDENCY";
		createCodeSystem(branchPath, shortName).statusCode(201);

		Map<Pair<String, String>, Date> moduleToReferencedComponentAndEffectiveDateMap = Maps.newHashMap();

		SnomedReferenceSetMembers moduleDependencyMembers1 = SnomedRequests.prepareSearchMember()
				.all()
				.filterByRefSet(Concepts.REFSET_MODULE_DEPENDENCY_TYPE)
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync();

		moduleDependencyMembers1.getItems().forEach(member -> {
			Pair<String, String> pair = Tuples.pair(member.getModuleId(), member.getReferencedComponent().getId());
			System.err.println(pair + " effective time: " + member.getEffectiveTime());
			moduleToReferencedComponentAndEffectiveDateMap.put(
				pair,
				member.getEffectiveTime()
			);
		});
		
		// create Norwegian module
		Map<?, ?> norwegianModuleRequestBody = createConceptRequestBody(Concepts.MODULE_ROOT, NORWEGIAN_MODULE_CONCEPT_ID, SnomedApiTestConstants.UK_PREFERRED_MAP)
				.put("id", NORWEGIAN_MODULE_CONCEPT_ID)
				.put("commitComment", "Created norwegian module concept")
				.build();
		createComponent(branchPath, SnomedComponentType.CONCEPT, norwegianModuleRequestBody).statusCode(201);
		
		// create both inferred and stated relationships
		Map<?, ?> inferredRelationshipRequestBody = SnomedRestFixtures
				.createRelationshipRequestBody(NORWEGIAN_MODULE_CONCEPT_ID, Concepts.IS_A, Concepts.MODULE_ROOT, NORWEGIAN_MODULE_CONCEPT_ID, CharacteristicType.INFERRED_RELATIONSHIP, 0)
				.put("commitComment", "Created inferred is_a from the norwegian rule to SCT_MODULE_CORE").build();
		
		Map<?, ?> statedRelationshipRequestBody = SnomedRestFixtures
				.createRelationshipRequestBody(NORWEGIAN_MODULE_CONCEPT_ID, Concepts.IS_A, Concepts.MODULE_ROOT, NORWEGIAN_MODULE_CONCEPT_ID, CharacteristicType.STATED_RELATIONSHIP, 0)
				.put("commitComment", "Created state is_a from the norwegian rule to SCT_MODULE_CORE").build();

		createComponent(branchPath, SnomedComponentType.RELATIONSHIP, inferredRelationshipRequestBody).statusCode(201);
		createComponent(branchPath, SnomedComponentType.RELATIONSHIP, statedRelationshipRequestBody).statusCode(201);
		
		SnomedConcepts concepts = SnomedRequests.prepareSearchConcept()
				.filterById(NORWEGIAN_MODULE_CONCEPT_ID)
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync();

		concepts.forEach(c -> assertEquals("Effective time must still be null", null, c.getEffectiveTime()));
				
		// version branch
		final String effectiveDate = getNextAvailableEffectiveDateAsString(SnomedTerminologyComponentConstants.SNOMED_SHORT_NAME);
		final String versionId = "testForModuleDependencyMembers";
		createVersion(shortName, versionId, effectiveDate).statusCode(201);
		getVersion(shortName, versionId).statusCode(200);

		SnomedReferenceSetMembers moduleDependencyMembersAfterVersioning = SnomedRequests.prepareSearchMember()
				.all()
				.filterByRefSet(Concepts.REFSET_MODULE_DEPENDENCY_TYPE)
				.build(SnomedDatastoreActivator.REPOSITORY_UUID, branchPath.getPath())
				.execute(getBus())
				.getSync();
		
		moduleDependencyMembersAfterVersioning.getItems().forEach(member-> {
			final Pair<String, String> pair = Tuples.pair(member.getModuleId(), member.getReferencedComponent().getId());
			final Date originalMemberEffectiveTime = moduleToReferencedComponentAndEffectiveDateMap.get(pair);
			final Date versionDate = EffectiveTimes.parse(effectiveDate, DateFormats.SHORT);
			if(originalMemberEffectiveTime != null) {
				assertEquals("Effective dates on existing module dependency members shouldn't be updated after versioning" , originalMemberEffectiveTime,  member.getEffectiveTime());
				moduleToReferencedComponentAndEffectiveDateMap.remove(pair);
			} else {
				assertEquals("The new members effective time should match the versionDate", versionDate, member.getEffectiveTime());
			}

		});
	}
	
}
{'fraction_non_alphanumeric': 0.0949748743718593, 'fraction_numerical': 0.010804020100502512, 'mean_word_length': 6.955363091272485, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '6039977', 'n_tokens_mistral': 4130, 'n_tokens_neox': 3646, 'n_words': 512}