<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) Open Application Platform Authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package oap.template;

import lombok.AllArgsConstructor;
import lombok.val;
import oap.io.Files;
import oap.template.StringTemplateTest.Tst.Test1;
import oap.template.StringTemplateTest.Tst.Test2;
import oap.template.StringTemplateTest.Tst.Test3;
import oap.template.StringTemplateTest.Tst.Test4;
import oap.testng.AbstractTest;
import oap.testng.Env;
import oap.util.Lists;
import oap.util.Maps;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Created by igor.petrenko on 15.06.2017.
 */
public class StringTemplateTest extends AbstractTest {
    private Engine engine;
    private Path test;

    @BeforeMethod
    @Override
    public void beforeMethod() throws Exception {
        super.beforeMethod();

        test = Env.tmpPath( "test" );
        Files.ensureDirectory( test );
        engine = new Engine( test );
    }

    @Test
    public void testTtl() {
        final String clazz = Engine.getName( "test" );
        val template = engine.getTemplate( clazz, EngineTest.Test1.class, "test${tst.test2.i}" );

        template.renderString( new EngineTest.Test1() );
        engine.run();

        assertThat( test.resolve( "oap.template." + clazz + ".java" ) ).exists();
        assertThat( test.resolve( "oap.template." + clazz + ".class" ) ).exists();

        Files.setLastModifiedTime( test.resolve( "oap.template." + clazz + ".java" ), System.currentTimeMillis() - engine.ttl - 100 );
        Files.setLastModifiedTime( test.resolve( "oap.template." + clazz + ".class" ), System.currentTimeMillis() - engine.ttl - 100 );

        engine.run();

        assertThat( test.resolve( "oap.template." + clazz + ".java" ) ).doesNotExist();
        assertThat( test.resolve( "oap.template." + clazz + ".class" ) ).doesNotExist();
    }

    @Test
    public void testLoadFromDisk() {
        final String clazz = Engine.getName( "test" );
        val template = engine.getTemplate( clazz, EngineTest.Test1.class, "test${tst.test2.i}" );

        template.renderString( new EngineTest.Test1() );

        Engine engine2 = new Engine( test );

        val template2 = engine2.getTemplate( clazz, EngineTest.Test1.class, "test${tst.test2.i}" );
        template2.renderString( new EngineTest.Test1() );
    }

    @Test
    public void testProcessWithoutVariables() throws Exception {
        assertThat( engine.getTemplate( "test", Container.class, "d" ) )
            .isExactlyInstanceOf( ConstTemplate.class );
        assertThat( engine.getTemplate( "test", Container.class, "d" )
            .renderString( new Container( new Tst() ) ) ).isEqualTo( "d" );
    }

    @Test
    public void testDepth() {
        Tst test = new Tst();
        Test1 test1 = new Test1( "a i/d" );
        test.test1 = Optional.of( test1 );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode(0)}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a i/d" );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a i/d" );

        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode(1)}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a+i%2Fd" );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode( 1) }" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a+i%2Fd" );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode(2)}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a%2Bi%252Fd" );
    }

    @Test
    public void testMap() {
        assertThat( engine.getTemplate( "tmp", Map.class, "id=${tst.test2.id}" )
            .renderString( Maps.of2( "tst", Maps.of2( "test2", "a i/d" ) ) ) ).isEqualTo( "id=a i/d" );
    }


    @Test
    public void testOtherJoinStrategy() {
        Tst test = new Tst();
        Test4 test4 = new Test4( 320, 50 );
        test.test4 = Optional.of( test4 );

        val template = engine.getTemplate( "tmp", Container.class,
            Lists.of( new JavaCTemplate.Line( "WaH", "tst.test4.{a,\"xx\",b}", "" ) ), "œ", new JoinAsSingleTemplateStrategy() );

        InvocationAccumulator invAccumulator = new InvocationAccumulator();

        template.render( new Container( test ), invAccumulator );
        assertThat( invAccumulator.get() ).isEqualTo( 1 );
        assertThat( invAccumulator.build() ).isEqualTo( "320xx50" );

    }

    @Test
    public void testAlternatives() {
        Tst test = new Tst();
        Test1 test1 = new Test1( "aid" );
        test.test1 = Optional.of( test1 );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=aid" );
<commit_msg>fix: tests<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) Open Application Platform Authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package oap.template;

import lombok.AllArgsConstructor;
import lombok.val;
import oap.io.Files;
import oap.template.StringTemplateTest.Tst.Test1;
import oap.template.StringTemplateTest.Tst.Test2;
import oap.template.StringTemplateTest.Tst.Test3;
import oap.template.StringTemplateTest.Tst.Test4;
import oap.testng.AbstractTest;
import oap.testng.Env;
import oap.util.Lists;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Created by igor.petrenko on 15.06.2017.
 */
public class StringTemplateTest extends AbstractTest {
    private Engine engine;
    private Path test;

    @BeforeMethod
    @Override
    public void beforeMethod() throws Exception {
        super.beforeMethod();

        test = Env.tmpPath( "test" );
        Files.ensureDirectory( test );
        engine = new Engine( test );
    }

    @Test
    public void testTtl() {
        final String clazz = Engine.getName( "test" );
        val template = engine.getTemplate( clazz, EngineTest.Test1.class, "test${tst.test2.i}" );

        template.renderString( new EngineTest.Test1() );
        engine.run();

        assertThat( test.resolve( "oap.template." + clazz + ".java" ) ).exists();
        assertThat( test.resolve( "oap.template." + clazz + ".class" ) ).exists();

        Files.setLastModifiedTime( test.resolve( "oap.template." + clazz + ".java" ), System.currentTimeMillis() - engine.ttl - 100 );
        Files.setLastModifiedTime( test.resolve( "oap.template." + clazz + ".class" ), System.currentTimeMillis() - engine.ttl - 100 );

        engine.run();

        assertThat( test.resolve( "oap.template." + clazz + ".java" ) ).doesNotExist();
        assertThat( test.resolve( "oap.template." + clazz + ".class" ) ).doesNotExist();
    }

    @Test
    public void testLoadFromDisk() {
        final String clazz = Engine.getName( "test" );
        val template = engine.getTemplate( clazz, EngineTest.Test1.class, "test${tst.test2.i}" );

        template.renderString( new EngineTest.Test1() );

        Engine engine2 = new Engine( test );

        val template2 = engine2.getTemplate( clazz, EngineTest.Test1.class, "test${tst.test2.i}" );
        template2.renderString( new EngineTest.Test1() );
    }

    @Test
    public void testProcessWithoutVariables() throws Exception {
        assertThat( engine.getTemplate( "test", Container.class, "d" ) )
            .isExactlyInstanceOf( ConstTemplate.class );
        assertThat( engine.getTemplate( "test", Container.class, "d" )
            .renderString( new Container( new Tst() ) ) ).isEqualTo( "d" );
    }

    @Test
    public void testDepth() {
        Tst test = new Tst();
        Test1 test1 = new Test1( "a i/d" );
        test.test1 = Optional.of( test1 );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode(0)}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a i/d" );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a i/d" );

        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode(1)}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a+i%2Fd" );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode( 1) }" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a+i%2Fd" );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id ; urlencode(2)}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=a%2Bi%252Fd" );
    }

    @Test
    public void testOtherJoinStrategy() {
        Tst test = new Tst();
        Test4 test4 = new Test4( 320, 50 );
        test.test4 = Optional.of( test4 );

        val template = engine.getTemplate( "tmp", Container.class,
            Lists.of( new JavaCTemplate.Line( "WaH", "tst.test4.{a,\"xx\",b}", "" ) ), "œ", new JoinAsSingleTemplateStrategy() );

        InvocationAccumulator invAccumulator = new InvocationAccumulator();

        template.render( new Container( test ), invAccumulator );
        assertThat( invAccumulator.get() ).isEqualTo( 1 );
        assertThat( invAccumulator.build() ).isEqualTo( "320xx50" );

    }

    @Test
    public void testAlternatives() {
        Tst test = new Tst();
        Test1 test1 = new Test1( "aid" );
        test.test1 = Optional.of( test1 );
        assertThat( engine.getTemplate( "tmp", Container.class, "id=${tst.test2.id | tst.test1.id}" )
            .renderString( new Container( test ) ) ).isEqualTo( "id=aid" );

{'fraction_non_alphanumeric': 0.13122605363984674, 'fraction_numerical': 0.013090676883780333, 'mean_word_length': 3.140449438202247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2768294', 'n_tokens_mistral': 3876, 'n_tokens_neox': 3795, 'n_words': 1068}