/*****************************************************************************
 * Copyright (c) 2017 CEA LIST and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.training.library.tests;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.papyrus.infra.types.core.registries.ElementTypeSetConfigurationRegistry;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * This is a parameterized test to validate all model in training library
 * 
 * @author Benoit Maggi
 */
@SuppressWarnings("nls")
@RunWith(Parameterized.class)
public class ModelValidationTest {

	@Parameters
	public static Collection<Object[]> data() {
		return Arrays.asList(new Object[][] { 
			//{"org.eclipse.papyrus.training.library.newchild/resources/extLibrary.elementtypesconfigurations"}, FIXME 2 warnings due to Periodical weekly with UML Literal		
			{"org.eclipse.papyrus.training.library.newchild/resources/newChild.creationmenumodel"},
			// {"org.eclipse.papyrus.training.library.properties/resources/extlibrary/extlibrary.ctx"},// FIXME missing org.eclipse.papyrus.customization.properties.model.xwt;bundle-version="[1.1.0,2.0.0)" dependency in oxygen
			{"org.eclipse.papyrus.training.library.palette/resources/library.paletteconfiguration"},
			{"org.eclipse.papyrus.training.library.palette/resources/extlibrarydi.elementtypesconfigurations"},
			// {"org.eclipse.papyrus.training.library.architecture/resources/book.nattableconfiguration"}, FIXME also invalid in SysML 1.4
			{"org.eclipse.papyrus.training.library.architecture/resources/library.architecture"},
		});
	}

	private String modelPath; 
	    
	public ModelValidationTest(String modelPath) {
		this.modelPath = modelPath;
	}

	@BeforeClass
	public static void loadElementTypeSet(){
		ElementTypeSetConfigurationRegistry.getInstance();
	}	
	
	/**
	 * Validate the model with the rules defined in the meta-model tooling
	 */
	@Test
	public void validateModel() {
		URI modelPlatformURI = URI.createPlatformPluginURI(this.modelPath, true);
		Resource resource = new ResourceSetImpl().getResource(modelPlatformURI, true);
		Diagnostic diagnostic = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
		Assert.assertEquals("The "+modelPath+" model is invalid "+print(diagnostic), Diagnostic.OK, diagnostic.getSeverity());
	}

	// FIXME : Something should exist in API to do that
	private String print(Diagnostic diagnostic) {
		List<Diagnostic> children = diagnostic.getChildren();
		StringBuilder stringBuilder = new StringBuilder(diagnostic.getMessage());
		for (Diagnostic diagnosticChildren : children) {
			stringBuilder.append("\n"); //$NON-NLS-1$
			stringBuilder.append(diagnosticChildren.getMessage());
		}
		return stringBuilder.toString();
	}	
	
}

@@ -1623,24 +1623,26 @@
 urations%22%7D, 
+//
 FIXME 2 warn
@@ -1690,16 +1690,16 @@
 teral%09%09%0A
-
 %09%09%09%7B%22org
@@ -1780,27 +1780,24 @@
 model%22%7D,%0A%09%09%09
-// 
 %7B%22org.eclips
@@ -1876,131 +1876,8 @@
 x%22%7D,
-// FIXME missing org.eclipse.papyrus.customization.properties.model.xwt;bundle-version=%22%5B1.1.0,2.0.0)%22 dependency in oxygen
 %0A%09%09%09
@@ -2071,19 +2071,16 @@
 s%22%7D,%0A%09%09%09
-// 
 %7B%22org.ec
@@ -2166,40 +2166,8 @@
 n%22%7D,
- FIXME also invalid in SysML 1.4
 %0A%09%09%09

Valide more model