/*
 * Copyright © 2021 by Data Virtuality GmbH
 */
package com.datavirtuality.dv.core.interfaces;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Roman Pedchenko
 * @date 2021.09.24
 */
public class DVPropertyHolderTest {
    public static final String TEST = "com.dv.test";

    @AfterEach
    public void tearDown() throws Exception {
        System.clearProperty(TEST);
    }

    @Test
    public void propSubstitution() {
        System.setProperty(TEST, "val2");
        final DVPropertyHolder ph = DVPropertyHolder.getInstance();
        Properties p = new Properties();
        p.setProperty("com.dv.prop1", "val1");
        p.setProperty("com.dv.prop2", "${com.dv.test}");
        p.setProperty("com.dv.prop3", "${com.dv.test1:val3}");
        ph.add(TEST, p);
        final Properties p1 = ph.get(TEST);
        assertNotNull(p1);
        assertEquals("val1", p1.getProperty("com.dv.prop1"));
        assertEquals("val2", p1.getProperty("com.dv.prop2"));
        assertEquals("val3", p1.getProperty("com.dv.prop3"));
    }

    @Test
    public void getQuartzInt() {
        System.setProperty(TEST, "15");
        final DVPropertyHolder ph = DVPropertyHolder.getInstance();
        Properties p = new Properties();
        p.setProperty("com.dv.prop1", "${com.dv.test:10}");
        p.setProperty("com.dv.prop2", "${com.dv.test1:2}");
        p.setProperty("com.dv.prop3", "easy peasy lemon squeezy");
        ph.add(DVPropertyHolder.Type.QUARTZ, p);
        assertEquals(15, ph.getInt(DVPropertyHolder.Type.QUARTZ, "com.dv.prop1", -1));
        assertEquals(2, ph.getInt(DVPropertyHolder.Type.QUARTZ, "com.dv.prop2", -1));
        assertEquals(-1, ph.getInt(DVPropertyHolder.Type.QUARTZ, "com.dv.prop3", -1));
        assertEquals(-1, ph.getInt(DVPropertyHolder.Type.QUARTZ, "com.dv.prop4", -1));
    }

    @Test
    public void getLdapValues() {
        System.setProperty(TEST, "5");
        final DVPropertyHolder ph = DVPropertyHolder.getInstance();
        Properties p = new Properties();
        p.setProperty("com.dv.prop1", "${com.dv.test:10}");
        p.setProperty("com.dv.prop2", "${com.dv.test1:2}");
        p.setProperty("com.dv.prop3", "a value");
        ph.add(DVPropertyHolder.Type.LDAP, p);
        assertEquals(5, ph.getInt(DVPropertyHolder.Type.LDAP, "com.dv.prop1", -1));
        assertEquals(2, ph.getInt(DVPropertyHolder.Type.LDAP, "com.dv.prop2", -1));
        assertEquals("a value", ph.getString(DVPropertyHolder.Type.LDAP, "com.dv.prop3", "nothing"));
    }
}
