/*
 * The MIT License
 *
 * Copyright (c) 2016, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package io.jenkins.blueocean.jsextensions;

import io.jenkins.blueocean.jsextensions.JenkinsJSExtensions;
import io.jenkins.blueocean.service.embedded.BaseTest;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
 */
public class JenkinsJSExtensionsTest extends BaseTest {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Test
    public void test() {
        // Simple test of the rest endpoint. It should find the "blueocean-dashboard"
        // plugin ExtensionPoint contributions.
        Map response = get("/js-extensions", Map.class);
        List<Map> extensions = (List)response.get("data");

        Assert.assertEquals(1, extensions.size());
        Assert.assertEquals("blueocean-dashboard", extensions.get(0).get("hpiPluginId"));

        List<Map> ext = (List<Map>) extensions.get(0).get("extensions");

        Assert.assertEquals(5, ext.size());
        Assert.assertEquals("AdminNavLink", ext.get(0).get("component"));
        Assert.assertEquals("jenkins.logo.top", ext.get(0).get("extensionPoint"));

        // Calling JenkinsJSExtensions.getJenkinsJSExtensionData() multiple times should
        // result in the same object instance being returned because the list of plugin
        // has not changed i.e. we have a simple optimization in there where we only scan
        // the classpath if the active plugin lust has changed.
        Assert.assertEquals(
            JenkinsJSExtensions.getJenkinsJSExtensionData(),
            JenkinsJSExtensions.getJenkinsJSExtensionData()
        );
    }

    @Override
    protected String getContextPath() {
        return "blue";
    }
}

@@ -1184,70 +1184,8 @@
 s;%0A%0A
-import io.jenkins.blueocean.jsextensions.JenkinsJSExtensions;%0A
 impo
@@ -1655,16 +1655,48 @@
       //
+ and %22blueocean-personalization%22
  plugin 
@@ -1870,17 +1870,17 @@
 tEquals(
-1
+2
 , extens
@@ -1889,32 +1889,33 @@
 ns.size());%0A
+%0A
         
 Assert.asser
@@ -1906,126 +1906,465 @@
     
-Assert.assertEquals(%22blueocean-dashboard%22, extensions.get(0).get(%22hpiPluginId%22));%0A%0A        List%3CMap%3E ext = (List%3CMap%3E)
+for (Map extension : extensions) %7B%0A            List%3CMap%3E extensionPoints = (List%3CMap%3E) extension.get(%22extensions%22);%0A            String pluginId = (String) extension.get(%22hpiPluginId%22);%0A%0A            if (%22blueocean-dashboard%22.equals(pluginId)) %7B%0A                Assert.assertEquals(5, extensionPoints.size());%0A                Assert.assertEquals(%22AdminNavLink%22, extensionPoints.get(0).get(%22component%22));%0A                Assert.assertEquals(%22jenkins.logo.top%22,
  ext
@@ -2365,24 +2365,29 @@
 %22, extension
+Point
 s.get(0).get
@@ -2401,22 +2401,102 @@
 sion
-s
+Point
 %22)
+)
 ;%0A
-%0A
+            %7D else if (%22blueocean-personalization%22.equals(pluginId)) %7B%0A     
         
 Asse
@@ -2483,32 +2483,35 @@
  %7B%0A             
+   
 Assert.assertEqu
@@ -2514,22 +2514,34 @@
 tEquals(
-5
+1
 , ext
+ensionPoints
 .size())
@@ -2542,32 +2542,40 @@
 ize());%0A        
+      
+  
 Assert.assertEqu
@@ -2579,34 +2579,55 @@
 Equals(%22
-AdminNavLink%22, ext
+components/ActionLink%22, extensionPoints
 .get(0).
@@ -2645,32 +2645,40 @@
 ent%22));%0A        
+ 
+       
 Assert.assertEqu
@@ -2694,53 +2694,207 @@
 ins.
-logo.top%22, ext.get(0).get(%22extensionPoint%22));
+dashboard.item.action%22, extensionPoints.get(0).get(%22extensionPoint%22));%0A            %7D else %7B%0A                Assert.fail(%22Found extensions from unknown pluginId: %22 + pluginId);%0A            %7D%0A        %7D
 %0A%0A  

update extension point test to account for contributions from new personalizations module