package org.jenkinsci.plugins.mktmpio;

import hudson.model.FreeStyleProject;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyString;

public class MktmpioDescriptorTest {
    @ClassRule
    public static final JenkinsRule j = new JenkinsRule().withNewHome();

    @Test
    public void testDefaults() throws Exception {
        final MktmpioDescriptor config = getDescriptor();
        config.setToken("");
        config.setServer("");
        assertThat(config.getToken(), isEmptyString());
        assertThat(config.getServer(), is(Mktmpio.DEFAULT_SERVER));
        config.setToken("something");
        assertThat(config.getToken(), is("something"));
        config.setServer("something-else");
        assertThat(config.getServer(), is("something-else"));
    }

    @Test
    public void testGlobalConfigRoundTrip() throws Exception {
        final MktmpioDescriptor config = getDescriptor();
        final String server = config.getServer();
        final String token = config.getToken();
        j.configRoundtrip();
        assertThat(config.getServer(), is(server));
        assertThat(config.getToken(), is(token));
    }

    @Test
    public void testJobConfigRoundTrip() throws Exception {
        FreeStyleProject p = j.createFreeStyleProject();
        Mktmpio before = new Mktmpio("redis");
        p.getBuildWrappersList().add(before);
        j.configRoundtrip(p);
        Mktmpio after = p.getBuildWrappersList().get(Mktmpio.class);
        j.assertEqualBeans(before, after, "dbs");
    }

    private MktmpioDescriptor getDescriptor() {
        return (MktmpioDescriptor) j.jenkins.getDescriptor(Mktmpio.class);
    }
}

@@ -470,16 +470,454 @@
 test
+Server
 Default
-s
+() throws Exception %7B%0A        final MktmpioDescriptor config = getDescriptor();%0A        config.setServer(%22%22);%0A        assertThat(config.getServer(), is(Mktmpio.DEFAULT_SERVER));%0A        config.setServer(%22something-else%22);%0A        assertThat(config.getServer(), is(%22something-else%22));%0A        config.setServer(null);%0A        assertThat(config.getServer(), is(Mktmpio.DEFAULT_SERVER));%0A    %7D%0A%0A    @Test%0A    public void testTokenDefault
 () t
@@ -1025,38 +1025,8 @@
 %22);%0A
-        config.setServer(%22%22);%0A
     
@@ -1081,76 +1081,8 @@
 ));%0A
-        assertThat(config.getServer(), is(Mktmpio.DEFAULT_SERVER));%0A
     
@@ -1189,39 +1189,26 @@
 nfig.set
-Server(%22something-else%22
+Token(null
 );%0A     
@@ -1235,37 +1235,31 @@
 .get
-Server(), is(%22something-else%22
+Token(), isEmptyString(
 ));%0A

refactor descriptor test to cover defaults