/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.gemstone.gemfire.management.internal;

import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import static org.junit.Assert.assertNotNull;

@Category(IntegrationTest.class)
public class AgentUtilJUnitTest {

  private AgentUtil agentUtil;
  private String version;

  @Before
  public void setUp() {
    version = getGemfireVersion();
    agentUtil = new AgentUtil(version);
  }

  @Test
  public void testRESTApiExists() {
    String gemFireWarLocation = agentUtil.findWarLocation("gemfire-web-api");
    assertNotNull("GemFire REST API WAR File was not found", gemFireWarLocation);
  }

  @Test
  public void testPulseWarExists() {
    String gemFireWarLocation = agentUtil.findWarLocation("gemfire-pulse");
    assertNotNull("Pulse WAR File was not found", gemFireWarLocation);
  }

  private String getGemfireVersion() {
    String version = null;

    Properties prop = new Properties();
    InputStream inputStream = null;
    String pathPrefix = null;
    try {
      pathPrefix = calculatePathPrefixToProjectRoot("gemfire-assembly/");
      inputStream = new FileInputStream(pathPrefix + "gradle.properties");
    } catch (FileNotFoundException e1) {
      try {
        pathPrefix = calculatePathPrefixToProjectRoot("gemfire-core/");
        inputStream = new FileInputStream(pathPrefix + "gradle.properties");
      } catch (FileNotFoundException e) {
      }
    }

    if (inputStream != null) {
      try {
        prop.load(inputStream);
        version = prop.getProperty("versionNumber") + prop.getProperty("releaseType");
      } catch (FileNotFoundException e) {
      } catch (IOException e) {
      }
    }
    return version;
  }

  private String calculatePathPrefixToProjectRoot(String subDirectory) {
    String pathPrefix = "";

    String currentDirectoryPath = new File(".").getAbsolutePath();
    int gemfireCoreLocationIx = currentDirectoryPath.indexOf(subDirectory);
    if (gemfireCoreLocationIx < 0) {
      return pathPrefix;
    }

    return currentDirectoryPath.substring(0, gemfireCoreLocationIx);
  }


}

@@ -847,16 +847,69 @@
 ernal;%0A%0A
+import com.gemstone.gemfire.internal.GemFireVersion;%0A
 import c
@@ -963,24 +963,24 @@
 rationTest;%0A
-
 import org.j
@@ -1071,185 +1071,8 @@
 y;%0A%0A
-import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Properties;%0A%0A
 impo
@@ -1288,23 +1288,38 @@
 rsion = 
+GemFireVersion.
 getGem
-f
+F
 ireVersi
@@ -1774,1266 +1774,11 @@
 n);%0A
+
   %7D%0A%0A
-  private String getGemfireVersion() %7B%0A    String version = null;%0A%0A    Properties prop = new Properties();%0A    InputStream inputStream = null;%0A    String pathPrefix = null;%0A    try %7B%0A      pathPrefix = calculatePathPrefixToProjectRoot(%22gemfire-assembly/%22);%0A      inputStream = new FileInputStream(pathPrefix + %22gradle.properties%22);%0A    %7D catch (FileNotFoundException e1) %7B%0A      try %7B%0A        pathPrefix = calculatePathPrefixToProjectRoot(%22gemfire-core/%22);%0A        inputStream = new FileInputStream(pathPrefix + %22gradle.properties%22);%0A      %7D catch (FileNotFoundException e) %7B%0A      %7D%0A    %7D%0A%0A    if (inputStream != null) %7B%0A      try %7B%0A        prop.load(inputStream);%0A        version = prop.getProperty(%22versionNumber%22) + prop.getProperty(%22releaseType%22);%0A      %7D catch (FileNotFoundException e) %7B%0A      %7D catch (IOException e) %7B%0A      %7D%0A    %7D%0A    return version;%0A  %7D%0A%0A  private String calculatePathPrefixToProjectRoot(String subDirectory) %7B%0A    String pathPrefix = %22%22;%0A%0A    String currentDirectoryPath = new File(%22.%22).getAbsolutePath();%0A    int gemfireCoreLocationIx = currentDirectoryPath.indexOf(subDirectory);%0A    if (gemfireCoreLocationIx %3C 0) %7B%0A      return pathPrefix;%0A    %7D%0A%0A    return currentDirectoryPath.substring(0, gemfireCoreLocationIx);%0A  %7D%0A%0A%0A
 %7D%0A

Standardize retrieving Geode version