/*
 * 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.installer.agent;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;

import jetbrains.buildServer.core.runtime.RuntimeUtil;
import jetbrains.buildServer.core.runtime.osgx.Configuration;
import jetbrains.buildServer.core.runtime.osgx.IBundle;
import jetbrains.buildServer.core.runtime.osgx.Version;
import jetbrains.buildServer.tools.AbstractToolingTestCase;
import jetbrains.buildServer.tools.ToolsConstants;
import junit.framework.Assert;

import org.junit.Test;

import com.intellij.openapi.util.io.StreamUtil;

public class ConfigurationTest extends AbstractToolingTestCase {

  @Test
  public void configuration_exists() throws Exception {
    final File registry = new File(getTestDataRoot(), ToolsInstallerAgentSupport.TEAMCITY_BUNLES_REPOSITORY);
    Assert.assertTrue(registry.exists());
  }

  @Test
  public void load_configuration() throws Exception {
    final File registry = new File(getTestDataRoot(), ToolsInstallerAgentSupport.TEAMCITY_BUNLES_REPOSITORY);
    final Configuration configuration = Configuration.fromStream(getTestDataRoot(), new FileInputStream(registry), RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(configuration);
  }

  @Test
  public void load_configuration_bundles() throws Exception {
    final File registry = new File(getTestDataRoot(), ToolsInstallerAgentSupport.TEAMCITY_BUNLES_REPOSITORY);
    final Configuration configuration = Configuration.fromStream(getTestDataRoot(), new FileInputStream(registry), RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(configuration);
    final IBundle[] bundles = configuration.getBundles();
    Assert.assertNotNull(bundles);
    Assert.assertEquals(2, bundles.length);
  }

  @Test
  public void load_configuration_bundle_jdk() throws Exception {
    final File registry = new File(getTestDataRoot(), ToolsInstallerAgentSupport.TEAMCITY_BUNLES_REPOSITORY);
    final Configuration configuration = Configuration.fromStream(getTestDataRoot(), new FileInputStream(registry), RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(configuration);
    final IBundle[] bundles = configuration.findBundle(ToolsConstants.SUN_JDK_TOOL_ID, null);
    Assert.assertNotNull(bundles);
    for (final IBundle bundle : bundles) {
      final File location = bundle.getLocation();
      Assert.assertNotNull(location);
      final Version version = bundle.getVersion();
      Assert.assertNotNull(version);
      final File[] paths = bundle.getPaths();
      Assert.assertNotNull(paths);
      Assert.assertEquals(0, paths.length);
      return;
    }
    Assert.assertTrue("Must not be here", false);
  }
  
  @Test
  public void load_configuration_bundle_ant() throws Exception {
    final File registry = new File(getTestDataRoot(), ToolsInstallerAgentSupport.TEAMCITY_BUNLES_REPOSITORY);
    final Configuration configuration = Configuration.fromStream(getTestDataRoot(), new FileInputStream(registry), RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(configuration);
    final IBundle[] bundles = configuration.findBundle("apache.ant", null);
    Assert.assertNotNull(bundles);
    for (final IBundle bundle : bundles) {
      final File location = bundle.getLocation();
      Assert.assertNotNull(location);
      final Version version = bundle.getVersion();
      Assert.assertNotNull(version);
      final File[] paths = bundle.getPaths();
      Assert.assertNotNull(paths);
      Assert.assertEquals(1, paths.length);
      return;
    }
    Assert.assertTrue("Must not be here", false);
  }

  @Test
  public void save_configuration() throws Exception {
    final File registry = new File(getTestDataRoot(), ToolsInstallerAgentSupport.TEAMCITY_BUNLES_REPOSITORY);
    final Configuration configuration = Configuration.fromStream(getTestDataRoot(), new FileInputStream(registry), RuntimeUtil.CONSOLE_MONITOR);
    //    Assert.assertNotNull(configuration);
    final IBundle[] originalBundles = configuration.getBundles();
    Assert.assertNotNull(originalBundles);
    Assert.assertTrue(originalBundles.length > 0);
    //serialize/deserialize
    final String content = StreamUtil.readText(configuration.toStream());
    Assert.assertNotNull(content);
    Assert.assertNotSame("", content.trim());
    //check content 
    final Configuration restored = Configuration.fromStream(getTestDataRoot(), new ByteArrayInputStream(content.trim().getBytes()), RuntimeUtil.CONSOLE_MONITOR);
    Assert.assertNotNull(restored);
    IBundle[] restoredBundles = restored.getBundles();
    Assert.assertNotNull(restoredBundles);
    Assert.assertEquals(originalBundles.length, restoredBundles.length);
    //check description 
    Assert.assertNotNull(restoredBundles[0].getDescription());
    Assert.assertNotSame("", restoredBundles[0].getDescription());
  }

}
