Commit 373038a0 authored by Romain Bioteau's avatar Romain Bioteau
Browse files

add coverage

parent ca298e2c
......@@ -16,76 +16,81 @@
*/
package org.bonitasoft.studio.migration.custom.migration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import java.util.List;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.bonitasoft.studio.common.DataTypeLabels;
import org.bonitasoft.studio.common.NamingUtils;
import org.bonitasoft.studio.model.process.ProcessPackage;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.edapt.migration.Instance;
import org.eclipse.emf.edapt.migration.MigrationFactory;
import org.eclipse.emf.edapt.migration.impl.MetamodelImpl;
import org.eclipse.emf.edapt.migration.impl.ModelImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* @author Romain Bioteau
*
*/
@RunWith(MockitoJUnitRunner.class)
public class BusinessObjectTypeCustomMigrationTest {
private BusinessObjectTypeCustomMigration migrationUnderTest;
@Spy
private ModelImpl model;
@Spy
private MetamodelImpl metamodel;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
migrationUnderTest= new BusinessObjectTypeCustomMigration();
MigrationFactory.eINSTANCE.createType();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Ignore
@Test
public void shouldMigrateAfter_AddBusinessObjectType_InDiagram_IfNotExists() throws Exception {
doReturn(metamodel).when(model).getMetamodel();
final EPackage ePackage = ProcessPackage.eINSTANCE.getMainProcess().getEPackage();
final EList<EPackage> result = new BasicEList<EPackage>();
result.add(ePackage);
doReturn(result).when(metamodel).getEPackages();
final EList<Instance> emptyList = new BasicEList<Instance>();
doReturn(emptyList).when(model).getAllInstances("process.BusinessObjectType");
final EList<Instance> diagramList = new BasicEList<Instance>();
final Instance instance = model.newInstance("process.MainProcess");
diagramList.add(instance);
doReturn(diagramList).when(model).getAllInstances("process.MainProcess");
migrationUnderTest.migrateAfter(model , metamodel);
assertThat((List<Instance>) instance.get("datatypes")).extracting("name").containsOnly(NamingUtils.convertToId(DataTypeLabels.businessObjectType));
}
private BusinessObjectTypeCustomMigration migrationUnderTest;
@Mock
private ModelImpl model;
@Mock
private MetamodelImpl metamodel;
@Mock
private Instance diagramInstance;
@Mock
private Instance businessDataTypeInstance;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
migrationUnderTest= new BusinessObjectTypeCustomMigration();
final EList<Instance> diagramList = new BasicEList<Instance>();
diagramList.add(diagramInstance);
when(model.getAllInstances("process.MainProcess")).thenReturn(diagramList);
when(model.newInstance("process.BusinessObjectType")).thenReturn(businessDataTypeInstance);
final EList<Instance> emptyDatatypesList = new BasicEList<Instance>();
when(diagramInstance.get("datatypes")).thenReturn(emptyDatatypesList);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_migrateAfter_Add_the_businessObjectType_InDiagram_IfNotExists() throws Exception {
migrationUnderTest.migrateAfter(model , metamodel);
verify(model).newInstance("process.BusinessObjectType");
verify(businessDataTypeInstance).set("name", NamingUtils.convertToId(DataTypeLabels.businessObjectType));
verify(diagramInstance).add("datatypes", businessDataTypeInstance);
}
@Test
public void should_migrateAfter_NOT_Add_the_businessObjectType_InDiagram_IfNotExists() throws Exception {
final EList<Instance> datatypesList = new BasicEList<Instance>();
when(businessDataTypeInstance.instanceOf("process.BusinessObjectType")).thenReturn(true);
datatypesList.add(businessDataTypeInstance);
when(diagramInstance.get("datatypes")).thenReturn(datatypesList);
migrationUnderTest.migrateAfter(model, metamodel);
verify(diagramInstance, never()).add("datatypes", businessDataTypeInstance);
}
}
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.migration.custom.migration.connector;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edapt.migration.Instance;
import org.eclipse.emf.edapt.migration.Metamodel;
import org.eclipse.emf.edapt.migration.Model;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* @author Romain Bioteau
*
*/
@RunWith(MockitoJUnitRunner.class)
public class UpdateConnectorVersionMigrationTest {
@Mock
private Model model;
@Mock
private Metamodel metamodel;
@Mock
private UpdateConnectorVersionMigration updateConnectorVersionMigration;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_migrateAfter_update_definitionVersion_in_connector() throws Exception {
//Given
doCallRealMethod().when(updateConnectorVersionMigration).migrateAfter(model, metamodel);
final EList<Instance> connectorInstanceList = connectorInstanceList("id1","id2");
final Instance oldConnectorInstance = aConnectorInstance("id1", "0.9");
connectorInstanceList.add(oldConnectorInstance);
when(model.getAllInstances("process.Connector")).thenReturn(connectorInstanceList);
when(model.getAllInstances("connectorconfiguration.ConnectorConfiguration")).thenReturn(new BasicEList<Instance>());
when(updateConnectorVersionMigration.shouldUpdateVersion("id1")).thenReturn(true);
when(updateConnectorVersionMigration.getOldDefinitionVersion()).thenReturn("1.0");
when(updateConnectorVersionMigration.getNewDefinitionVersion()).thenReturn("2.0");
//When
updateConnectorVersionMigration.migrateAfter(model, metamodel);
//Then
final Instance id1Connector = connectorInstanceList.get(0);
final Instance id2Connector = connectorInstanceList.get(1);
verify(id1Connector).set(UpdateConnectorVersionMigration.DEFINITION_VERSION_FEATURE_NAME, "2.0");
verify(id2Connector, never()).set(UpdateConnectorVersionMigration.DEFINITION_VERSION_FEATURE_NAME, "2.0");
verify(oldConnectorInstance, never()).set(UpdateConnectorVersionMigration.DEFINITION_VERSION_FEATURE_NAME, "2.0");
}
@Test
public void should_migrateAfter_update_version_in_connector_configuration() throws Exception {
//Given
doCallRealMethod().when(updateConnectorVersionMigration).migrateAfter(model, metamodel);
final EList<Instance> connectorConfigInstanceList = connectorConfiguratiobInstanceList("id1", "id2");
final Instance oldConnectorConfInstance = aConnectorConfigurationInstance("id1", "0.9");
connectorConfigInstanceList.add(oldConnectorConfInstance);
when(model.getAllInstances("connectorconfiguration.ConnectorConfiguration")).thenReturn(connectorConfigInstanceList);
when(model.getAllInstances("process.Connector")).thenReturn(new BasicEList<Instance>());
when(updateConnectorVersionMigration.shouldUpdateVersion("id1")).thenReturn(true);
when(updateConnectorVersionMigration.getOldDefinitionVersion()).thenReturn("1.0");
when(updateConnectorVersionMigration.getNewDefinitionVersion()).thenReturn("2.0");
//When
updateConnectorVersionMigration.migrateAfter(model, metamodel);
//Then
final Instance id1ConnectorConf = connectorConfigInstanceList.get(0);
final Instance id2ConnectorConf = connectorConfigInstanceList.get(1);
verify(id1ConnectorConf).set(UpdateConnectorVersionMigration.VERSION_FEATURE_NAME, "2.0");
verify(id2ConnectorConf, never()).set(UpdateConnectorVersionMigration.VERSION_FEATURE_NAME, "2.0");
verify(oldConnectorConfInstance, never()).set(UpdateConnectorVersionMigration.VERSION_FEATURE_NAME, "2.0");
}
private EList<Instance> connectorInstanceList(final String... defIds) {
final BasicEList<Instance> instances = new BasicEList<Instance>();
for (final String defId : defIds) {
instances.add(aConnectorInstance(defId, "1.0"));
}
return instances;
}
private EList<Instance> connectorConfiguratiobInstanceList(final String... defIds) {
final BasicEList<Instance> instances = new BasicEList<Instance>();
for (final String defId : defIds) {
instances.add(aConnectorConfigurationInstance(defId, "1.0"));
}
return instances;
}
private Instance aConnectorConfigurationInstance(final String definitionId, final String definitionVersion) {
final Instance instance = mock(Instance.class);
when(instance.get(UpdateConnectorVersionMigration.DEFINITION_ID_FEATURE_NAME)).thenReturn(definitionId);
when(instance.get(UpdateConnectorVersionMigration.VERSION_FEATURE_NAME)).thenReturn(definitionVersion);
return instance;
}
private Instance aConnectorInstance(final String definitionId, final String definitionVersion) {
final Instance instance = mock(Instance.class);
when(instance.get(UpdateConnectorVersionMigration.DEFINITION_ID_FEATURE_NAME)).thenReturn(definitionId);
when(instance.get(UpdateConnectorVersionMigration.DEFINITION_VERSION_FEATURE_NAME)).thenReturn(definitionVersion);
return instance;
}
}
......@@ -22,20 +22,20 @@ import org.eclipse.emf.edapt.migration.Model;
public abstract class UpdateConnectorVersionMigration extends CustomMigration {
public UpdateConnectorVersionMigration() {
super();
}
protected static final String VERSION_FEATURE_NAME = "version";
protected static final String DEFINITION_VERSION_FEATURE_NAME = "definitionVersion";
protected static final String DEFINITION_ID_FEATURE_NAME = "definitionId";
@Override
public void migrateAfter(final Model model, final Metamodel metamodel) throws MigrationException {
for (final Instance connectorInstance : model.getAllInstances("process.Connector")) {
final String defId = connectorInstance.get("definitionId");
final String defId = connectorInstance.get(DEFINITION_ID_FEATURE_NAME);
if (shouldUpdateVersion(defId)) {
updateVersion(connectorInstance);
}
}
for (final Instance connectorInstance : model.getAllInstances("connectorconfiguration.ConnectorConfiguration")) {
final String defId = connectorInstance.get("definitionId");
final String defId = connectorInstance.get(DEFINITION_ID_FEATURE_NAME);
if (shouldUpdateVersion(defId)) {
updateConfigVersion(connectorInstance);
}
......@@ -43,18 +43,18 @@ public abstract class UpdateConnectorVersionMigration extends CustomMigration {
}
private void updateVersion(final Instance connectorInstance) {
final String defVersion = connectorInstance.get("definitionVersion");
final String defVersion = connectorInstance.get(DEFINITION_VERSION_FEATURE_NAME);
final String previousDefinitionVersion = getOldDefinitionVersion();
if (defVersion.equals(previousDefinitionVersion)) {
connectorInstance.set("definitionVersion", getNewDefinitionVersion());
connectorInstance.set(DEFINITION_VERSION_FEATURE_NAME, getNewDefinitionVersion());
}
}
private void updateConfigVersion(final Instance connectorConfigInstance) {
final String defVersion = connectorConfigInstance.get("version");
final String defVersion = connectorConfigInstance.get(VERSION_FEATURE_NAME);
final String previousDefinitionVersion = getOldDefinitionVersion();
if (defVersion.equals(previousDefinitionVersion)) {
connectorConfigInstance.set("version", getNewDefinitionVersion());
connectorConfigInstance.set(VERSION_FEATURE_NAME, getNewDefinitionVersion());
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment