/*
 * Copyright 2015 Norbert Potocki (norbert.potocki@nort.pl)
 *
 * 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 pl.nort.config.provider;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Properties;

@RunWith(MockitoJUnitRunner.class)
public class SimpleConfigurationProviderBindTest extends SimpleConfigurationProviderAbstractTest {

  public interface ConfigPojo {
    Integer someSetting();

    List<Boolean> otherSetting();
  }

  @Test
  public void bindShouldThrowWhenFetchingNonexistentKey() throws Exception {
    when(configurationSource.getConfiguration()).thenReturn(new Properties());

    expectedException.expect(NoSuchElementException.class);
    simpleConfigurationProvider.bind("", ConfigPojo.class);
  }

  @Test
  public void bindShouldThrowWhenUnableToFetchKey() throws Exception {
    when(configurationSource.getConfiguration()).thenThrow(IllegalStateException.class);

    expectedException.expect(IllegalStateException.class);
    simpleConfigurationProvider.bind("", ConfigPojo.class);
  }

  @Test
  public void bindShouldThrowOnIncompatibleConversion() throws Exception {
    when(configurationSource.getConfiguration()).thenReturn(propertiesWith("someSetting", "42", "otherSetting", "42"));

    expectedException.expect(IllegalArgumentException.class);
    simpleConfigurationProvider.bind("", ConfigPojo.class);
  }

  @Test
  public void shouldBindInitialValues() throws Exception {
    when(configurationSource.getConfiguration()).thenReturn(propertiesWith("someSetting", "42", "otherSetting", "true,false"));

    ConfigPojo config = simpleConfigurationProvider.bind("", ConfigPojo.class);
    assertThat(config.someSetting()).isEqualTo(42);
    assertThat(config.otherSetting()).containsExactly(true, false);
  }

  @Test
  public void shouldBindInitialValuesInSubPath() throws Exception {
    when(configurationSource.getConfiguration()).thenReturn(propertiesWith("myContext.someSetting", "42", "myContext.otherSetting", "true,false"));

    ConfigPojo config = simpleConfigurationProvider.bind("myContext", ConfigPojo.class);
    assertThat(config.someSetting()).isEqualTo(42);
    assertThat(config.otherSetting()).containsExactly(true, false);
  }

  @Test
  public void shouldUpdateValuesAfterChange() throws Exception {
    when(configurationSource.getConfiguration()).thenReturn(propertiesWith("someSetting", "42", "otherSetting", "true,false"));
    ConfigPojo config = simpleConfigurationProvider.bind("", ConfigPojo.class);

    when(configurationSource.getConfiguration()).thenReturn(propertiesWith("someSetting", "0", "otherSetting", "true"));

    assertThat(config.someSetting()).isEqualTo(0);
    assertThat(config.otherSetting()).containsExactly(true);
  }
}

@@ -1139,16 +1139,84 @@
 ting();%0A
+  %7D%0A%0A  public interface MultiPropertyConfigPojo extends ConfigPojo %7B
 %0A    Lis
@@ -2008,32 +2008,22 @@
 %22, %22
-42%22, %22otherSetting%22, %2242
+shouldBeNumber
 %22));
@@ -2183,28 +2183,34 @@
 ouldBind
-InitialValue
+AllInterfaceMethod
 s() thro
@@ -2349,32 +2349,45 @@
 ,false%22));%0A%0A    
+MultiProperty
 ConfigPojo confi
@@ -2419,32 +2419,45 @@
 ovider.bind(%22%22, 
+MultiProperty
 ConfigPojo.class
@@ -2633,17 +2633,8 @@
 lues
-InSubPath
 () t
@@ -2727,26 +2727,16 @@
 esWith(%22
-myContext.
 someSett
@@ -2749,48 +2749,8 @@
 %2242%22
-, %22myContext.otherSetting%22, %22true,false%22
 ));%0A
@@ -2812,17 +2812,8 @@
 nd(%22
-myContext
 %22, C
@@ -2888,71 +2888,334 @@
 ;%0A  
-  assertThat(config.otherSetting()).containsExactly(true, false
+%7D%0A%0A  @Test%0A  public void shouldBindInitialValuesInSubPath() throws Exception %7B%0A    when(configurationSource.getConfiguration()).thenReturn(propertiesWith(%22myContext.someSetting%22, %2242%22));%0A%0A    ConfigPojo config = simpleConfigurationProvider.bind(%22myContext%22, ConfigPojo.class);%0A    assertThat(config.someSetting()).isEqualTo(42
 );%0A 
@@ -3250,31 +3250,28 @@
 ould
-UpdateValuesAfter
+ReactToSource
 Change
+s
 () t
@@ -3386,38 +3386,8 @@
 %2242%22
-, %22otherSetting%22, %22true,false%22
 ));%0A
@@ -3564,32 +3564,8 @@
  %220%22
-, %22otherSetting%22, %22true%22
 ));%0A
@@ -3620,69 +3620,8 @@
 0);%0A
-    assertThat(config.otherSetting()).containsExactly(true);%0A
   %7D%0A

simplify SimpleConfigurationProviderBindTest