package nl.hsleiden.beans;

import hslbeans.YoutubePlayerParameters;
import hslbeans.YoutubeUrlParameters;

import javax.jcr.Node;

import nl.hsleiden.utils.TestUtils;

import org.easymock.EasyMock;
import org.hippoecm.hst.content.beans.ObjectBeanManagerException;
import org.hippoecm.hst.content.beans.manager.ObjectConverter;
import org.junit.Assert;
import org.junit.Test;

public class YoutubeBeanTest {

    @Test
    public void getUrlTest() throws Exception {
        boolean wrongVideoId = false;
        ObjectConverter objectConverter = createMockObjectConverter(wrongVideoId);
        YoutubeBean youtubeBean = new YoutubeBean();
        youtubeBean.setObjectConverter(objectConverter);
        String url = youtubeBean.getUrl();
        Assert.assertEquals("http://www.youtube.com/embed/yhKB-VxJWpg?disablekb=0&theme=black&showinfo=0&rel=0&autoplay=1",
                url);
    }

    @Test
    public void getUrlTestVideoIdEmpty() throws Exception {
        boolean wrongVideoId = true;
        ObjectConverter objectConverter = createMockObjectConverter(wrongVideoId);
        YoutubeBean youtubeBean = new YoutubeBean();
        youtubeBean.setObjectConverter(objectConverter);
        String url = youtubeBean.getUrl();
        Assert.assertEquals(null, url);
    }

    private ObjectConverter createMockObjectConverter(boolean wrongVideoid) throws Exception {
        ObjectConverter objectConverter = EasyMock.createMock(ObjectConverter.class);
        try {
            EasyMock.expect(objectConverter.getObject((Node) null, "hsl:youtubeUrlParameters"))
                    .andReturn(createMockYoutubeUrlParameters(wrongVideoid)).anyTimes();
            EasyMock.expect(objectConverter.getObject((Node) null, "hsl:youtubePlayerParameters"))
                    .andReturn(createMockYoutubePlayerParameters()).anyTimes();
            EasyMock.replay(objectConverter);
        } catch (ObjectBeanManagerException e) {
            // never going to happens
        }
        return objectConverter;
    }

    private YoutubeUrlParameters createMockYoutubeUrlParameters(boolean wrongVideoId) throws NoSuchFieldException,
            SecurityException, IllegalArgumentException, IllegalAccessException {

        YoutubeUrlParameters youtubeUrlParameters = new YoutubeUrlParameters();
        if (wrongVideoId) {
            TestUtils.setPrivateField(youtubeUrlParameters, "youtubeUrl", "wrong");
        } else {
            TestUtils
                    .setPrivateField(youtubeUrlParameters, "youtubeUrl", "https://www.youtube.com/watch?v=yhKB-VxJWpg");
        }

        TestUtils.setPrivateField(youtubeUrlParameters, "theme", "black");
        return youtubeUrlParameters;
    }

    private YoutubePlayerParameters createMockYoutubePlayerParameters() throws NoSuchFieldException,
            IllegalAccessException {
        YoutubePlayerParameters youtubePlayerParameters = new YoutubePlayerParameters();
        TestUtils.setPrivateField(youtubePlayerParameters, "autoplay", true);
        return youtubePlayerParameters;
    }

}

@@ -726,32 +726,41 @@
 eBean.getUrl();%0A
+        %0A
         Assert.a
@@ -764,21 +764,32 @@
 t.assert
-Equal
+True(url.contain
 s(%22http:
@@ -827,85 +827,286 @@
 JWpg
-?disablekb=0&theme=black&showinfo=0&rel=0&autoplay=1%22,%0A                url
+%22));%0A        Assert.assertTrue(url.contains(%22disablekb=0%22));%0A        Assert.assertTrue(url.contains(%22theme=black%22));%0A        Assert.assertTrue(url.contains(%22showinfo=0%22));%0A        Assert.assertTrue(url.contains(%22rel=0%22));%0A        Assert.assertTrue(url.contains(%22autoplay=1%22)
 );%0A
+%0A
     

make test compatible with java 8