<commit_before>package org.xander.dao;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteResult;
import com.mongodb.CommandResult;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceOutput;
import com.mongodb.WriteResult;
import org.bson.Document;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.xander.model.Car;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.data.mongodb.core.query.Criteria.where;

@ContextConfiguration(locations = {"classpath:/org/xander/model/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class CarDaoTest {
    public static final String COLLECTION_NAME = "cars";

    @Autowired
    private CarDao carDao;
    @Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void saveImage() {
        String imageName = "Capture.png";
        String actualFileName = carDao.saveImage(imageName);
        assertThat("file names are different", actualFileName, is(imageName));
    }

    @Test
    public void removeByEntity() {
        Car actualCar = new Car("Mercedes", 90000);
        carDao.save(actualCar);

        assertNotNull("cound not get an entity for delete", actualCar);

        WriteResult writeResult = carDao.removeByEntity(actualCar);

        assertTrue("entity was not acknowledged", writeResult.wasAcknowledged());
        assertFalse("there was an updateNameToLowerCase of existing entity", writeResult.isUpdateOfExisting());
        assertNull("upserted id is not null", writeResult.getUpsertedId());
        assertThat("entity was not removed", writeResult.getN(), is(1));

        exception.expect(CarNotFoundException.class);
        carDao.get(actualCar.getId());
    }

    @Test
    public void removeByEntityNotPresent() {
        Car car = new Car();
        String id = car.getId();
        WriteResult writeResult = carDao.removeByEntity(car);

        assertTrue("entity was not acknowledged", writeResult.wasAcknowledged());
        assertFalse("there was an updateNameToLowerCase of existing entity", writeResult.isUpdateOfExisting());
        assertNull("upserted id is not null", writeResult.getUpsertedId());

        assertThat("entity was not removed", writeResult.getN(), is(0));

        exception.expect(CarNotFoundException.class);
        assertNull("car was not removed", carDao.get(id));
    }

    @Test
    public void getCarById() {
        Car car = new Car("TestCar", 90000);
        carDao.save(car);

        String id = car.getId();
        Car actualCar = carDao.findOne(id);
        assertThat("id is different", actualCar.getId(), is(car.getId()));
        assertThat("name is different", actualCar.getName(), is("TestCar"));
        assertThat("price is different", actualCar.getPrice(), is(90000));

        carDao.remove(id);
    }

    @Test
    public void updateEntity() {
        Car renault = new Car("Renault", 50000);
        carDao.save(renault);

        List<Car> cars = carDao.updateNameToLowerCase(renault);
        assertThat("where is no car updated", cars.get(0).getName(), is(renault.getName().toLowerCase()));
    }


    @Test
    public void getOptionalNoEntry() {
        exception.expect(CarNotFoundException.class);
        carDao.findOne("asd");
    }

    @Test
    public void aggregation() {
        AggregationResults<ArrayList> aggregate = carDao.aggregate();
        Object result = ((Map.Entry) ((BasicDBObject) ((BasicDBList) aggregate.getRawResults().get("result")).toArray()[0]).entrySet().toArray()[0]).getKey();

        assertThat("price field is not present", result.equals("price"), is(true));
    }

    @Test
    public void entityWithLikeQuery() {
        List<Car> entityLikeQuery = carDao.getEntityLikeQuery();
        assertThat("there are no entries", entityLikeQuery.size(), is(2));
    }

    @Test
    public void convertJsonToDbObj() {
        String json = "{'name':'<NAME>', 'price':102000}";
        Car car = carDao.convertJsonToDbObject(json);

        assertThat("id is different", car.getId() != null, is(true));
        assertThat("name is different", car.getName(), is("<NAME>"));
        assertThat("price is different", car.getPrice(), is(102000));
    }


    @Test
    public void collectionPresent() {
        assertTrue(carDao.collectionPresent(COLLECTION_NAME));
    }<commit_msg>adding rule exception expect message<commit_after>package org.xander.dao;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteResult;
import com.mongodb.CommandResult;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceOutput;
import com.mongodb.WriteResult;
import org.bson.Document;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.xander.model.Car;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.data.mongodb.core.query.Criteria.where;

@ContextConfiguration(locations = {"classpath:/org/xander/model/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class CarDaoTest {
    public static final String COLLECTION_NAME = "cars";

    @Autowired
    private CarDao carDao;
    @Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void saveImage() {
        String imageName = "Capture.png";
        String actualFileName = carDao.saveImage(imageName);
        assertThat("file names are different", actualFileName, is(imageName));
    }

    @Test
    public void removeByEntity() {
        Car actualCar = new Car("Mercedes", 90000);
        carDao.save(actualCar);

        assertNotNull("cound not get an entity for delete", actualCar);

        WriteResult writeResult = carDao.removeByEntity(actualCar);

        assertTrue("entity was not acknowledged", writeResult.wasAcknowledged());
        assertFalse("there was an updateNameToLowerCase of existing entity", writeResult.isUpdateOfExisting());
        assertNull("upserted id is not null", writeResult.getUpsertedId());
        assertThat("entity was not removed", writeResult.getN(), is(1));

        exception.expect(CarNotFoundException.class);
        exception.expectMessage("No car entry found with id: <" + actualCar.getId() + ">");
        carDao.get(actualCar.getId());
    }

    @Test
    public void removeByEntityNotPresent() {
        Car car = new Car();
        String id = car.getId();
        WriteResult writeResult = carDao.removeByEntity(car);

        assertTrue("entity was not acknowledged", writeResult.wasAcknowledged());
        assertFalse("there was an updateNameToLowerCase of existing entity", writeResult.isUpdateOfExisting());
        assertNull("upserted id is not null", writeResult.getUpsertedId());

        assertThat("entity was not removed", writeResult.getN(), is(0));

        exception.expect(CarNotFoundException.class);
        exception.expectMessage("No car entry found with id: <" + id + ">");
        assertNull("car was not removed", carDao.get(id));
    }

    @Test
    public void getCarById() {
        Car car = new Car("TestCar", 90000);
        carDao.save(car);

        String id = car.getId();
        Car actualCar = carDao.findOne(id);
        assertThat("id is different", actualCar.getId(), is(car.getId()));
        assertThat("name is different", actualCar.getName(), is("TestCar"));
        assertThat("price is different", actualCar.getPrice(), is(90000));

        carDao.remove(id);
    }

    @Test
    public void updateEntity() {
        Car renault = new Car("Renault", 50000);
        carDao.save(renault);

        List<Car> cars = carDao.updateNameToLowerCase(renault);
        assertThat("where is no car updated", cars.get(0).getName(), is(renault.getName().toLowerCase()));
    }


    @Test
    public void getOptionalNoEntry() {
        String carId = "illegalId";
        exception.expectMessage("No car entry found with id: <" + carId + ">");
        exception.expect(CarNotFoundException.class);
        carDao.findOne(carId);
    }

    @Test
    public void aggregation() {
        AggregationResults<ArrayList> aggregate = carDao.aggregate();
        Object result = ((Map.Entry) ((BasicDBObject) ((BasicDBList) aggregate.getRawResults().get("result")).toArray()[0]).entrySet().toArray()[0]).getKey();

        assertThat("price field is not present", result.equals("price"), is(true));
    }

    @Test
    public void entityWithLikeQuery() {
        List<Car> entityLikeQuery = carDao.getEntityLikeQuery();
        assertThat("there are no entries", entityLikeQuery.size(), is(2));
    }

    @Test
    public void convertJsonToDbObj() {
        String json = "{'name':'<NAME>', 'price':102000}";
        Car car = carDao.convertJsonToDbObject(json);

        assertThat("id is different", car.getId() != null, is(true));
        assertThat("name is different", car.getName(), is("<NAME>"));
        assertThat("price is different", car.getPrice(), is(102000));
    }


    @Test
    public void collectionPresent() {
        assertTrue(carDao.collectionPresent(COLLECTION_NAME));
    }
{'fraction_non_alphanumeric': 0.12337294850028296, 'fraction_numerical': 0.007734389737785324, 'mean_word_length': 3.810798548094374, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1779656', 'n_tokens_mistral': 3047, 'n_tokens_neox': 3011, 'n_words': 674}