package com.atlassian.bitbucket.property;

import org.junit.Test;

import javax.annotation.Nonnull;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class SimplePropertySupportTest {

    @Test
    public void testInitializeNonNullProperties() {
        PropertyMap properties = new SimplePropertySupport(new Builder()).getProperties();
        assertNotNull(properties);
        assertTrue(properties.isEmpty());
    }

    @Test(expected = IllegalStateException.class)
    public void testSetPropertiesInitializedWithNonEmptyProperties() {
        SimplePropertySupport support = new SimplePropertySupport(new Builder().property("key", "value"));
        support.setProperties(new PropertyMap.Builder().property("key", "value").build());
    }

    @Test
    public void testSetEmptyPropertiesMultipleTimes() {
        SimplePropertySupport support = new SimplePropertySupport(new Builder());
        support.setProperties(new PropertyMap.Builder().build());
        support.setProperties(PropertyMap.EMPTY);
        support.setProperties(new PropertyMap.Builder().property("key", "value").build());
    }

    @Test(expected = IllegalStateException.class)
    public void testSetNonEmptyPropertiesMoreThanOnce() {
        SimplePropertySupport support = new SimplePropertySupport(new Builder());
        support.setProperties(new PropertyMap.Builder().property("key", "value").build());
        support.setProperties(new PropertyMap.Builder().property("anotherkey", "anothervalue").build());
    }

    private static class Builder extends SimplePropertySupport.AbstractPropertyBuilder<Builder> {

        @Nonnull
        @Override
        protected Builder self() {
            return this;
        }
    }
}
