/*
 * Autosleep
 * Copyright (C) 2016 Orange
 * Authors: Benjamin Einaudi   benjamin.einaudi@orange.com
 *          Arnaud Ruffin      arnaud.ruffin@orange.com
 *
 * 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 org.cloudfoundry.autosleep.config;

import org.cloudfoundry.autosleep.util.BeanGenerator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.env.Environment;

import java.util.UUID;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class DeployedApplicationConfigTest {

    private static final UUID APP_ID = UUID.randomUUID();

    private static final String APPLICATION_NAME = "test";

    private static final String[] URIS = {"somewhere.org", "somewhere-else.org", "nowhere.org"};

    @Mock
    private Environment environment;

    @InjectMocks
    private DeployedApplicationConfig config;

    @Test
    public void testLoadCurrentDeployment() throws Exception {
        when(environment.getProperty(eq(Config.EnvKey.APPLICATION_DESCRIPTION_ENVIRONMENT_KEY)))
                .thenReturn(null);
        DeployedApplicationConfig.Deployment deployment = config.loadCurrentDeployment();
        assertThat(deployment, is(nullValue()));

        when(environment.getProperty(eq(Config.EnvKey.APPLICATION_DESCRIPTION_ENVIRONMENT_KEY)))
                .thenReturn(BeanGenerator.getSampleVcapApplication(APP_ID, APPLICATION_NAME))
                .thenReturn(BeanGenerator.getSampleVcapApplication(APP_ID, APPLICATION_NAME, URIS));
        deployment = config.loadCurrentDeployment();
        assertThat(deployment.getApplicationUris(), is(notNullValue()));
        assertThat(deployment.getApplicationUris().size(), is(equalTo(0)));
        assertThat(deployment.getFirstUri(), is(nullValue()));

        deployment = config.loadCurrentDeployment();
        assertThat(deployment, is(notNullValue()));
        assertThat(deployment.getApplicationId(), is(equalTo(APP_ID.toString())));
        assertThat(deployment.getApplicationName(), is(equalTo(APPLICATION_NAME)));
        assertThat(deployment.getApplicationUris(), is(notNullValue()));
        assertThat(deployment.getApplicationUris().size(), is(equalTo(URIS.length)));
        assertThat(deployment.getFirstUri(), is(equalTo(URIS[0])));

    }

}
@@ -1463,99 +1463,99 @@
 nal 
-UUID APP_ID = UUID.randomUUID();%0A%0A    private static final String APPLICATION_NAME = %22test%22
+String APPLICATION_NAME = %22test%22;%0A%0A    private static final UUID APP_ID = UUID.randomUUID()
 ;%0A%0A 
@@ -1655,56 +1655,8 @@
 %7D;%0A%0A
-    @Mock%0A    private Environment environment;%0A%0A
     
@@ -1715,16 +1715,64 @@
 onfig;%0A%0A
+    @Mock%0A    private Environment environment;%0A%0A
     @Tes
@@ -1797,20 +1797,23 @@
 test
-LoadC
+_read_c
 urrent
-D
+_d
 eplo
@@ -1817,16 +1817,33 @@
 ployment
+_with_but_no_uris
 () throw
@@ -1856,16 +1856,65 @@
 ption %7B%0A
+        //Given property is present without uris%0A
         
@@ -2034,14 +2034,108 @@
 urn(
-null);
+BeanGenerator.getSampleVcapApplication(APP_ID, APPLICATION_NAME));%0A        //When deployment is done
 %0A   
@@ -2213,32 +2213,100 @@
 ntDeployment();%0A
+        //Then deployment is not null but  does not contain any uri%0A
         assertTh
@@ -2320,24 +2320,27 @@
 oyment, is(n
+otN
 ullValue()))
@@ -2333,32 +2333,385 @@
 tNullValue()));%0A
+        assertThat(deployment.getApplicationUris(), is(notNullValue()));%0A        assertThat(deployment.getApplicationUris().size(), is(equalTo(0)));%0A        assertThat(deployment.getFirstUri(), is(nullValue()));%0A    %7D%0A%0A    @Test%0A    public void test_read_current_deployment_with_uris() throws Exception %7B%0A        //Given property is present without uris
 %0A        when(en
@@ -2796,102 +2796,8 @@
 )))%0A
-                .thenReturn(BeanGenerator.getSampleVcapApplication(APP_ID, APPLICATION_NAME))%0A
     
@@ -2893,32 +2893,39 @@
 URIS));%0A        
+//When 
 deployment = con
@@ -2923,251 +2923,103 @@
 ent 
-= config.loadCurrentDeployment();%0A        assertThat(deployment.getApplicationUris(), is(notNullValue()));%0A        assertThat(deployment.getApplicationUris().size(), is(equalTo(0)));%0A        assertThat(deployment.getFirstUri(), is(nullValue())
+is done%0A        DeployedApplicationConfig.Deployment deployment = config.loadCurrentDeployment(
 );%0A%0A
@@ -3026,16 +3026,23 @@
         
+//Then 
 deployme
@@ -3048,41 +3048,40 @@
 ent 
-= config.loadCurrentDeployment();
+is not null and contains any uri
 %0A   
@@ -3531,9 +3531,496 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void test_read_current_deployment_without_property() throws Exception %7B%0A        //Given property is not present%0A        when(environment.getProperty(eq(Config.EnvKey.APPLICATION_DESCRIPTION_ENVIRONMENT_KEY)))%0A                .thenReturn(null);%0A        //When deployment is done%0A        DeployedApplicationConfig.Deployment deployment = config.loadCurrentDeployment();%0A        //Then deployment returns nothing%0A        assertThat(deployment, is(nullValue()));%0A    %7D%0A%0A
 %7D

Refactor of DeployedApplicationConfigTest