package com.atlassian.stash.internal.johnson.handler.exception;

import com.atlassian.stash.internal.home.HomeDirectoryMigrationException;
import com.atlassian.stash.internal.johnson.handler.EventDetails;
import com.atlassian.stash.internal.johnson.handler.bean.BeanFailureHandler;
import com.atlassian.stash.internal.johnson.handler.bean.HomeDirectoryMigrationHandler;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

public class HomeDirectoryMigrationHandlerTest {

    @Test
    public void testHandle() throws Exception {
        BeanFailureHandler handler = new HomeDirectoryMigrationHandler();
        HomeDirectoryMigrationException error = new HomeDirectoryMigrationException(ImmutableList.of("migration-error1", "migration-error2"));
        EventDetails event = handler.handle(new BeanCreationException("msg", error));

        assertNotNull("the migration exception should have been handled", event);
        assertThat(event.getEventType(), equalTo("home-migration"));
        assertThat(event.getThrowable(), equalTo((Throwable) error));
        assertThat(event.getMessage(), allOf(containsString("migration-error1"), containsString("migration-error2")));
    }
}
