package io.dropwizard;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.nio.file.Files;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class BundleTest {
    @Test
    public void deprecatedBundleWillBeInitializedAndRun() throws Exception {
        final DeprecatedBundle deprecatedBundle = new DeprecatedBundle();
        assertThat(deprecatedBundle.wasInitialized()).isFalse();
        assertThat(deprecatedBundle.wasRun()).isFalse();

        final File configFile = File.createTempFile("bundle-test", ".yml");
        try {
            Files.write(configFile.toPath(), Collections.singleton("text: Test"));
            final TestApplication application = new TestApplication(deprecatedBundle);
            application.run("server", configFile.getAbsolutePath());
        } finally {
            configFile.delete();
        }

        assertThat(deprecatedBundle.wasInitialized()).isTrue();
        assertThat(deprecatedBundle.wasRun()).isTrue();
    }

    private static class TestConfiguration extends Configuration {
        @JsonProperty
        String text = "";

        public void setText(String text) {
            this.text = text;
        }

        public String getText() {
            return text;
        }
    }

    @SuppressWarnings("deprecation")
    private static class TestApplication extends Application<TestConfiguration> {
        Bundle bundle;

        public TestApplication(Bundle bundle) {
            this.bundle = bundle;
        }

        @Override
        public void initialize(Bootstrap<TestConfiguration> bootstrap) {
            bootstrap.addBundle(bundle);
        }

        @Override
        public void run(TestConfiguration configuration, Environment environment) {
        }
    }

    @SuppressWarnings("deprecation")
    private static class DeprecatedBundle implements Bundle {
        boolean wasInitialized = false;
        boolean wasRun = false;

        @Override
        public void initialize(Bootstrap<?> bootstrap) {
            wasInitialized = true;
        }

        @Override
        public void run(Environment environment) {
            wasRun = true;
        }

        public boolean wasInitialized() {
            return wasInitialized;
        }

        public boolean wasRun() {
            return wasRun;
        }
    }
}

@@ -251,26 +251,21 @@
 va.util.
-Collection
+Array
 s;%0A%0Aimpo
@@ -768,46 +768,316 @@
 (), 
-Collections.singleton(%22text: Test%22
+Arrays.asList(%0A                %22text: Test%22,%0A                %22server:%22,%0A                %22  applicationConnectors:%22,%0A                %22    - type: http%22,%0A                %22      port: 0%22,%0A                %22  adminConnectors:%22,%0A                %22    - type: http%22,%0A                %22      port: 0%22%0A            
 ));%0A
+%0A
     

Fix test that binds to 8080