/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.tools;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;

import jetbrains.buildServer.core.runtime.RuntimeUtil;
import jetbrains.buildServer.core.runtime.osgx.IToolDescriptor;
import jetbrains.buildServer.core.runtime.osgx.ToolRepository;
import jetbrains.buildServer.core.runtime.osgx.IToolDescriptor.IInstallActionDescriptor;
import jetbrains.buildServer.tools.installer.agent.ToolsInstallerAgentSupport;
import junit.framework.Assert;

import org.junit.Test;

public class ToolRepositoryAccessTest extends AbstractToolingTestCase {

  @Test
  public void localRepository_properly_configured() throws Exception {
    final ToolRepository localRepo = getBundleRepository();
    Assert.assertNotNull(localRepo);
  }

  @Test
  public void localRepository_tools() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    Assert.assertNotNull(localRepo.tools(RuntimeUtil.CONSOLE_MONITOR));
    Assert.assertTrue(localRepo.tools(RuntimeUtil.CONSOLE_MONITOR).length > 0);
  }

  @Test
  public void localRepository_tools_all_actions() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    Assert.assertNotNull(localRepo.tools(RuntimeUtil.CONSOLE_MONITOR));
    Assert.assertTrue(localRepo.tools(RuntimeUtil.CONSOLE_MONITOR).length > 0);
    ArrayList<IToolDescriptor> toolsWithoutActions = new ArrayList<IToolDescriptor>();
    for (IToolDescriptor tool : localRepo.tools(RuntimeUtil.CONSOLE_MONITOR)) {
      final IInstallActionDescriptor[] actions = tool.getActions();
      if (actions == null || actions.length == 0) {
        toolsWithoutActions.add(tool);
      }
    }
    Assert.assertEquals(Arrays.toString(new IToolDescriptor[0]), Arrays.toString(toolsWithoutActions.toArray(new IToolDescriptor[toolsWithoutActions.size()])));
  }

  @Test
  public void localRepository_agent_any() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] agents = localRepo.findTool(TestToolConstant.TEAMCITY_AGENT_TOOL_ID, null, null, null);
    Assert.assertNotNull(agents);
    Assert.assertEquals(2, agents.length);
  }

  @Test
  public void localRepository_agent_linux() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] agents = localRepo.findTool(TestToolConstant.TEAMCITY_AGENT_TOOL_ID, "lin.*", null, null);
    Assert.assertNotNull(agents);
    Assert.assertEquals(1, agents.length);
    Assert.assertEquals("linux", agents[0].getPlatform());
  }

  @Test
  public void localRepository_agent_win() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] agents = localRepo.findTool(TestToolConstant.TEAMCITY_AGENT_TOOL_ID, "win", null, null);
    Assert.assertNotNull(agents);
    Assert.assertEquals(1, agents.length);
    Assert.assertEquals("win", agents[0].getPlatform());
  }

  @Test
  public void localRepository_agent_6_0_0_15772() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] agents = localRepo.findTool(TestToolConstant.TEAMCITY_AGENT_TOOL_ID, null, null, "6.0.0.15772");
    Assert.assertNotNull(agents);
    Assert.assertEquals(2, agents.length);
    Assert.assertEquals("6.0.0.15772", agents[0].getVersion());
    Assert.assertEquals("6.0.0.15772", agents[1].getVersion());
  }

  @Test
  public void localRepository_agent_linux_6_0_0_15772() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] agents = localRepo.findTool(TestToolConstant.TEAMCITY_AGENT_TOOL_ID, "linux", null, "6.0.0.15772");
    Assert.assertNotNull(agents);
    Assert.assertEquals(1, agents.length);
    Assert.assertEquals("linux", agents[0].getPlatform());
    Assert.assertEquals("6.0.0.15772", agents[0].getVersion());
  }

  //sun_jre

  @Test
  public void localRepository_sun_jre() throws Exception {
    final ToolRepository localRepo = new ToolRepository(getTestRepoUri());
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] sun_jre = localRepo.findTool(TestToolConstant.SUN_JRE_TOOL_ID, null, null, null);
    Assert.assertNotNull(sun_jre);
    Assert.assertEquals(5, sun_jre.length);
    final URI jre_locationURI = sun_jre[0].getLocationURI();
    Assert.assertNotNull(jre_locationURI);
    Assert.assertNull(jre_locationURI.getAuthority());
    Assert.assertNotNull(jre_locationURI.getPath());
  }

  @Test
  public void localRepository_sun_jdk() throws Exception {
    final ToolRepository localRepo = getBundleRepository();
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] sun_jdk = localRepo.findTool(ToolsConstants.SUN_JDK_TOOL_ID, null, null, null);
    Assert.assertNotNull(sun_jdk);
    Assert.assertEquals(9, sun_jdk.length);//todo: WRONG TEST!!!!
    for (IToolDescriptor jdk : sun_jdk) {
      System.out.println(jdk);
      System.out.println("\t" + Arrays.toString(jdk.getActions()));
    }
    final URI jdk_locationURI = sun_jdk[0].getLocationURI();
    Assert.assertNotNull(jdk_locationURI);
  }

  @Test
  public void localRepository_sun_jdk_paths() throws Exception {
    final ToolRepository localRepo = getBundleRepository();
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] sun_jdk = localRepo.findTool(ToolsConstants.SUN_JDK_TOOL_ID, null, null, null);
    Assert.assertNotNull(sun_jdk);
    Assert.assertTrue(sun_jdk.length > 0);
    final String[] paths = sun_jdk[0].getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(0, paths.length);
  }
  
  @Test
  public void localRepository_apache_ant_paths() throws Exception {
    final ToolRepository localRepo = getBundleRepository();
    Assert.assertNotNull(localRepo);
    final IToolDescriptor[] apache_ant = localRepo.findTool("apache.ant", null, null, null);
    Assert.assertNotNull(apache_ant);
    Assert.assertTrue(apache_ant.length > 0);
    final String[] paths = apache_ant[0].getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(1, paths.length);
  }
  

}
