/*
 * Copyright 2011-2015 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.snomed.mrcm.core.server.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collection;

import org.junit.Test;

import com.b2international.commons.platform.PlatformUtil;
import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.branch.BranchManager;
import com.b2international.snowowl.core.date.Dates;
import com.b2international.snowowl.datastore.BranchPathUtils;
import com.b2international.snowowl.snomed.datastore.MrcmEditingContext;
import com.b2international.snowowl.snomed.datastore.SnomedPredicateBrowser;
import com.b2international.snowowl.snomed.datastore.snor.PredicateIndexEntry;
import com.b2international.snowowl.snomed.mrcm.core.io.MrcmExportFormat;
import com.b2international.snowowl.snomed.mrcm.core.io.MrcmExporter;
import com.b2international.snowowl.snomed.mrcm.core.io.MrcmImporter;
import com.b2international.snowowl.test.commons.Services;

/**
 * @since 4.4
 */
public class MrcmImportExportTest {

	@Test
	public void importTest() throws Exception {
		// default/old MRCM import file contains 58 rules
		final IBranchPath branch = BranchPathUtils.createMainPath();
		final Path path = Paths.get(PlatformUtil.toAbsolutePath(MrcmImportExportTest.class, "mrcm_defaults.xmi"));
		
		try (final InputStream stream = Files.newInputStream(path, StandardOpenOption.READ)) {
			Services.service(MrcmImporter.class).doImport("MAIN", "test", stream);
		} 
		
		
		// verify CDO content
		try (MrcmEditingContext context = new MrcmEditingContext(branch)) {
			assertEquals(58, context.getOrCreateConceptModel().getConstraints().size());
		}
		// verify index
		final Collection<PredicateIndexEntry> allPredicates = ApplicationContext.getServiceForClass(SnomedPredicateBrowser.class).getAllPredicates(branch);
		assertEquals(58, allPredicates.size());
	}
	
	@Test
	public void importBranchTest() throws Exception {
		final Branch childBranch = Services.service(BranchManager.class).getMainBranch().createChild("A");

		// default/old MRCM import file contains 58 rules
		final IBranchPath branchPath = childBranch.branchPath();
		final Path path = Paths.get(PlatformUtil.toAbsolutePath(MrcmImportExportTest.class, "mrcm_defaults.xmi"));
		
		try (final InputStream stream = Files.newInputStream(path, StandardOpenOption.READ)) {
			Services.service(MrcmImporter.class).doImport(childBranch.path(), "test", stream);
		} 
		
		// verify CDO content
		try (MrcmEditingContext context = new MrcmEditingContext(branchPath)) {
			assertEquals(58, context.getOrCreateConceptModel().getConstraints().size());
		}
		// verify index
		final Collection<PredicateIndexEntry> allPredicates = ApplicationContext.getServiceForClass(SnomedPredicateBrowser.class).getAllPredicates(branchPath);
		assertEquals(58, allPredicates.size());
	}
	
	@Test
	public void exportTest() throws Exception {
		importTest();
		
		Path target = Paths.get("target");
		target.toFile().mkdirs();
		Path exportedFile = target.resolve("mrcm_" + Dates.now() + ".xmi");
		assertFalse(exportedFile.toFile().exists());
		try (final OutputStream stream = Files.newOutputStream(exportedFile, StandardOpenOption.CREATE_NEW)) {
			Services.service(MrcmExporter.class).doExport("MAIN", "test", stream, MrcmExportFormat.XMI);
		}
		assertTrue(exportedFile.toFile().exists());
		
		target = Paths.get("target");
		target.toFile().mkdirs();
		exportedFile = target.resolve("mrcm_" + Dates.now() + ".csv");
		assertFalse(exportedFile.toFile().exists());
		try (final OutputStream stream = Files.newOutputStream(exportedFile, StandardOpenOption.CREATE_NEW)) {
			Services.service(MrcmExporter.class).doExport("MAIN", "test", stream, MrcmExportFormat.CSV);
		}
		assertTrue(exportedFile.toFile().exists());
		
	}
	
}

@@ -1236,125 +1236,8 @@
 th;%0A
-import com.b2international.snowowl.core.branch.Branch;%0Aimport com.b2international.snowowl.core.branch.BranchManager;%0A
 impo
@@ -2809,110 +2809,8 @@
 n %7B%0A
-%09%09final Branch childBranch = Services.service(BranchManager.class).getMainBranch().createChild(%22A%22);%0A%0A
 %09%09//
@@ -2887,33 +2887,37 @@
 anch
-Path = childBranch.branch
+ = BranchPathUtils.createMain
 Path
@@ -3174,26 +3174,25 @@
 ort(
-childBranch.path()
+%22MAIN/2002-01-31%22
 , %22t
@@ -3210,24 +3210,27 @@
 m);%0A%09%09%7D %0A%09%09%0A
+%09%09%0A
 %09%09// verify 
@@ -3306,20 +3306,16 @@
 t(branch
-Path
 )) %7B%0A%09%09%09
@@ -3405,32 +3405,32 @@
 // verify index%0A
+
 %09%09final Collecti
@@ -3560,20 +3560,16 @@
 s(branch
-Path
 );%0A%09%09ass

Revert "WRP-2891: Fix failure in MrcmImportExportTest"