001/*
002 * Copyright 2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl1.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.data;
017
018import org.apache.log4j.Level;
019import org.junit.Before;
020import org.junit.Test;
021import org.kuali.rice.core.api.data.DataType;
022import org.kuali.rice.krad.datadictionary.AttributeDefinition;
023import org.kuali.rice.krad.datadictionary.DataObjectEntry;
024import org.kuali.rice.krad.datadictionary.RelationshipDefinition;
025import org.kuali.rice.krad.test.KRADTestCase;
026import org.kuali.rice.krad.test.TestDictionaryConfig;
027import org.kuali.rice.krad.uif.control.TextControl;
028
029import static org.junit.Assert.*;
030
031/**
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034@TestDictionaryConfig(namespaceCode="KR-NS",dataDictionaryFiles="classpath:org/kuali/rice/krad/test/datadictionary/TestDataObject.xml")
035public class DataDictionaryMetadataDefaultingTest extends KRADTestCase {
036
037    protected static final String MAIN_DATA_OBJECT_FOR_TESTING = "org.kuali.rice.krad.data.jpa.testbo.TestDataObject";
038
039    @Override
040    @Before
041    public void setUp() throws Exception {
042        setLogLevel("org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl", Level.DEBUG);
043        setLogLevel("org.kuali.rice.krad.data.jpa.JpaMetadataProviderImpl", Level.DEBUG);
044        setLogLevel("org.kuali.rice.krad.data.provider.spring.SpringMetadataProviderImpl", Level.DEBUG);
045        setLogLevel("org.kuali.rice.krad.data.provider.impl.CompositeMetadataProviderImpl", Level.DEBUG);
046
047        setLogLevel("org.kuali.rice.krad.datadictionary.AttributeDefinition", Level.DEBUG);
048        setLogLevel("org.kuali.rice.krad.datadictionary.DataObjectEntry", Level.DEBUG);
049        setLogLevel("org.kuali.rice.krad.datadictionary.AttributeDefinitionBase", Level.DEBUG);
050        setLogLevel("org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase", Level.DEBUG);
051
052        super.setUp();
053
054        assertNotNull( "DD not set - configuration error!!!", dd );
055    }
056
057    @Test
058    public void verifyInRegistry() {
059        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
060        assertNotNull( "No embedded metadata object for TestDataObject", dataObjectEntry.getDataObjectMetadata() );
061    }
062
063    @Test
064    public void verifyLabelOverride() {
065        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
066
067        assertEquals("Label for DO not pulled from metadata: " + dataObjectEntry + "\n", "A Spring-Provided Label",
068                dataObjectEntry.getObjectLabel());
069    }
070
071    @Test
072    public void verifyUifObjectDescriptionOverride() {
073        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
074
075        assertEquals("Description not overridden", "UIF-Provided Description", dataObjectEntry.getObjectDescription());
076    }
077
078    @Test
079    public void verifyAttributeLabelOverrideFromSpringMetadata() {
080        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
081        AttributeDefinition attributeDefinition = dataObjectEntry.getAttributeDefinition("nonStandardDataType");
082        assertNotNull("nonStandardDataType attribute did not exist",attributeDefinition);
083        assertEquals(
084                "Label on nonStandardDataType attribute not pulled from Spring metadata: " + attributeDefinition + "\n",
085                "Non Standard Label-Spring", attributeDefinition.getLabel());
086    }
087
088    @Test
089    public void verifyAttributesExist() {
090        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
091
092        assertNotNull("attribute list should not have been null", dataObjectEntry.getAttributes() );
093        assertFalse("attribute list should not have been empty", dataObjectEntry.getAttributes().isEmpty());
094
095        assertNotNull("stringProperty should have been present in the list", dataObjectEntry.getAttributeDefinition("stringProperty") );
096    }
097
098    @Test
099    public void verifyAttributeProvidedAttributes() {
100        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
101
102        AttributeDefinition attributeDefinition = dataObjectEntry.getAttributeDefinition("stringProperty");
103        assertEquals("springProperty label incorrect", "Attribute Label From Annotation",
104                attributeDefinition.getLabel());
105        assertEquals("springProperty data type incorrect", DataType.STRING, attributeDefinition.getDataType());
106    }
107
108    public void verifyTitleAttribute() {
109        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
110
111        assertEquals("title attribute property incorrect", "primaryKeyProperty", dataObjectEntry.getTitleAttribute());
112    }
113
114    @Test
115    public void verifyPrimaryKey() {
116        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
117
118        assertNotNull("PK list should not have been null", dataObjectEntry.getPrimaryKeys() );
119        assertFalse("PK list should not have been empty", dataObjectEntry.getPrimaryKeys().isEmpty());
120        assertEquals("PK list length wrong", 1, dataObjectEntry.getPrimaryKeys().size());
121        assertEquals("PK field incorrect", "primaryKeyProperty", dataObjectEntry.getPrimaryKeys().get(0));
122    }
123
124    @Test
125    public void verifyDefaultedControl_stringProperty() {
126        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
127        String propertyName = "stringProperty";
128        AttributeDefinition attributeDefinition = dataObjectEntry.getAttributeDefinition(propertyName);
129        assertNotNull(propertyName + " should have been present in the attribute list", attributeDefinition );
130
131        assertNotNull( "the ControlField should not have been null", attributeDefinition.getControlField() );
132        assertTrue("Type of control field is incorrect", attributeDefinition.getControlField() instanceof TextControl);
133        assertEquals("Size of control is incorrect", 40,
134                ((TextControl) attributeDefinition.getControlField()).getSize());
135        assertNotNull( "MaxLength of control is missing", ((TextControl) attributeDefinition.getControlField()).getMaxLength() );
136        assertEquals("MaxLength of control is incorrect", 40,
137                ((TextControl) attributeDefinition.getControlField()).getMaxLength().intValue());
138        assertEquals("textExpand property incorrect", false,
139                ((TextControl) attributeDefinition.getControlField()).isTextExpand());
140    }
141
142    @Test
143    public void verifyDefaultedControl_longStringProperty() {
144        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
145        String propertyName = "longStringProperty";
146        AttributeDefinition attributeDefinition = dataObjectEntry.getAttributeDefinition(propertyName);
147        assertNotNull(propertyName + " should have been present in the attribute list", attributeDefinition );
148
149        assertNotNull( "the ControlField should not have been null", attributeDefinition.getControlField() );
150        assertTrue("Type of control field is incorrect", attributeDefinition.getControlField() instanceof TextControl);
151        assertEquals("Size of control is incorrect", 200,
152                ((TextControl) attributeDefinition.getControlField()).getSize());
153        assertNotNull( "MaxLength of control is missing", ((TextControl) attributeDefinition.getControlField()).getMaxLength() );
154        assertEquals("MaxLength of control is incorrect", 200,
155                ((TextControl) attributeDefinition.getControlField()).getMaxLength().intValue());
156        assertEquals("textExpand property incorrect", true,
157                ((TextControl) attributeDefinition.getControlField()).isTextExpand());
158    }
159
160    @Test
161    public void verifyDefaultedControl_dateProperty() {
162        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
163        String propertyName = "dateProperty";
164        AttributeDefinition attributeDefinition = dataObjectEntry.getAttributeDefinition(propertyName);
165        assertNotNull(propertyName + " should have been present in the attribute list", attributeDefinition );
166
167        assertNotNull( "the ControlField should not have been null", attributeDefinition.getControlField() );
168        assertTrue("Type of control field is incorrect", attributeDefinition.getControlField() instanceof TextControl);
169        assertNotNull( "control field is missing a datepicker", ((TextControl) attributeDefinition.getControlField()).getDatePicker() );
170    }
171
172    @Test
173    public void verifyDefaultedRelationship_existence() {
174        DataObjectEntry dataObjectEntry = getDataObjectEntry(MAIN_DATA_OBJECT_FOR_TESTING);
175        String relationshipName = "referencedObject";
176        RelationshipDefinition relationshipDefinition = dataObjectEntry.getRelationshipDefinition(relationshipName);
177        assertNotNull(relationshipName + " should have been present in the relationship list", relationshipDefinition );
178    }
179
180    protected DataObjectEntry getDataObjectEntry( String dataObjectClassName ) {
181        DataObjectEntry dataObjectEntry = dd.getDataObjectEntry(dataObjectClassName);
182        LOG.info("Loading DataObjectEntry for : " + dataObjectClassName);
183        assertNotNull( "Unable to retrieve data object entry for : " + dataObjectClassName, dataObjectEntry );
184        LOG.info( "DataObjectEntry: " + dataObjectEntry);
185        return dataObjectEntry;
186    }
187
188}