package no.ssb.vtl.script.operations;

/*-
 * ========================LICENSE_START=================================
 * Java VTL
 * %%
 * Copyright (C) 2016 - 2017 Hadrien Kohl
 * %%
 * Licensed 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.
 * =========================LICENSE_END==================================
 */

import no.ssb.vtl.model.Component;
import no.ssb.vtl.model.Dataset;
import no.ssb.vtl.model.StaticDataset;
import no.ssb.vtl.model.VTLBoolean;
import no.ssb.vtl.model.VTLExpression;
import no.ssb.vtl.model.VTLObject;
import no.ssb.vtl.script.operations.join.ComponentBindings;
import org.junit.Before;
import org.junit.Test;

import javax.script.Bindings;

import static org.assertj.core.api.Assertions.assertThat;

public class FilterOperationTest {

    private Dataset dataset;
    private ComponentBindings componentBindings;

    private static VTLExpression NULL = new VTLExpression() {
        @Override
        public VTLObject resolve(Bindings bindings) {
            return VTLObject.of((VTLObject) null);
        }

        @Override
        public Class getVTLType() {
            return VTLBoolean.class;
        }
    };

    private static VTLExpression FALSE = new VTLExpression() {
        @Override
        public VTLObject resolve(Bindings bindings) {
            return VTLBoolean.of(false);
        }

        @Override
        public Class getVTLType() {
            return VTLBoolean.class;
        }
    };

    private static VTLExpression TRUE = new VTLExpression() {
        @Override
        public VTLObject resolve(Bindings bindings) {
            return VTLBoolean.of(true);
        }

        @Override
        public Class getVTLType() {
            return VTLBoolean.class;
        }
    };

    @Before
    public void setUp() {
        dataset = StaticDataset.create()
                .addComponent("id", Component.Role.IDENTIFIER, String.class)
                .addComponent("mc", Component.Role.MEASURE, String.class)
                .addPoints("idValue", null)
                .build();
        componentBindings = new ComponentBindings(dataset);
    }

    @Test
    public void testPredicateReturnsNull() {

        FilterOperation resultBooleanNull = new FilterOperation(dataset, NULL, componentBindings);
        assertThat(resultBooleanNull.getData()).isEmpty();
    }

    @Test
    public void testPredicateReturnsFalse() {

        FilterOperation result = new FilterOperation(dataset, FALSE, componentBindings);
        assertThat(result.getData()).isEmpty();
    }


    @Test
    public void testPredicateReturnsTrue() {

        FilterOperation result = new FilterOperation(dataset, TRUE, componentBindings);
        assertThat(result.getData()).isNotEmpty();
    }
}


@@ -356,17 +356,16 @@
 se at%0A *
- 
 %0A *     
@@ -410,17 +410,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -839,199 +839,9 @@
 del.
-Component;%0Aimport no.ssb.vtl.model.Dataset;%0Aimport no.ssb.vtl.model.StaticDataset;%0Aimport no.ssb.vtl.model.VTLBoolean;%0Aimport no.ssb.vtl.model.VTLExpression;%0Aimport no.ssb.vtl.model.VTLObject
+*
 ;%0Aim
@@ -898,16 +898,70 @@
 ndings;%0A
+import no.ssb.vtl.script.support.DatasetCloseWatcher;%0A
 import o
@@ -1032,16 +1032,49 @@
 dings;%0A%0A
+import java.util.stream.Stream;%0A%0A
 import s
@@ -1179,16 +1179,28 @@
  Dataset
+CloseWatcher
  dataset
@@ -2202,16 +2202,41 @@
 taset = 
+DatasetCloseWatcher.wrap(
 StaticDa
@@ -2469,16 +2469,17 @@
 .build()
+)
 ;%0A      
@@ -2590,25 +2590,24 @@
 rnsNull() %7B%0A
-%0A
         Filt
@@ -2705,462 +2705,894 @@
     
-assertThat(resultBooleanNull.getData()).isEmpty();%0A    %7D%0A%0A    @Test%0A    public void testPredicateReturnsFalse() %7B%0A%0A        FilterOperation result = new FilterOperation(dataset, FALSE, componentBindings);%0A        assertThat(result.getData()).isEmpty();%0A    %7D%0A%0A%0A    @Test%0A    public void testPredicateReturnsTrue() %7B%0A%0A        FilterOperation result = new FilterOperation(dataset, TRUE, componentBindings);%0A        assertThat(result.getData()).isNotEmpty();
+try (Stream%3CDataPoint%3E data = resultBooleanNull.getData()) %7B%0A            assertThat(data).isEmpty();%0A        %7D finally %7B%0A            assertThat(dataset.allStreamWereClosed()).isTrue();%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testPredicateReturnsFalse() %7B%0A%0A        FilterOperation result = new FilterOperation(dataset, FALSE, componentBindings);%0A        try (Stream%3CDataPoint%3E data = result.getData()) %7B%0A            assertThat(data).isEmpty();%0A        %7D finally %7B%0A            assertThat(dataset.allStreamWereClosed()).isTrue();%0A        %7D%0A    %7D%0A%0A%0A    @Test%0A    public void testPredicateReturnsTrue() %7B%0A%0A        FilterOperation result = new FilterOperation(dataset, TRUE, componentBindings);%0A        try (Stream%3CDataPoint%3E data = result.getData()) %7B%0A            assertThat(data).isNotEmpty();%0A        %7D finally %7B%0A            assertThat(dataset.allStreamWereClosed()).isTrue();%0A        %7D
 %0A   

Check that filter operation is closing the stream it uses