/**
 * 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 org.apache.pulsar.io.flume.node;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

public final class TestEnvVarResolverProperties {
    private static final File TESTFILE = new File(
            TestEnvVarResolverProperties.class.getClassLoader()
                    .getResource("flume-conf-with-envvars.properties").getFile());

    private PropertiesFileConfigurationProvider provider;

    @Before
    public void setUp() {
        provider = new PropertiesFileConfigurationProvider("a1", TESTFILE);
    }

    @Test
    public void resolveEnvVar() {
        injectEnvironmentVariable("VARNAME", "varvalue");
        String resolved = EnvVarResolverProperties.resolveEnvVars("padding ${VARNAME} padding");
        Assert.assertEquals("padding varvalue padding", resolved);
    }

    @Test
    public void resolveEnvVars() {
        injectEnvironmentVariable("VARNAME1", "varvalue1");
        injectEnvironmentVariable("VARNAME2", "varvalue2");
        String resolved = EnvVarResolverProperties
                .resolveEnvVars("padding ${VARNAME1} ${VARNAME2} padding");
        Assert.assertEquals("padding varvalue1 varvalue2 padding", resolved);
    }

    @Test
    public void getProperty() {
        String NC_PORT = "6667";
        injectEnvironmentVariable("NC_PORT", NC_PORT);
        System.setProperty("propertiesImplementation",
                "org.apache.pulsar.io.flume.node.EnvVarResolverProperties");

        Assert.assertEquals(NC_PORT, provider.getFlumeConfiguration()
                .getConfigurationFor("a1")
                .getSourceContext().get("r1").getParameters().get("port"));
    }

    @SneakyThrows
    private static void injectEnvironmentVariable(String key, String value) {

        Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");
        Map<String,String> unmodifiableMap = new HashMap<>(Whitebox
                .getInternalState(processEnvironment, "theUnmodifiableEnvironment"));
        unmodifiableMap.put(key, value);
        Whitebox.setInternalState(processEnvironment, "theUnmodifiableEnvironment", unmodifiableMap);

        Map<String,String> envMap = new HashMap<>(Whitebox
                .getInternalState(processEnvironment, "theEnvironment"));
        envMap.put(key, value);
        Whitebox.setInternalState(processEnvironment, "theEnvironment", envMap);
    }
}

@@ -844,16 +844,62 @@
 .node;%0A%0A
+import static org.testng.Assert.assertEquals;%0A
 import j
@@ -959,17 +959,16 @@
 il.Map;%0A
-%0A
 import l
@@ -1002,45 +1002,78 @@
 org.
-junit.Assert;%0Aimport org.junit
+powermock.reflect.Whitebox;%0Aimport org.testng.annotations
 .Before
+Method
 ;%0Aim
@@ -1085,57 +1085,31 @@
 org.
-junit.Test;%0Aimport org.powermock.reflect.Whitebox
+testng.annotations.Test
 ;%0A%0Ap
@@ -1426,16 +1426,40 @@
  @Before
+Method(alwaysRun = true)
 %0A    pub
@@ -1759,39 +1759,32 @@
 ding%22);%0A        
-Assert.
 assertEquals(%22pa
@@ -1772,32 +1772,42 @@
    assertEquals(
+resolved, 
 %22padding varvalu
@@ -1812,34 +1812,24 @@
 lue padding%22
-, resolved
 );%0A    %7D%0A%0A  
@@ -2118,39 +2118,32 @@
 ding%22);%0A        
-Assert.
 assertEquals(%22pa
@@ -2139,16 +2139,26 @@
 tEquals(
+resolved, 
 %22padding
@@ -2190,18 +2190,8 @@
 ing%22
-, resolved
 );%0A 
@@ -2467,23 +2467,16 @@
         
-Assert.
 assertEq
@@ -2484,17 +2484,8 @@
 als(
-NC_PORT, 
 prov
@@ -2629,16 +2629,25 @@
 (%22port%22)
+, NC_PORT
 );%0A    %7D

Use TestNG instead of JUnit (#15580)