<commit_before>/*
 * Copyright (C) 2011 <NAME> <b<EMAIL> at gmail dot com> and <NAME> <<EMAIL> dot yves at g<EMAIL> dot <EMAIL>>
 *
 * 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.
 */

package org.jongo.marshall;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.*;
import org.jongo.MongoCollection;
import org.jongo.model.Friend;
import org.jongo.util.JongoTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;

public class DocumentMarshallingTest extends JongoTestBase {

    private MongoCollection collection;

    @Before
    public void setUp() throws Exception {
        collection = createEmptyCollection("marshalling");
    }

    @After
    public void tearDown() throws Exception {
        dropCollection("marshalling");
    }

    @Test
    public void canHandleJavaTypes() throws Exception {

        JavaNativeType type = new JavaNativeType();
        type.number = 100L;
        type.string = "value";
        type.bool = false;
        type.anEnum = Parameter.TEST;

        collection.save(type);

        assertHasBeenPersistedAs("{'number' : 100}");
        assertHasBeenPersistedAs("{'string' : 'value'}");
        assertHasBeenPersistedAs("{'bool' : false}");
        assertHasBeenPersistedAs("{'anEnum' : 'TEST'}");
        JavaNativeType result = collection.findOne("{}").as(JavaNativeType.class);
        assertThat(result.bool).isFalse();
        assertThat(result.string).isEqualTo("value");
        assertThat(result.number).isEqualTo(100L);
        assertThat(result.anEnum).isEqualTo(Parameter.TEST);
    }

    @Test
    public void canHandleMinAndMaxKey() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.minKey = new MinKey();
        type.maxKey = new MaxKey();

        collection.save(type);

        assertHasBeenPersistedAs("{'minKey' : { '$minKey' : 1}}");
        assertHasBeenPersistedAs("{'maxKey' : { '$maxKey' : 1}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.minKey).isNotNull();
        assertThat(result.maxKey).isNotNull();
    }

    @Test
    public void canHandleDecimal128() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.decimal128 = Decimal128.parse("10");

        collection.save(type);

        assertHasBeenPersistedAs("{'decimal128' : { '$type' : 'decimal'}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.decimal128).isNotNull();
    }

    @Test
    public void canHandleObjectId() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.key = new ObjectId("<KEY>");

        collection.save(type);

        assertHasBeenPersistedAs("{'key' : { '$oid' : '<KEY>'}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.key).isEqualTo(new ObjectId("<KEY>"));
    }

    @Test
    public void canHandlePattern() throws Exception {

        Pattern chars = Pattern.compile("[a-z]");
        BSONPrimitiveType type = new BSONPrimitiveType();
        type.pattern = chars;

        collection.save(type);

        assertHasBeenPersistedAs("{'pattern' : { '$regex' : '[a-z]'}}");//options is not longer generated since 2.8.0
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.pattern.toString()).isEqualTo(chars.toString());
    }

    @Test
    public void canHandleBSONTimestamp() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.timestamp = new BSONTimestamp(1, 2);

        collection.save(type);

        assertHasBeenPersistedAs("{'timestamp' : { '$timestamp' : { 't' : 1 , 'i' : 2}}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.timestamp).isEqualTo(new BSONTimestamp(1, 2));
    }

    @Test
    public void canHandleISODate() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.date = new Date(123);

        collection.save(type);

        assertHasBeenPersistedAs("{'date' : { '$date' : 123}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.date).isEqualTo(new Date(123));
    }

    @Test
    public void canHandleNonIsoDate() throws IOException {

        collection.insert("{date:#}", 1340714101235L);

        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);

        assertThat(result.date).isEqualTo(new Date(1340714101235L));
    }

    @Test
    public void canHandleUUID() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.uuid = UUID.fromString("cf0eddfa-2670-4929-a581-eb263d839cab");

        collection.save(type);

        assertHasBeenPersistedAs("{'uuid' : { '$uuid' : 'cf0eddfa-2670-4929-a581-eb263d839cab'}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.uuid).isEqualTo(type.uuid);
    }

    @Test
    public void canHandleDBObject() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.dbo = new BasicDBObject("key", "value");

        collection.save(type);

        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.dbo).isEqualTo(type.dbo);
    }

    @Test
    public void canHandleMapWithPrimitiveType() throws Exception {

        Map<String, Date> strings = new HashMap<String, Date>();
        strings.put("key", new Date(456));
        BSONPrimitiveType type = new BSONPrimitiveType();
        type.mapWithDates = strings;

        collection.save(type);

        assertHasBeenPersistedAs("{mapWithDates : { key : { $date : 456}}}");<commit_msg>UUID are no longer serialized using $uuid operator<commit_after>/*
 * Copyright (C) 2011 <NAME> <<EMAIL> at <EMAIL>> and <NAME> <amsellem dot yves at gmail dot com>
 *
 * 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.
 */

package org.jongo.marshall;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.BsonBinary;
import org.bson.UuidRepresentation;
import org.bson.types.*;
import org.jongo.MongoCollection;
import org.jongo.model.Friend;
import org.jongo.util.JongoTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;

public class DocumentMarshallingTest extends JongoTestBase {

    private MongoCollection collection;

    @Before
    public void setUp() throws Exception {
        collection = createEmptyCollection("marshalling");
    }

    @After
    public void tearDown() throws Exception {
        dropCollection("marshalling");
    }

    @Test
    public void canHandleJavaTypes() throws Exception {

        JavaNativeType type = new JavaNativeType();
        type.number = 100L;
        type.string = "value";
        type.bool = false;
        type.anEnum = Parameter.TEST;

        collection.save(type);

        assertHasBeenPersistedAs("{'number' : 100}");
        assertHasBeenPersistedAs("{'string' : 'value'}");
        assertHasBeenPersistedAs("{'bool' : false}");
        assertHasBeenPersistedAs("{'anEnum' : 'TEST'}");
        JavaNativeType result = collection.findOne("{}").as(JavaNativeType.class);
        assertThat(result.bool).isFalse();
        assertThat(result.string).isEqualTo("value");
        assertThat(result.number).isEqualTo(100L);
        assertThat(result.anEnum).isEqualTo(Parameter.TEST);
    }

    @Test
    public void canHandleMinAndMaxKey() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.minKey = new MinKey();
        type.maxKey = new MaxKey();

        collection.save(type);

        assertHasBeenPersistedAs("{'minKey' : { '$minKey' : 1}}");
        assertHasBeenPersistedAs("{'maxKey' : { '$maxKey' : 1}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.minKey).isNotNull();
        assertThat(result.maxKey).isNotNull();
    }

    @Test
    public void canHandleDecimal128() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.decimal128 = Decimal128.parse("10");

        collection.save(type);

        assertHasBeenPersistedAs("{'decimal128' : { '$type' : 'decimal'}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.decimal128).isNotNull();
    }

    @Test
    public void canHandleObjectId() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.key = new ObjectId("<KEY>");

        collection.save(type);

        assertHasBeenPersistedAs("{'key' : { '$oid' : '<KEY>'}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.key).isEqualTo(new ObjectId("<KEY>"));
    }

    @Test
    public void canHandlePattern() throws Exception {

        Pattern chars = Pattern.compile("[a-z]");
        BSONPrimitiveType type = new BSONPrimitiveType();
        type.pattern = chars;

        collection.save(type);

        assertHasBeenPersistedAs("{'pattern' : { '$regex' : '[a-z]'}}");//options is not longer generated since 2.8.0
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.pattern.toString()).isEqualTo(chars.toString());
    }

    @Test
    public void canHandleBSONTimestamp() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.timestamp = new BSONTimestamp(1, 2);

        collection.save(type);

        assertHasBeenPersistedAs("{'timestamp' : { '$timestamp' : { 't' : 1 , 'i' : 2}}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.timestamp).isEqualTo(new BSONTimestamp(1, 2));
    }

    @Test
    public void canHandleISODate() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.date = new Date(123);

        collection.save(type);

        assertHasBeenPersistedAs("{'date' : { '$date' : 123}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.date).isEqualTo(new Date(123));
    }

    @Test
    public void canHandleNonIsoDate() throws IOException {

        collection.insert("{date:#}", 1340714101235L);

        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);

        assertThat(result.date).isEqualTo(new Date(1340714101235L));
    }

    @Test
    public void canHandleUUID() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.uuid = UUID.fromString("cf0eddfa-2670-4929-a581-eb263d839cab");
        String uuidBase64 = Base64.getEncoder().encodeToString(new BsonBinary(type.uuid, UuidRepresentation.JAVA_LEGACY).getData());

        collection.save(type);

        assertHasBeenPersistedAs("{'uuid' : {'$binary': {'base64': '" + uuidBase64 + "', 'subType': '03'}}}");
        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.uuid).isEqualTo(type.uuid);
    }

    @Test
    public void canHandleDBObject() throws Exception {

        BSONPrimitiveType type = new BSONPrimitiveType();
        type.dbo = new BasicDBObject("key", "value");

        collection.save(type);

        BSONPrimitiveType result = collection.findOne("{}").as(BSONPrimitiveType.class);
        assertThat(result.dbo).isEqualTo(type.dbo);
    }

    @Test
    public void canHandleMapWithPrimitiveType() throws Exception {

        Map<String, Date> strings = new HashMap<String, Date>();
        strings.put("key", new Date(456));
        BSONPrimitiveType type = new BSONPrimitiveType();
        type.mapWithDates = strings;

        collection.save(type);

        assertHasBeenPersistedAs("{mapWithDates : { key : { $date : 456}}}");
{'fraction_non_alphanumeric': 0.1300177095631641, 'fraction_numerical': 0.01741440377804014, 'mean_word_length': 3.386084142394822, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7319674', 'n_tokens_mistral': 4071, 'n_tokens_neox': 3941, 'n_words': 840}