<commit_before>/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.index.fielddata;

import org.elasticsearch.index.fielddata.ScriptDocValues.Longs;
import org.elasticsearch.test.ESTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.ReadableDateTime;

import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.containsInAnyOrder;

public class ScriptDocValuesLongsTests extends ESTestCase {
    public void testLongs() throws IOException {
        long[][] values = new long[between(3, 10)][];
        for (int d = 0; d < values.length; d++) {
            values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
            for (int i = 0; i < values[d].length; i++) {
                values[d][i] = randomLong();
            }
        }
        Longs longs = wrap(values, deprecationMessage -> {fail("unexpected deprecation: " + deprecationMessage);});

        for (int round = 0; round < 10; round++) {
            int d = between(0, values.length - 1);
            longs.setNextDocId(d);
            if (values[d].length > 0) {
                assertEquals(values[d][0], longs.getValue());
            } else {
                Exception e = expectThrows(IllegalStateException.class, () -> longs.getValue());
                assertEquals("A document doesn't have a value for a field! " +
                    "Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
            }
            assertEquals(values[d].length, longs.size());
            assertEquals(values[d].length, longs.getValues().size());
            for (int i = 0; i < values[d].length; i++) {
                assertEquals(values[d][i], longs.get(i).longValue());
                assertEquals(values[d][i], longs.getValues().get(i).longValue());
            }

            Exception e = expectThrows(UnsupportedOperationException.class, () -> longs.getValues().add(100L));
            assertEquals("doc values are unmodifiable", e.getMessage());
        }
    }

    public void testDates() throws IOException {
        long[][] values = new long[between(3, 10)][];
        ReadableDateTime[][] dates = new ReadableDateTime[values.length][];
        for (int d = 0; d < values.length; d++) {
            values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
            dates[d] = new ReadableDateTime[values[d].length];
            for (int i = 0; i < values[d].length; i++) {
                dates[d][i] = new DateTime(randomNonNegativeLong(), DateTimeZone.UTC);
                values[d][i] = dates[d][i].getMillis();
            }
        }
        Set<String> warnings = new HashSet<>();
        Longs longs = wrap(values, deprecationMessage -> {
            warnings.add(deprecationMessage);
            /* Create a temporary directory to prove we are running with the
             * server's permissions. */
            createTempDir();
        });

        for (int round = 0; round < 10; round++) {
            int d = between(0, values.length - 1);
            longs.setNextDocId(d);
            if (dates[d].length > 0) {
                assertEquals(dates[d][0], longs.getDate());
            } else {
                Exception e = expectThrows(IllegalStateException.class, () -> longs.getDate());
                assertEquals("A document doesn't have a value for a field! " +
                    "Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
            }

            assertEquals(values[d].length, longs.getDates().size());
            for (int i = 0; i < values[d].length; i++) {
                assertEquals(dates[d][i], longs.getDates().get(i));
            }

            Exception e = expectThrows(UnsupportedOperationException.class, () -> longs.getDates().add(new DateTime()));
            assertEquals("doc values are unmodifiable", e.getMessage());
        }

        /*
         * Invoke getDates without any privileges to verify that
         * it still works without any. In particularly, this
         * verifies that the callback that we've configured
         * above works. That callback creates a temporary
         * directory which is not possible with "noPermissions".
         */
        PermissionCollection noPermissions = new Permissions();
        AccessControlContext noPermissionsAcc = new AccessControlContext(
            new ProtectionDomain[] {
                new ProtectionDomain(null, noPermissions)
            }
        );
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                try {
                    longs.getDates();
                } catch (IOException e) {
                    throw new RuntimeException("unexpected", e);
                }
                return null;
            }
        }, noPermissionsAcc);

        assertThat(warnings, containsInAnyOrder(
                "getDate on numeric fields is deprecated. Use a date field to get dates.",
                "getDates on numeric fields is deprecated. Use a date field to get dates.",
                "The joda time api for doc values is deprecated. Use -Des.scripting.use_java_time=true" +
                    " to use the java time api for date field doc values"));
    }

    private Longs wrap(long[][] values, Consumer<String> deprecationCallback) {
        return new Longs(new AbstractSortedNumericDocValues() {
            long[] current;<commit_msg>Fix scriptdocvalues tests with dates<commit_after>/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.index.fielddata;

import org.elasticsearch.index.fielddata.ScriptDocValues.Longs;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.containsInAnyOrder;

public class ScriptDocValuesLongsTests extends ESTestCase {
    public void testLongs() throws IOException {
        long[][] values = new long[between(3, 10)][];
        for (int d = 0; d < values.length; d++) {
            values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
            for (int i = 0; i < values[d].length; i++) {
                values[d][i] = randomLong();
            }
        }
        Longs longs = wrap(values, deprecationMessage -> {fail("unexpected deprecation: " + deprecationMessage);});

        for (int round = 0; round < 10; round++) {
            int d = between(0, values.length - 1);
            longs.setNextDocId(d);
            if (values[d].length > 0) {
                assertEquals(values[d][0], longs.getValue());
            } else {
                Exception e = expectThrows(IllegalStateException.class, () -> longs.getValue());
                assertEquals("A document doesn't have a value for a field! " +
                    "Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
            }
            assertEquals(values[d].length, longs.size());
            assertEquals(values[d].length, longs.getValues().size());
            for (int i = 0; i < values[d].length; i++) {
                assertEquals(values[d][i], longs.get(i).longValue());
                assertEquals(values[d][i], longs.getValues().get(i).longValue());
            }

            Exception e = expectThrows(UnsupportedOperationException.class, () -> longs.getValues().add(100L));
            assertEquals("doc values are unmodifiable", e.getMessage());
        }
    }

    public void testDates() throws IOException {
        long[][] values = new long[between(3, 10)][];
        Object[][] dates = new Object[values.length][];
        for (int d = 0; d < values.length; d++) {
            values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
            dates[d] = new Object[values[d].length];
            for (int i = 0; i < values[d].length; i++) {
                values[d][i] = randomNonNegativeLong();
                dates[d][i] = ZonedDateTime.ofInstant(Instant.ofEpochMilli(values[d][i]), ZoneOffset.UTC);

            }
        }
        Set<String> warnings = new HashSet<>();
        Longs longs = wrap(values, deprecationMessage -> {
            warnings.add(deprecationMessage);
            /* Create a temporary directory to prove we are running with the
             * server's permissions. */
            createTempDir();
        });

        for (int round = 0; round < 10; round++) {
            int d = between(0, values.length - 1);
            longs.setNextDocId(d);
            if (dates[d].length > 0) {
                assertEquals(dates[d][0], longs.getDate());
            } else {
                Exception e = expectThrows(IllegalStateException.class, () -> longs.getDate());
                assertEquals("A document doesn't have a value for a field! " +
                    "Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
            }

            assertEquals(values[d].length, longs.getDates().size());
            for (int i = 0; i < values[d].length; i++) {
                assertEquals(dates[d][i], longs.getDates().get(i));
            }

            Exception e = expectThrows(UnsupportedOperationException.class, () -> longs.getDates().add(ZonedDateTime.now(ZoneOffset.UTC)));
            assertEquals("doc values are unmodifiable", e.getMessage());
        }

        /*
         * Invoke getDates without any privileges to verify that
         * it still works without any. In particularly, this
         * verifies that the callback that we've configured
         * above works. That callback creates a temporary
         * directory which is not possible with "noPermissions".
         */
        PermissionCollection noPermissions = new Permissions();
        AccessControlContext noPermissionsAcc = new AccessControlContext(
            new ProtectionDomain[] {
                new ProtectionDomain(null, noPermissions)
            }
        );
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                try {
                    longs.getDates();
                } catch (IOException e) {
                    throw new RuntimeException("unexpected", e);
                }
                return null;
            }
        }, noPermissionsAcc);

        assertThat(warnings, containsInAnyOrder(
                "getDate on numeric fields is deprecated. Use a date field to get dates.",
                "getDates on numeric fields is deprecated. Use a date field to get dates."));
    }

    private Longs wrap(long[][] values, Consumer<String> deprecationCallback) {
        return new Longs(new AbstractSortedNumericDocValues() {
            long[] current;
{'fraction_non_alphanumeric': 0.11455608555162333, 'fraction_numerical': 0.007231881827973534, 'mean_word_length': 2.386920270974466, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5047780', 'n_tokens_mistral': 3494, 'n_tokens_neox': 3361, 'n_words': 1017}