package net.raymanoz.config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import org.junit.Before;
import org.junit.Test;
import net.raymanoz.util.Properties;
import static org.junit.Assert.*;
public class ParameteriserImplTests {
final private Properties properties = mock(Properties.class);
private Parameteriser parameteriser;
@Before
public void setUp(){
when(properties.getProperty(ParameteriserImpl.key)).thenReturn("acc_mosaic_importer,acc_mosaic,an_undefined_param,emptystr_param");
when(properties.getProperty("acc_mosaic_importer")).thenReturn("mosaic_importer");
when(properties.getProperty("acc_mosaic")).thenReturn("mosaic");
when(properties.getProperty("an_undefined_param")).thenReturn(null);
when(properties.getProperty("emptystr_param")).thenReturn(" ");
parameteriser = new ParameteriserImpl(properties);
}
private void chk(String expected, String source){
assertEquals("parameteriser: " + source, expected, parameteriser.parameterise(source));
}
@Test
public void parameteriserShouldNotReplaceValueIfUndefinedNull(){
chk("${acc_mosaic}.table", "mosaic.table");
chk("an_undefined_param.table", "an_undefined_param.table");
chk("on an_undefined_param;", "on an_undefined_param;");
chk("connect an_undefined_param/", "connect an_undefined_param/");
}
@Test
public void parameteriserShouldNotReplaceValueIfOnOwn(){
chk("mosaic", "mosaic");
chk(" mosaic_importer ", " mosaic_importer ");
}
@Test
public void parameteriserShouldNotReplaceIfParameterName(){
chk("${mosaic_importer}", "${mosaic_importer}");
chk("${mosaic}", "${mosaic}");
}
@Test
public void parameteriserShouldNotReplaceIfMiddleString(){
chk("ThisIsMosaic.", "ThisIsMosaic.");
chk("Xmosaic_importer.", "Xmosaic_importer.");
chk("_mosaic_importer.", "_mosaic_importer.");
chk("on MosaicThis.", "on MosaicThis.");
}
@Test
public void parameteriserReplaceIfFollowedFullStop(){
chk("${acc_mosaic}.table", "mosaic.table");
chk("${acc_mosaic_importer}.table", "MOSAIC_IMPORTER.table");
chk("${acc_mosaic}.", "Mosaic.");
chk("\n${acc_mosaic}.", "\nMosaic.");
}
@Test
public void parameteriserNotReplacedIfFollowedFollowsFullStop(){
chk("schema.mosaic.table", "schema.mosaic.table");
chk("schema.MOSAIC_IMPORTER.table", "schema.MOSAIC_IMPORTER.table");
chk(".Mosaic.table", ".Mosaic.table");
}
@Test
public void parameteriserShouldBeReplaceValueIfInConnect(){
chk("connect ${acc_mosaic_importer}/", "connect mosaic_importer/");
chk("connect ${acc_mosaic}/", "CONNECT mosaic/");
chk("connect ${acc_mosaic_importer}/", "connect MOSAIC_IMPORTER/");
}
@Test
public void parameteriserShouldBeReplaceValueIfFollowingOn(){
chk("to ${acc_mosaic};", "to mosaic;");
chk("to ${acc_mosaic_importer} ;", "to MOSAIC_IMPORTER ;");
chk("to ${acc_mosaic_importer};", "to mosaic_importer;");
}
@Test
public void transferParameteriserProperties(){
final Properties uMigrateProperties = mock(Properties.class);
final Properties scriptProperties = mock(Properties.class);
doThrow(new NullPointerException()).when(scriptProperties).setProperty(anyString(), (String) eq(null));
when(uMigrateProperties.getProperty(UMigrateProperty.PARAMETERISER_PROPERTIES.getKey())).thenReturn("acc_mosaic_importer,acc_mosaic,an_undefined_param,emptystr_param");
when(uMigrateProperties.getProperty("acc_mosaic_importer")).thenReturn("mosaic_importer");
when(uMigrateProperties.getProperty("acc_mosaic")).thenReturn("mosaic");
when(uMigrateProperties.getProperty("an_undefined_param")).thenReturn(null);
when(uMigrateProperties.getProperty("emptystr_param")).thenReturn(" ");
ParameteriserImpl.transferParameteriserProperties(uMigrateProperties, scriptProperties);
verify(scriptProperties).setProperty(ParameteriserImpl.key, "acc_mosaic_importer,acc_mosaic,an_undefined_param,emptystr_param");
verify(scriptProperties).setProperty("acc_mosaic_importer", "mosaic_importer");
verify(scriptProperties).setProperty("acc_mosaic", "mosaic");
verify(scriptProperties, never()).setProperty("an_undefined_param", null);
verify(scriptProperties).setProperty("emptystr_param", " ");
verify(uMigrateProperties).getProperty(UMigrateProperty.PARAMETERISER_PROPERTIES.getKey());
verify(uMigrateProperties).getProperty("acc_mosaic_importer");
verify(uMigrateProperties).getProperty("acc_mosaic");
verify(uMigrateProperties).getProperty("an_undefined_param");
verify(uMigrateProperties).getProperty("emptystr_param");
verifyNoMoreInteractions(uMigrateProperties, scriptProperties);
}
@Test
public void transferParameteriserPropertiesNullParamertiserProperty(){
final Properties uMigrateProperties = mock(Properties.class);
final Properties scriptProperties = mock(Properties.class);
doThrow(new NullPointerException()).when(scriptProperties).setProperty(anyString(), (String) eq(null));
when(uMigrateProperties.getProperty(UMigrateProperty.PARAMETERISER_PROPERTIES.getKey())).thenReturn(null);
ParameteriserImpl.transferParameteriserProperties(uMigrateProperties, scriptProperties);
verify(uMigrateProperties).getProperty(UMigrateProperty.PARAMETERISER_PROPERTIES.getKey());
verifyNoMoreInteractions(uMigrateProperties, scriptProperties);
}
}