/*
 *  ******************************************************************************
 *  *
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Apache License, Version 2.0 which is available at
 *  * https://www.apache.org/licenses/LICENSE-2.0.
 *  *
 *  *  See the NOTICE file distributed with this work for additional
 *  *  information regarding copyright ownership.
 *  * 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.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  *****************************************************************************
 */

package net.brutex.spark;

import lombok.extern.slf4j.Slf4j;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

import java.io.Serializable;

@Slf4j
public abstract class BaseSparkSessionTest implements Serializable {
    private static SparkSession spark;

    public static SparkSession getSession() {
        SparkConf sparkConf = new SparkConf()
                .setMaster("spark://10.5.5.200:7077")
                .setAppName(BaseSparkSessionTest.class.getSimpleName())
                .set("spark.driver.bindAddress", "10.5.5.145")
                .set("spark.network.timeout", "240000")
                .set("spark.driver.host", "10.5.5.145")
                .set("spark.deploy.mode", "client")
                .set("spark.executor.memory", "4g")
                .set("spark.cores.max", "4")
                .set("spark.worker.cleanup.enabled", "true")
                .set("spark.driver.extraJavaOptions", "-Dlog4j.configurationFile=log4j2.xml")
                .set("spark.executor.extraJavaOptions", "-Dlog4j.configurationFile=log4j2.xml")
                .set("spark.hadoop.fs.defaultFS", "hdfs://10.5.5.200:9000");

        spark = SparkSession.builder()
                .config(sparkConf)
                .getOrCreate();

        return spark;
    }

    @BeforeAll
    public static void beforeAll() {

    }

    @AfterAll
    public static synchronized void afterAll() {
        getSession().close();

    }
}
