/*
 * Copyright 2019 Google LLC.
 *
 * 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 com.google.cloud.tools.opensource.dashboard;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.eclipse.aether.artifact.Artifact;
import org.junit.Assert;
import org.junit.Test;
import com.google.cloud.tools.opensource.dependencies.Bom;
import com.google.cloud.tools.opensource.dependencies.MavenRepositoryException;

public class BomTest {

  private static final Path CLOUD_OSS_BOM_PATH =
      Paths.get("..", "boms", "cloud-oss-bom", "pom.xml").toAbsolutePath();  
  
  @Test
  public void testArtifactsExist()
      throws IOException, MavenRepositoryException {
    List<Artifact> artifacts =
        Bom.readBom(CLOUD_OSS_BOM_PATH).getManagedDependencies();
    for (Artifact artifact : artifacts) {
      assertReachable(buildMavenCentralUrl(artifact));
    }
  }

  private static String buildMavenCentralUrl(Artifact artifact) {
    return "https://repo1.maven.org/maven2/"
        + artifact.getGroupId().replace('.', '/')
        + "/"
        + artifact.getArtifactId()
        + "/"
        + artifact.getVersion()
        + "/";
  }

  private static void assertReachable(String url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestMethod("HEAD");
    try {
      Assert.assertEquals(
          "Could not reach " + url, HttpURLConnection.HTTP_OK, connection.getResponseCode());
    } catch (IOException ex) {
      Assert.fail("Could not reach " + url + "\n" + ex.getMessage());
    }
  }
}

@@ -1066,63 +1066,117 @@
 t %7B%0A
-%0A  private static final Path CLOUD_OSS_BOM_PATH =%0A     
+  %0A  @Test%0A  public void testLtsBom()%0A      throws IOException, MavenRepositoryException %7B%0A    Path bomPath =
  Pat
@@ -1203,18 +1203,18 @@
  %22cloud-
-os
+lt
 s-bom%22, 
@@ -1244,16 +1244,43 @@
 th();  %0A
+    checkBom(bomPath);%0A  %7D%0A
   %0A  @Te
@@ -1304,22 +1304,20 @@
 test
-ArtifactsExist
+LibrariesBom
 ()%0A 
@@ -1364,24 +1364,228 @@
 Exception %7B%0A
+    Path bomPath = Paths.get(%22..%22, %22boms%22, %22cloud-oss-bom%22, %22pom.xml%22).toAbsolutePath();  %0A    checkBom(bomPath);%0A  %7D%0A%0A  private void checkBom(Path bomPath) throws MavenRepositoryException, IOException %7B%0A
     List%3CArt
@@ -1602,24 +1602,16 @@
 ifacts =
-%0A       
  Bom.rea
@@ -1619,26 +1619,15 @@
 Bom(
-CLOUD_OSS_BOM_PATH
+bomPath
 ).ge

test the LTS BOM (#2039)