package com.github.sbugat.ec2tools.service.configuration;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import com.github.sbugat.GenericMockitoTest;

/**
 * Configuration service test.
 * 
 * @author Sylvain Bugat
 */
@RunWith(MockitoJUnitRunner.class)
public class ConfigurationServiceTest extends GenericMockitoTest {

	@Mock
	private HierarchicalINIConfiguration hierarchicalINIConfiguration;

	@InjectMocks
	private ConfigurationService configurationService;

	@Test
	public void testLoadConfigurationEmpty() throws ConfigurationException {

		configurationService.loadConfiguration();

		final InOrder inOrder = Mockito.inOrder(hierarchicalINIConfiguration);

		inOrder.verify(hierarchicalINIConfiguration).clear();
		inOrder.verify(hierarchicalINIConfiguration).load(ConfigurationService.CONFIGURATION_FILE_NAME);
		inOrder.verify(hierarchicalINIConfiguration).getSections();
	}
}

@@ -160,35 +160,32 @@
 ion.Hierarchical
-INI
 Configuration;%0Ai
@@ -179,24 +179,331 @@
 figuration;%0A
+import org.apache.commons.configuration.HierarchicalINIConfiguration;%0Aimport org.apache.commons.configuration.SubnodeConfiguration;%0Aimport org.assertj.core.api.Assertions;%0Aimport org.assertj.core.data.MapEntry;%0Aimport org.assertj.core.util.Lists;%0Aimport org.assertj.core.util.Sets;%0Aimport org.junit.Assert;%0A
 import org.j
@@ -751,16 +751,110 @@
 itoTest;
+%0Aimport com.github.sbugat.ec2tools.InstanceOrder;%0Aimport com.github.sbugat.ec2tools.OrderType;
 %0A%0A/**%0A *
@@ -1018,16 +1018,125 @@
 Test %7B%0A%0A
+%09private static final String SECTION = %22section%22;%0A%09private static final String INSTANCE_ID = %22instance id%22;%0A%0A
 %09@Mock%0A%09
@@ -1626,16 +1626,16 @@
 _NAME);%0A
-
 %09%09inOrde
@@ -1690,11 +1690,1717 @@
 ns();%0A%09%7D
+%0A%0A%09@Test%0A%09public void testLoadConfigurationException() throws ConfigurationException %7B%0A%0A%09%09Mockito.doThrow(new ConfigurationException()).when(hierarchicalINIConfiguration).load(ConfigurationService.CONFIGURATION_FILE_NAME);%0A%0A%09%09try %7B%0A%09%09%09configurationService.loadConfiguration();%0A%09%09%09Assert.fail();%0A%09%09%7D%0A%09%09catch (final ConfigurationException e) %7B%0A%09%09%09// Expected exeption%0A%0A%09%09%09final InOrder inOrder = Mockito.inOrder(hierarchicalINIConfiguration);%0A%0A%09%09%09inOrder.verify(hierarchicalINIConfiguration).clear();%0A%09%09%09inOrder.verify(hierarchicalINIConfiguration).load(ConfigurationService.CONFIGURATION_FILE_NAME);%0A%09%09%7D%0A%09%7D%0A%0A%09@Test%0A%09public void testLoadConfiguration() throws ConfigurationException %7B%0A%0A%09%09Mockito.doReturn(Sets.newLinkedHashSet(SECTION)).when(hierarchicalINIConfiguration).getSections();%0A%0A%09%09final SubnodeConfiguration subnodeConfiguration = new SubnodeConfiguration(new HierarchicalConfiguration(), new HierarchicalConfiguration.Node());%0A%09%09subnodeConfiguration.setProperty(INSTANCE_ID, OrderType.START.toString());%0A%09%09Mockito.doReturn(subnodeConfiguration).when(hierarchicalINIConfiguration).getSection(SECTION);%0A%0A%09%09configurationService.loadConfiguration();%0A%0A%09%09final InOrder inOrder = Mockito.inOrder(hierarchicalINIConfiguration);%0A%0A%09%09inOrder.verify(hierarchicalINIConfiguration).clear();%0A%09%09inOrder.verify(hierarchicalINIConfiguration).load(ConfigurationService.CONFIGURATION_FILE_NAME);%0A%09%09inOrder.verify(hierarchicalINIConfiguration).getSections();%0A%09%09inOrder.verify(hierarchicalINIConfiguration).getSection(SECTION);%0A%0A%09%09Assertions.assertThat(configurationService.getConfiguredSections()).containsOnly(MapEntry.entry(SECTION, Lists.newArrayList(new InstanceOrder(INSTANCE_ID, OrderType.START.toString()))));%0A%09%7D
 %0A%7D%0A

Add tests