// Copyright 2020 The Bazel Authors. All rights reserved.
//
// 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 build.buildfarm.common;

import build.buildfarm.v1test.BuildFarmServerConfig;
import build.buildfarm.v1test.ShardWorkerConfig;
import build.buildfarm.v1test.WorkerConfig;
import com.google.protobuf.TextFormat;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

// Test that example config files can load properly
@RunWith(JUnit4.class)
public class ExampleConfigsTest {

  @Before
  public void skipWindows() {
    org.junit.Assume.assumeFalse(System.getProperty("os.name").contains("Win"));
  }

  @Test
  public void workerConfig() throws IOException {

    // parse text into protobuf
    Path configPath = Paths.get("examples", "worker.config.example");
    try (InputStream configInputStream = Files.newInputStream(configPath)) {
      WorkerConfig.Builder builder = WorkerConfig.newBuilder();
      TextFormat.merge(new InputStreamReader(configInputStream), builder);
      builder.build();
    }
  }

  @Test
  public void serverConfig() throws IOException {

    // parse text into protobuf
    Path configPath = Paths.get("examples", "server.config.example");
    try (InputStream configInputStream = Files.newInputStream(configPath)) {
      BuildFarmServerConfig.Builder builder = BuildFarmServerConfig.newBuilder();
      TextFormat.merge(new InputStreamReader(configInputStream), builder);
      builder.build();
    }
  }

  @Test
  public void shardWorkerConfig() throws IOException {

    // parse text into protobuf
    Path configPath = Paths.get("examples", "shard-worker.config.example");
    try (InputStream configInputStream = Files.newInputStream(configPath)) {
      ShardWorkerConfig.Builder builder = ShardWorkerConfig.newBuilder();
      TextFormat.merge(new InputStreamReader(configInputStream), builder);
      builder.build();
    }
  }

  @Test
  public void shardServerConfig() throws IOException {

    // parse text into protobuf
    Path configPath = Paths.get("examples", "shard-server.config.example");
    try (InputStream configInputStream = Files.newInputStream(configPath)) {
      BuildFarmServerConfig.Builder builder = BuildFarmServerConfig.newBuilder();
      TextFormat.merge(new InputStreamReader(configInputStream), builder);
      builder.build();
    }
  }
}

@@ -1455,32 +1455,40 @@
 ath configPath =
+%0A       
  Paths.get(%22exam
@@ -1474,32 +1474,94 @@
       Paths.get(
+%0A            System.getenv(%22TEST_SRCDIR%22), %22build_buildfarm%22, 
 %22examples%22, %22wor
@@ -1936,32 +1936,40 @@
 ath configPath =
+%0A       
  Paths.get(%22exam
@@ -1955,32 +1955,94 @@
       Paths.get(
+%0A            System.getenv(%22TEST_SRCDIR%22), %22build_buildfarm%22, 
 %22examples%22, %22ser
@@ -2440,32 +2440,40 @@
 ath configPath =
+%0A       
  Paths.get(%22exam
@@ -2463,35 +2463,133 @@
   Paths.get(
-%22examples%22,
+%0A            System.getenv(%22TEST_SRCDIR%22),%0A            %22build_buildfarm%22,%0A            %22examples%22,%0A           
  %22shard-work
@@ -2986,16 +2986,24 @@
 igPath =
+%0A       
  Paths.g
@@ -3005,27 +3005,125 @@
 ths.get(
-%22examples%22,
+%0A            System.getenv(%22TEST_SRCDIR%22),%0A            %22build_buildfarm%22,%0A            %22examples%22,%0A           
  %22shard-

correct path when testing configs (#563)