/*
 * 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 jetbrains.buildServer.core.runtime.Platform;
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.Version;
import junit.framework.Assert;

import org.junit.Test;

public class ToolRepositoryTest extends AbstractToolingTestCase {

  @Test
  public void bundled_repository_loading() throws Exception {
    final ToolRepository bundleRepo = getBundleRepository();
    Assert.assertNotNull(bundleRepo);
  }

  @Test
  public void bundled_repository_jdk_latest() throws Exception {
    final ToolRepository bundleRepo = getBundleRepository();
    IToolDescriptor[] jdk = bundleRepo.findTool(ToolsConstants.SUN_JDK_TOOL_ID, null, null, Version.LATEST);
    Assert.assertNotNull(jdk);
    Assert.assertTrue(jdk.length == 3);
  }

//  @Test
//  public void bundled_repository_teamcity_agent() throws Exception {
//    final ToolRepository bundleRepo = getBundleRepository();
//    IToolDescriptor[] agents = bundleRepo.findTool(ToolsConstants.JETBRAINS_TEAMCITY_AGENT_TOOL_ID, null, null, null);
//    Assert.assertNotNull(agents);
//    Assert.assertTrue(agents.length == 2);
//  }

//  @Test
//  public void bundled_repository_teamcity_agent_win() throws Exception {
//    final ToolRepository bundleRepo = getBundleRepository();
//    IToolDescriptor[] agents = bundleRepo.findTool(ToolsConstants.JETBRAINS_TEAMCITY_AGENT_TOOL_ID, Platform.WIN.getOs(), null, null);
//    Assert.assertNotNull(agents);
//    Assert.assertTrue(agents.length == 1);
//  }

//  @Test
//  public void bundled_repository_teamcity_agent_linux() throws Exception {
//    final ToolRepository bundleRepo = getBundleRepository();
//    IToolDescriptor[] agents = bundleRepo.findTool(ToolsConstants.JETBRAINS_TEAMCITY_AGENT_TOOL_ID, Platform.LINUX.getOs(), null, null);
//    Assert.assertNotNull(agents);
//    Assert.assertTrue(agents.length == 1);
//  }

//  @Test
//  public void bundled_repository_teamcity_agent_solaris() throws Exception {
//    final ToolRepository bundleRepo = getBundleRepository();
//    IToolDescriptor[] agents = bundleRepo.findTool(ToolsConstants.JETBRAINS_TEAMCITY_AGENT_TOOL_ID, Platform.SOLARIS.getOs(), null, null);
//    Assert.assertNotNull(agents);
//    Assert.assertTrue(agents.length == 1);
//  }

//  @Test
//  public void bundled_repository_teamcity_agent_mac() throws Exception {
//    final ToolRepository bundleRepo = getBundleRepository();
//    IToolDescriptor[] agents = bundleRepo.findTool(ToolsConstants.JETBRAINS_TEAMCITY_AGENT_TOOL_ID, Platform.MAC.getOs(), null, null);
//    Assert.assertNotNull(agents);
//    Assert.assertTrue(agents.length == 1);
//  }

  @Test
  public void merge_repository_add_new_tools() throws Exception {
    final ToolRepository bundleRepo = getBundleRepository();
    final IToolDescriptor[] bundledTools = bundleRepo.tools(RuntimeUtil.CONSOLE_MONITOR);
    final ToolRepository newToolsRepo = new ToolRepository(new URI(String.format("%s/merge/%s", getTestRepoUri(), "merge.tools.manifest.xml")));
    Assert.assertNotNull(newToolsRepo);
    final IToolDescriptor[] newTools = newToolsRepo.tools(RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(newTools);
    Assert.assertTrue(newTools.length > 0);
    int bundledToolsCount = bundledTools.length;
    final ToolRepository mergedRepo = bundleRepo.merge(newToolsRepo, RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(mergedRepo);
    Assert.assertEquals(bundledToolsCount + newTools.length, mergedRepo.tools(RuntimeUtil.CONSOLE_MONITOR).length);
  }

  @Test
  public void merge_repository_full_replace() throws Exception {
    final ToolRepository bundleRepo = new ToolRepository(new URI(String.format("%s/merge/%s", getTestRepoUri(), "merge.sun.jre.tools.manifest.xml")));
    final ToolRepository newToolsRepo = new ToolRepository(new URI(String.format("%s/merge/%s", getTestRepoUri(), "merge.sun.jre.tools.manifest.xml")));
    Assert.assertNotNull(newToolsRepo);
    final IToolDescriptor[] newTools = newToolsRepo.tools(RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(newTools);
    Assert.assertTrue(newTools.length > 0);
    final ToolRepository mergedRepo = bundleRepo.merge(newToolsRepo, RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(mergedRepo);
    Assert.assertEquals(bundleRepo.tools(RuntimeUtil.CONSOLE_MONITOR).length, mergedRepo.tools(RuntimeUtil.CONSOLE_MONITOR).length);
  }

  @Test
  public void merge_repository_add_new_versions() throws Exception {
    final ToolRepository bundleRepo = new ToolRepository(new URI(String.format("%s/merge/%s", getTestRepoUri(), "merge.sun.jre.tools.manifest.xml")));
    final ToolRepository newToolsRepo = new ToolRepository(new URI(String.format("%s/merge/%s", getTestRepoUri(), "merge.sun.jre.new.version.manifest.xml")));
    Assert.assertNotNull(newToolsRepo);
    final IToolDescriptor[] newTools = newToolsRepo.tools(RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(newTools);
    Assert.assertTrue(newTools.length > 0);
    final IToolDescriptor[] bundledTools = bundleRepo.tools(RuntimeUtil.CONSOLE_MONITOR);
    final ToolRepository mergedRepo = bundleRepo.merge(newToolsRepo, RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(mergedRepo);
    Assert.assertEquals(bundledTools.length + newTools.length, mergedRepo.tools(RuntimeUtil.CONSOLE_MONITOR).length);
  }

}
