Package org.openengsb.core.ekb.common

Source Code of org.openengsb.core.ekb.common.EDBConverterTest

/**
* Licensed to the Austrian Association for Software Tool Integration (AASTI)
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. The AASTI 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.openengsb.core.ekb.common;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.openengsb.core.api.context.ContextHolder;
import org.openengsb.core.api.model.ModelWrapper;
import org.openengsb.core.api.model.annotation.OpenEngSBForeignKey;
import org.openengsb.core.edb.api.EDBConstants;
import org.openengsb.core.edb.api.EDBObject;
import org.openengsb.core.edb.api.EDBObjectEntry;
import org.openengsb.core.edb.api.EngineeringDatabaseService;
import org.openengsb.core.ekb.api.ConnectorInformation;
import org.openengsb.core.ekb.common.models.EngineeringObjectModel;
import org.openengsb.core.ekb.common.models.PrimitivePropertiesModel;
import org.openengsb.core.ekb.common.models.RecursiveModel;
import org.openengsb.core.ekb.common.models.SubModel;
import org.openengsb.core.ekb.common.models.TestModel;
import org.openengsb.core.ekb.common.models.TestModel2.ENUM;
import org.openengsb.core.ekb.common.models.WrappedPropertiesModel;

/**
* The EDBConverter test file only tests the converting Model -> EDBObject since the other way round is tested by the
* QueryInterfaceService test file.
*/
public class EDBConverterTest {
    private static final String CONTEXT_ID = "testcontext";
    private EDBConverter converter;

    private EngineeringDatabaseService mockedService;

    @Before
    public void setUp() {
        mockedService = mock(EngineeringDatabaseService.class);
        converter = new EDBConverter(mockedService);
        ContextHolder.get().setCurrentContextId(CONTEXT_ID);
    }

    @Test
    public void testIfModelAgentIsSet_shouldWork() throws Exception {
        TestModel model = new TestModel();
        assertThat("TestModel isn't enhanced. Maybe you forgot to set the java agent?",
            ModelWrapper.isModel(model.getClass()), is(true));
    }

    @Test
    public void primitivePropertyConversion_toEDBObject_works() {
        PrimitivePropertiesModel model = new PrimitivePropertiesModel();
        model.setId("test");
        model.setBooleanByGet(true);
        model.setBooleanByIs(true);
        model.setPrimitiveChar(Character.MAX_VALUE);
        model.setPrimitiveDouble(Double.MAX_VALUE);
        model.setPrimitiveFloat(Float.MAX_VALUE);
        model.setPrimitiveInt(Integer.MAX_VALUE);
        model.setPrimitiveLong(Long.MAX_VALUE);
        model.setPrimitiveShort(Short.MAX_VALUE);
        ConnectorInformation id = getTestConnectorInformation();
        List<EDBObject> objects = converter.convertModelToEDBObject(model, id);
        EDBObject object = objects.get(0);
        // check entry types
        assertThat(object.get("booleanByGet").getType(), is(Boolean.class.getName()));
        assertThat(object.get("booleanByIs").getType(), is(Boolean.class.getName()));
        assertThat(object.get("primitiveChar").getType(), is(Character.class.getName()));
        assertThat(object.get("primitiveShort").getType(), is(Short.class.getName()));
        assertThat(object.get("primitiveInt").getType(), is(Integer.class.getName()));
        assertThat(object.get("primitiveLong").getType(), is(Long.class.getName()));
        assertThat(object.get("primitiveFloat").getType(), is(Float.class.getName()));
        assertThat(object.get("primitiveDouble").getType(), is(Double.class.getName()));
        // check values
        assertThat(object.getBoolean("booleanByGet"), is(true));
        assertThat(object.getBoolean("booleanByIs"), is(true));
        assertThat(object.getChar("primitiveChar"), is(Character.MAX_VALUE));
        assertThat(object.getShort("primitiveShort"), is(Short.MAX_VALUE));
        assertThat(object.getInteger("primitiveInt"), is(Integer.MAX_VALUE));
        assertThat(object.getLong("primitiveLong"), is(Long.MAX_VALUE));
        assertThat(object.getFloat("primitiveFloat"), is(Float.MAX_VALUE));
        assertThat(object.getDouble("primitiveDouble"), is(Double.MAX_VALUE));
    }

    @Test
    public void primitivePropertyConversion_toModel_works() {
        EDBObject object = new EDBObject("test");
        object.putEDBObjectEntry(EDBConstants.MODEL_TYPE, PrimitivePropertiesModel.class.getName());
        object.putEDBObjectEntry(EDBConstants.MODEL_OID, "test");
        object.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        object.putEDBObjectEntry("id", "test");
        object.putEDBObjectEntry("booleanByGet", true);
        object.putEDBObjectEntry("booleanByIs", true);
        object.putEDBObjectEntry("primitiveChar", Character.MAX_VALUE);
        object.putEDBObjectEntry("primitiveShort", Short.MAX_VALUE);
        object.putEDBObjectEntry("primitiveInt", Integer.MAX_VALUE);
        object.putEDBObjectEntry("primitiveLong", Long.MAX_VALUE);
        object.putEDBObjectEntry("primitiveFloat", Float.MAX_VALUE);
        object.putEDBObjectEntry("primitiveDouble", Double.MAX_VALUE);

        PrimitivePropertiesModel model = converter.convertEDBObjectToModel(PrimitivePropertiesModel.class, object);
        assertThat(model.getId(), is("test"));
        assertThat(model.getBooleanByGet(), is(true));
        assertThat(model.isBooleanByIs(), is(true));
        assertThat(model.getPrimitiveChar(), is(Character.MAX_VALUE));
        assertThat(model.getPrimitiveShort(), is(Short.MAX_VALUE));
        assertThat(model.getPrimitiveInt(), is(Integer.MAX_VALUE));
        assertThat(model.getPrimitiveLong(), is(Long.MAX_VALUE));
        assertThat(model.getPrimitiveFloat(), is(Float.MAX_VALUE));
        assertThat(model.getPrimitiveDouble(), is(Double.MAX_VALUE));
    }

    @Test
    public void wrappedPropertyConversion_toEDBObject_works() {
        WrappedPropertiesModel model = new WrappedPropertiesModel();
        model.setId("test");
        model.setBooleanByGet(true);
        model.setBooleanByIs(true);
        model.setWrappedChar(Character.MAX_VALUE);
        model.setWrappedDouble(Double.MAX_VALUE);
        model.setWrappedFloat(Float.MAX_VALUE);
        model.setWrappedInt(Integer.MAX_VALUE);
        model.setWrappedLong(Long.MAX_VALUE);
        model.setWrappedShort(Short.MAX_VALUE);

        ConnectorInformation id = getTestConnectorInformation();
        List<EDBObject> objects = converter.convertModelToEDBObject(model, id);
        EDBObject object = objects.get(0);
        // check entry types
        assertThat(object.get("booleanByGet").getType(), is(Boolean.class.getName()));
        assertThat(object.get("booleanByIs").getType(), is(Boolean.class.getName()));
        assertThat(object.get("wrappedChar").getType(), is(Character.class.getName()));
        assertThat(object.get("wrappedShort").getType(), is(Short.class.getName()));
        assertThat(object.get("wrappedInt").getType(), is(Integer.class.getName()));
        assertThat(object.get("wrappedLong").getType(), is(Long.class.getName()));
        assertThat(object.get("wrappedFloat").getType(), is(Float.class.getName()));
        assertThat(object.get("wrappedDouble").getType(), is(Double.class.getName()));
        // check values
        assertThat(object.getBoolean("booleanByGet"), is(true));
        assertThat(object.getBoolean("booleanByIs"), is(true));
        assertThat(object.getChar("wrappedChar"), is(Character.MAX_VALUE));
        assertThat(object.getShort("wrappedShort"), is(Short.MAX_VALUE));
        assertThat(object.getInteger("wrappedInt"), is(Integer.MAX_VALUE));
        assertThat(object.getLong("wrappedLong"), is(Long.MAX_VALUE));
        assertThat(object.getFloat("wrappedFloat"), is(Float.MAX_VALUE));
        assertThat(object.getDouble("wrappedDouble"), is(Double.MAX_VALUE));
    }

    @Test
    public void wrappedPropertyConversion_toModel_works() {
        EDBObject object = new EDBObject("test");
        object.putEDBObjectEntry(EDBConstants.MODEL_TYPE, WrappedPropertiesModel.class.getName());
        object.putEDBObjectEntry(EDBConstants.MODEL_OID, "test");
        object.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        object.putEDBObjectEntry("id", "test");
        object.putEDBObjectEntry("booleanByGet", true);
        object.putEDBObjectEntry("booleanByIs", true);
        object.putEDBObjectEntry("wrappedChar", Character.MAX_VALUE);
        object.putEDBObjectEntry("wrappedShort", Short.MAX_VALUE);
        object.putEDBObjectEntry("wrappedInt", Integer.MAX_VALUE);
        object.putEDBObjectEntry("wrappedLong", Long.MAX_VALUE);
        object.putEDBObjectEntry("wrappedFloat", Float.MAX_VALUE);
        object.putEDBObjectEntry("wrappedDouble", Double.MAX_VALUE);

        WrappedPropertiesModel model = converter.convertEDBObjectToModel(WrappedPropertiesModel.class, object);
        assertThat(model.getId(), is("test"));
        assertThat(model.getBooleanByGet(), is(true));
        assertThat(model.isBooleanByIs(), is(true));
        assertThat(model.getWrappedChar(), is(Character.MAX_VALUE));
        assertThat(model.getWrappedShort(), is(Short.MAX_VALUE));
        assertThat(model.getWrappedInt(), is(Integer.MAX_VALUE));
        assertThat(model.getWrappedLong(), is(Long.MAX_VALUE));
        assertThat(model.getWrappedFloat(), is(Float.MAX_VALUE));
        assertThat(model.getWrappedDouble(), is(Double.MAX_VALUE));
    }

    @Test
    public void testSimpleModelToEDBObjectConversion_shouldWork() throws Exception {
        TestModel model = new TestModel();
        model.setId("test");
        Date date = new Date();
        model.setDate(date);
        model.setEnumeration(ENUM.A);
        model.setName("testobject");
        model.setCheck(false);
        model.setCheck2(true);
        model.setCheck3(false);

        ConnectorInformation id = getTestConnectorInformation();

        List<EDBObject> objects = converter.convertModelToEDBObject(model, id);
        EDBObject object = objects.get(0);

        assertThat(object.getString("connectorId"), is("testconnector"));
        assertThat(object.getString("id"), is("test"));
        assertThat(object.getString("oid"), is(ModelWrapper.wrap(model).getCompleteModelOID()));
        assertThat(object.getString("domainId"), is("testdomain"));
        assertThat(object.getString("name"), is("testobject"));
        assertThat(object.getString("instanceId"), is("testinstance"));
        assertThat(object.getObject("enumeration", ENUM.class), is(ENUM.A));
        assertThat(object.getObject("date", Date.class), is(date));
        assertThat(object.getObject("check", Boolean.class), is(false));
        assertThat(object.getObject("check2", Boolean.class), is(true));
        assertThat(object.getObject("check3", Boolean.class), is(false));
        assertThat(object.getString(EDBConstants.MODEL_TYPE), is(TestModel.class.getName()));
    }

    @Test
    public void testComplexModelToEDBObjectConversion_shouldWork() throws Exception {
        TestModel model = new TestModel();
        model.setId("test");
        SubModel sub = new SubModel();
        sub.setId("sub");
        sub.setValue("teststring");
        model.setSub(sub);
        ConnectorInformation id = getTestConnectorInformation();

        List<EDBObject> objects = converter.convertModelToEDBObject(model, id);
        EDBObject object = objects.get(1);
        assertThat(object.getString("sub"), is(ModelWrapper.wrap(sub).getCompleteModelOID()));
        EDBObject subObject = objects.get(0);
        assertThat(subObject.getString("id"), is("sub"));
        assertThat(subObject.getString(EDBConstants.MODEL_TYPE), is(SubModel.class.getName()));
    }

    @Test
    public void testRecursiveModelToEDBObjectConversion_shouldWork() throws Exception {
        // prepare
        RecursiveModel root = new RecursiveModel();
        root.setId("root");
        RecursiveModel rootChild = new RecursiveModel();
        rootChild.setId("root_child");
        RecursiveModel rootChildChild = new RecursiveModel();
        rootChildChild.setId("root_child_child");
        RecursiveModel rootChildChildChild = new RecursiveModel();
        rootChildChildChild.setId("root_child_child_child");

        root.setChild(rootChild);
        rootChild.setChild(rootChildChild);
        rootChildChild.setChild(rootChildChildChild);
        ConnectorInformation id = getTestConnectorInformation();

        // test
        List<EDBObject> objects = converter.convertModelToEDBObject(root, id);
        // assert
        EDBObject obj = objects.get(3);
        assertThat(obj.getString("id"), is("root"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("child"), is(ModelWrapper.wrap(rootChild).getCompleteModelOID()));

        obj = objects.get(2);
        assertThat(obj.getString("id"), is("root_child"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("child"), is(ModelWrapper.wrap(rootChildChild).getCompleteModelOID()));

        obj = objects.get(1);
        assertThat(obj.getString("id"), is("root_child_child"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("child"), is(ModelWrapper.wrap(rootChildChildChild).getCompleteModelOID()));

        obj = objects.get(0);
        assertThat(obj.getString("id"), is("root_child_child_child"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("child"), nullValue());
    }

    @Test
    public void testRecursiveModelWithCompositionToEDBObjectConversion_shouldWork() throws Exception {
        // prepare
        RecursiveModel root = new RecursiveModel("root");
        RecursiveModel rootChild1 = new RecursiveModel("root_child1");
        RecursiveModel rootChild2 = new RecursiveModel("root_child2");
        RecursiveModel child1Child1 = new RecursiveModel("child1_child1");
        RecursiveModel child2Child1 = new RecursiveModel("child2_child1");
        RecursiveModel child2Child2 = new RecursiveModel("child2_child2");
        List<RecursiveModel> rootChildren = Arrays.asList(new RecursiveModel[]{ rootChild1, rootChild2 });
        List<RecursiveModel> child2Children = Arrays.asList(new RecursiveModel[]{ child2Child1, child2Child2 });
        root.setChildren(rootChildren);
        rootChild1.setChild(child1Child1);
        rootChild2.setChildren(child2Children);
        ConnectorInformation id = getTestConnectorInformation();
        // test
        List<EDBObject> objects = converter.convertModelToEDBObject(root, id);
        // assert
        EDBObject obj = objects.get(5);
        assertThat(obj.getString("id"), is("root"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("children.0"), is(ModelWrapper.wrap(rootChild1).getCompleteModelOID()));
        assertThat(obj.getString("children.1"), is(ModelWrapper.wrap(rootChild2).getCompleteModelOID()));

        obj = objects.get(4);
        assertThat(obj.getString("id"), is("root_child2"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("children.0"), is(ModelWrapper.wrap(child2Child1).getCompleteModelOID()));
        assertThat(obj.getString("children.1"), is(ModelWrapper.wrap(child2Child2).getCompleteModelOID()));

        obj = objects.get(3);
        assertThat(obj.getString("id"), is("child2_child2"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));

        obj = objects.get(2);
        assertThat(obj.getString("id"), is("child2_child1"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));

        obj = objects.get(1);
        assertThat(obj.getString("id"), is("root_child1"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
        assertThat(obj.getString("child"), is(ModelWrapper.wrap(child1Child1).getCompleteModelOID()));

        obj = objects.get(0);
        assertThat(obj.getString("id"), is("child1_child1"));
        assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName()));
    }

    @Test
    public void testIfEDBObjectsContainCorrectType_shouldWork() throws Exception {
        TestModel model = new TestModel();
        model.setNumber(42);
        ConnectorInformation id = getTestConnectorInformation();
        List<EDBObject> objects = converter.convertModelToEDBObject(model, id);
        EDBObject object = objects.get(0);
        EDBObjectEntry entry = object.get("number");
        assertThat(entry.getType(), is(Integer.class.getName()));
    }

    @Test
    public void testComplexListModelToEDBObjectConversion_shouldWork() throws Exception {
        TestModel model = new TestModel();
        model.setId("test");
        SubModel sub1 = new SubModel();
        sub1.setId("sub1");
        sub1.setValue("teststring1");
        SubModel sub2 = new SubModel();
        sub2.setId("sub2");
        sub2.setValue("teststring2");
        List<SubModel> subs = new ArrayList<SubModel>();
        subs.add(sub1);
        subs.add(sub2);
        model.setSubs(subs);
        ConnectorInformation id = getTestConnectorInformation();
        List<EDBObject> objects = converter.convertModelToEDBObject(model, id);
        EDBObject object = objects.get(2);
        EDBObject subObject1 = objects.get(0);
        EDBObject subObject2 = objects.get(1);

        assertThat(object.getString(EDBConverter.getEntryNameForList("subs", 0)),
            is(ModelWrapper.wrap(sub1).getCompleteModelOID()));
        assertThat(object.getString(EDBConverter.getEntryNameForList("subs", 1)),
            is(ModelWrapper.wrap(sub2).getCompleteModelOID()));
        assertThat(subObject1.getString("id"), is("sub1"));
        assertThat(subObject1.getString(EDBConstants.MODEL_TYPE), is(SubModel.class.getName()));
        assertThat(subObject2.getString("id"), is("sub2"));
        assertThat(subObject2.getString(EDBConstants.MODEL_TYPE), is(SubModel.class.getName()));
    }

    @Test
    public void testMapModelToEDBObjectConversion_shouldWork() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("keyA", "valueA");
        map.put("keyB", "valueB");
        TestModel model = new TestModel();
        model.setId("test");
        model.setMap(map);
        ConnectorInformation id = getTestConnectorInformation();
        EDBObject object = converter.convertModelToEDBObject(model, id).get(0);

        assertThat(object.getString(EDBConverter.getEntryNameForMapKey("map", 0)), is("keyA"));
        assertThat(object.getString(EDBConverter.getEntryNameForMapValue("map", 0)), is("valueA"));
        assertThat(object.getString(EDBConverter.getEntryNameForMapKey("map", 1)), is("keyB"));
        assertThat(object.getString(EDBConverter.getEntryNameForMapValue("map", 1)), is("valueB"));
    }

    @Test
    public void testConversionInBothDirections_shouldWork() throws Exception {
        TestModel model = new TestModel();
        model.setId("test");
        Date date = new Date();
        model.setDate(date);
        model.setEnumeration(ENUM.A);
        model.setName("testobject");
        EDBObject object = converter.convertModelToEDBObject(model, getTestConnectorInformation()).get(0);
        TestModel result = converter.convertEDBObjectToModel(TestModel.class, object);

        assertThat(model.getId(), is(result.getId()));
        assertThat(model.getDate(), is(result.getDate()));
        assertThat(model.getEnumeration(), is(result.getEnumeration()));
        assertThat(model.getName(), is(result.getName()));
    }

    @Test
    public void testIfArraysAreSupported_shouldWork() throws Exception {
        TestModel model = new TestModel();
        Integer[] numbers = new Integer[]{ 1, 2, 3, 4 };
        model.setNumbers(numbers);
        EDBObject object = converter.convertModelToEDBObject(model, getTestConnectorInformation()).get(0);
        TestModel result = converter.convertEDBObjectToModel(TestModel.class, object);

        assertThat(result.getNumbers(), notNullValue());
        assertThat(numbers[0], is(result.getNumbers()[0]));
        assertThat(numbers[1], is(result.getNumbers()[1]));
        assertThat(numbers[2], is(result.getNumbers()[2]));
        assertThat(numbers[3], is(result.getNumbers()[3]));
    }

    @Test
    public void testEDBObjectToModelConversion_shouldWork() throws Exception {
        EDBObject object = new EDBObject("test");
        object.putEDBObjectEntry(EDBConstants.MODEL_TYPE, TestModel.class.getName());
        object.putEDBObjectEntry(EDBConstants.MODEL_OID, "test");
        object.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        object.putEDBObjectEntry("id", "test");
        object.putEDBObjectEntry("name", "testname");
        object.putEDBObjectEntry("number", 42);
        object.putEDBObjectEntry("check", false);
        object.putEDBObjectEntry("check2", true);
        TestModel model = converter.convertEDBObjectToModel(TestModel.class, object);
        assertThat(model.getId(), is("test"));
        assertThat(model.getName(), is("testname"));
        assertThat(model.isCheck(), is(false));
        assertThat(model.isCheck2(), is(true));
        Object version = ModelWrapper.wrap(model).retrieveInternalModelVersion();
        assertThat(version, notNullValue());
        assertThat((Integer) version, is(1));
    }

    @Test
    public void testEDBObjectToRecursiveModelConversion_shouldWork() throws Exception {
        // prepare
        EDBObject root = new EDBObject("root");
        root.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        root.putEDBObjectEntry(EDBConstants.MODEL_OID, "root");
        root.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        root.putEDBObjectEntry("id", "root");
        root.putEDBObjectEntry("child", "child1");

        EDBObject child1 = new EDBObject("child1");
        child1.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child1.putEDBObjectEntry(EDBConstants.MODEL_OID, "child1");
        child1.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child1.putEDBObjectEntry("id", "child1");
        child1.putEDBObjectEntry("child", "child2");

        EDBObject child2 = new EDBObject("child2");
        child2.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child2.putEDBObjectEntry(EDBConstants.MODEL_OID, "child2");
        child2.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child2.putEDBObjectEntry("id", "child2");
        child2.putEDBObjectEntry("child", "child3");

        EDBObject child3 = new EDBObject("child3");
        child3.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child3.putEDBObjectEntry(EDBConstants.MODEL_OID, "child3");
        child3.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child3.putEDBObjectEntry("id", "child3");

        when(mockedService.getObject(eq("child1"), anyLong())).thenReturn(child1);
        when(mockedService.getObject(eq("child2"), anyLong())).thenReturn(child2);
        when(mockedService.getObject(eq("child3"), anyLong())).thenReturn(child3);

        // test
        RecursiveModel mRoot = converter.convertEDBObjectToModel(RecursiveModel.class, root);
        // assert
        assertThat(mRoot.getId(), is("root"));

        RecursiveModel mChild1 = mRoot.getChild();
        assertThat(mChild1, notNullValue());
        assertThat(mChild1.getId(), is("child1"));
        RecursiveModel mChild2 = mChild1.getChild();
        assertThat(mChild2, notNullValue());
        assertThat(mChild2.getId(), is("child2"));
        RecursiveModel mChild3 = mChild2.getChild();
        assertThat(mChild3, notNullValue());
        assertThat(mChild3.getId(), is("child3"));
    }

    @Test
    public void testEDBObjectToRecursiveModelWithCompositionConversion_shouldWork() throws Exception {
        // prepare
        EDBObject root = new EDBObject("root");
        root.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        root.putEDBObjectEntry(EDBConstants.MODEL_OID, "root");
        root.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        root.putEDBObjectEntry("id", "root");
        root.putEDBObjectEntry("children.0", "child1");
        root.putEDBObjectEntry("children.1", "child2");

        EDBObject child1 = new EDBObject("child1");
        child1.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child1.putEDBObjectEntry(EDBConstants.MODEL_OID, "child1");
        child1.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child1.putEDBObjectEntry("id", "child1");
        child1.putEDBObjectEntry("children.0", "child3");
        child1.putEDBObjectEntry("children.1", "child4");

        EDBObject child2 = new EDBObject("child2");
        child2.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child2.putEDBObjectEntry(EDBConstants.MODEL_OID, "child2");
        child2.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child2.putEDBObjectEntry("id", "child2");

        EDBObject child3 = new EDBObject("child3");
        child3.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child3.putEDBObjectEntry(EDBConstants.MODEL_OID, "child3");
        child3.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child3.putEDBObjectEntry("id", "child3");

        EDBObject child4 = new EDBObject("child4");
        child4.putEDBObjectEntry(EDBConstants.MODEL_TYPE, RecursiveModel.class.getName());
        child4.putEDBObjectEntry(EDBConstants.MODEL_OID, "child4");
        child4.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        child4.putEDBObjectEntry("id", "child4");

        when(mockedService.getObject("child1")).thenReturn(child1);
        when(mockedService.getObject("child2")).thenReturn(child2);
        when(mockedService.getObject("child3")).thenReturn(child3);
        when(mockedService.getObject("child4")).thenReturn(child4);

        // test
        RecursiveModel mRoot = converter.convertEDBObjectToModel(RecursiveModel.class, root);

        // assert
        RecursiveModel mChild1;
        RecursiveModel mChild2;
        RecursiveModel mChild3;
        RecursiveModel mChild4;
        List<RecursiveModel> rootChildren;
        List<RecursiveModel> child1Children;

        assertThat(mRoot.getId(), is("root"));
        rootChildren = mRoot.getChildren();
        assertThat(rootChildren, notNullValue());
        assertThat(rootChildren.size(), is(2));

        mChild1 = rootChildren.get(0);
        mChild2 = rootChildren.get(1);

        assertThat(mChild1.getId(), is("child1"));
        assertThat(mChild2.getId(), is("child2"));

        child1Children = mChild1.getChildren();
        assertThat(child1Children, notNullValue());
        assertThat(child1Children.size(), is(2));

        mChild3 = child1Children.get(0);
        mChild4 = child1Children.get(1);

        assertThat(mChild3.getId(), is("child3"));
        assertThat(mChild4.getId(), is("child4"));
    }

    @Test
    public void testIfEngineeringObjectInformationIsAdded_shouldAddEOInformation() throws Exception {
        EngineeringObjectModel model = new EngineeringObjectModel();
        model.setModelAId("testReferenceToModelA");
        model.setModelBId("testReferenceToModelB");

        List<EDBObject> objects = converter.convertModelToEDBObject(model, getTestConnectorInformation());
        assertThat(objects.size(), is(1));
        EDBObject result = objects.get(0);
        String key1 = getReferenceString(model.getClass(), "modelAId");
        String key2 = getReferenceString(model.getClass(), "modelBId");
        assertThat(result.getString(key1), is(CONTEXT_ID + "/testReferenceToModelA"));
        assertThat(result.getString(key2), is(CONTEXT_ID + "/testReferenceToModelB"));
    }

    @Test
    public void testIfEngineeringObjectInformationIsDeleted_shouldDeleteEOInformation() throws Exception {
        EDBObject object = new EDBObject("test");
        object.putEDBObjectEntry(EDBConstants.MODEL_TYPE, EngineeringObjectModel.class.getName());
        object.putEDBObjectEntry(EDBConstants.MODEL_OID, "test");
        object.putEDBObjectEntry(EDBConstants.MODEL_VERSION, Integer.valueOf(1));
        String key1 = getReferenceString(EngineeringObjectModel.class, "modelAId");
        String key2 = getReferenceString(EngineeringObjectModel.class, "modelBId");
        object.putEDBObjectEntry(key1, "testReferenceA");
        object.putEDBObjectEntry(key2, "testReferenceB");
        converter.convertEDBObjectToModel(EngineeringObjectModel.class, object);
        assertThat(object.get(key1), nullValue());
        assertThat(object.get(key2), nullValue());
    }

    private String getReferenceString(Class<?> model, String field) throws Exception {
        return EDBConverter.getEOReferenceStringFromAnnotation(model.getDeclaredField(field).getAnnotation(
            OpenEngSBForeignKey.class));
    }

    private ConnectorInformation getTestConnectorInformation() {
        return new ConnectorInformation("testdomain", "testconnector", "testinstance");
    }
}
TOP

Related Classes of org.openengsb.core.ekb.common.EDBConverterTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.