com.setronica.ucs.storage.JSONTest.java Source code

Java tutorial

Introduction

Here is the source code for com.setronica.ucs.storage.JSONTest.java

Source

/*
 * Copyright (C) 2014 SETRONICA - setronica.com
 * This source code is available under the terms of the GNU Lesser General Public License
 * as published by The Open Source Initiative (OSI), either version 3 of the License,
 * or (at your option) any later version.
 *
 * UCS 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License.
 */

package com.setronica.ucs.storage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.setronica.ucs.product.ProductIdentity;
import com.setronica.ucs.product.identity.AsinIdentity;
import com.setronica.ucs.product.identity.EanIdentity;
import com.setronica.ucs.product.identity.ProductIdentityFactory;
import com.setronica.ucs.product.impl.MemPackedProduct;
import com.setronica.ucs.product.impl.MemPackedProductContent;
import junit.framework.TestCase;

import java.io.IOException;
import java.util.HashMap;

/**
 * User: nikolay
 * Date: 9/19/13
 */
public class JSONTest extends TestCase {

    public void testParse() throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule enumModule = new SimpleModule("EnumModule").addDeserializer(ProductIdentity.class,
                new PgStructMapStorage.MemProductIdentityDeserializer(new ProductIdentityFactory()));
        mapper.registerModule(enumModule);
        MemPackedProduct p = new MemPackedProduct();
        p.setLastModified(1);
        p.setIdentities(new ProductIdentity[] { new AsinIdentity("B005Y3721U"), new EanIdentity("234") });
        p.setContents(new HashMap<String, MemPackedProductContent>());
        p.getContents().put("bob", new MemPackedProductContent());
        String json = mapper.writeValueAsString(p);
        MemPackedProduct p2 = mapper.readValue(json, MemPackedProduct.class);
        assertEquals(p.getLastModified(), p2.getLastModified());
        assertEquals(p.getIdentities().length, p2.getIdentities().length);
        assertEquals(p.getIdentities()[0], p2.getIdentities()[0]);
        assertEquals(p.getIdentities()[1], p2.getIdentities()[1]);
    }
}