org.craftercms.commerce.client.itest.data.MongoTestDataService.java Source code

Java tutorial

Introduction

Here is the source code for org.craftercms.commerce.client.itest.data.MongoTestDataService.java

Source

/*
 * Copyright (C) 2007-2013 Crafter Software Corporation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.craftercms.commerce.client.itest.data;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.craftercms.commerce.api.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * A service meant to create and destroy test data inside a Mongo database.
 * This is useful for integration testing.
 * 
 * @author Michiel Verkaik (mverkaik@rivetlogic.com)
 *
 */
public class MongoTestDataService implements TestDataService<Object> {

    private static final Log LOGGER = LogFactory.getLog(MongoTestDataService.class);
    private static final String TEST_DATA_FILE = "/itest-data/mongo-orders.txt";

    private ObjectMapper mapper;
    private MongoTemplate mongoTemplate;

    public MongoTestDataService(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        mapper = new ObjectMapper();
    }

    public void createTestData() throws Exception {
        Resource resource = new ClassPathResource(TEST_DATA_FILE);
        File file = resource.getFile();

        //Order order = mapper.readValue(file, Order.class);
        //mongoTemplate.save(order, Order.class.getName());

        //      Set<Order> orders = mapper.readValue(file, new TypeReference<Set<Order>>() {});
        //      mongoTemplate.insert(orders, Order.class.getName());

        LOGGER.info("Finished creating Mongo test data.");
    }

    public void verifyTestData() {
        LOGGER.info("Finished VERIFYING MONGO test data ( NOT YET IMPLEMENTED !!!).");
    }

    public void destroyTestData() {
        // simply drop the db
        mongoTemplate.getDb().dropDatabase();

        LOGGER.info("Finished destroying Mongo test data.");
    }

}