View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.provider.annotation.impl;
17  
18  import org.apache.log4j.BasicConfigurator;
19  import org.apache.log4j.Level;
20  import org.apache.log4j.Logger;
21  import org.junit.Before;
22  import org.junit.BeforeClass;
23  import org.junit.Test;
24  import org.kuali.rice.krad.data.DataObjectService;
25  import org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl;
26  import org.kuali.rice.krad.data.jpa.testbo.ReferencedDataObject;
27  import org.kuali.rice.krad.data.jpa.testbo.SomeOtherCollection;
28  import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
29  import org.kuali.rice.krad.data.metadata.DataObjectAttribute;
30  import org.kuali.rice.krad.data.metadata.DataObjectCollection;
31  import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
32  import org.kuali.rice.krad.data.provider.MetadataProvider;
33  import org.kuali.rice.krad.data.provider.impl.CompositeMetadataProviderImpl;
34  import org.mockito.Mockito;
35  
36  import javax.persistence.EntityManagerFactory;
37  import javax.persistence.Persistence;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import static org.junit.Assert.*;
42  
43  public class AnnotationMetadataProviderImplTest {
44  
45      static DataObjectService dataObjectService;
46  	static EclipseLinkJpaMetadataProviderImpl jpaMetadataProvider;
47  	CompositeMetadataProviderImpl compositeProvider;
48  	AnnotationMetadataProviderImpl annotationMetadataProvider;
49  
50  	@BeforeClass
51  	public static void setUpBeforeClass() throws Exception {
52  		BasicConfigurator.configure();
53  		Logger.getLogger(CompositeMetadataProviderImpl.class).setLevel(Level.DEBUG);
54  		Logger.getLogger(AnnotationMetadataProviderImpl.class).setLevel(Level.DEBUG);
55  		Logger.getLogger(EclipseLinkJpaMetadataProviderImpl.class).setLevel(Level.DEBUG);
56          dataObjectService = Mockito.mock(DataObjectService.class);
57  		jpaMetadataProvider = new EclipseLinkJpaMetadataProviderImpl();
58  		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("krad-data-unit-test");
59  		jpaMetadataProvider.setEntityManager(entityManagerFactory.createEntityManager());
60  	}
61  
62  	@Before
63  	public void setUp() throws Exception {
64  		annotationMetadataProvider = new AnnotationMetadataProviderImpl();
65          annotationMetadataProvider.setDataObjectService(dataObjectService);
66  		ArrayList<MetadataProvider> providers = new ArrayList<MetadataProvider>();
67  		providers.add(jpaMetadataProvider);
68  		providers.add(annotationMetadataProvider);
69  		compositeProvider = new CompositeMetadataProviderImpl();
70  		compositeProvider.setProviders(providers);
71  	}
72  
73  	@Test
74  	public void testInitializeMetadataNoTypesProvided() {
75  		AnnotationMetadataProviderImpl provider = new AnnotationMetadataProviderImpl();
76          provider.initializeMetadata(null);
77          assertTrue(provider.isInitializationAttempted());
78  		assertTrue(provider.getSupportedTypes().isEmpty());
79          assertFalse(provider.handles(TestDataObject.class));
80  	}
81  
82  	@Test
83  	public void testProvideMetadata() {
84  		assertNotNull("Metadata map should not be null", compositeProvider.provideMetadata());
85  		assertFalse("Metadata map should not have been empty", compositeProvider.provideMetadata().isEmpty());
86  		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
87  				TestDataObject.class);
88  		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
89  		assertEquals("Label not read properly from annotation metadata provider", "Label From Annotation",
90  				metadata.getLabel());
91  		assertFalse("Attributes should still be present from the JPA metadata", metadata.getAttributes()
92  				.isEmpty());
93  		System.err.println(metadata);
94  	}
95  
96  	@Test
97  	public void testBusinessKeyMetadata() {
98  		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
99  				TestDataObject.class);
100 		assertEquals("With no definition, the PK list and business key list should have been equal",
101 				metadata.getPrimaryKeyAttributeNames(), metadata.getBusinessKeyAttributeNames());
102 
103 		metadata = compositeProvider.provideMetadata().get(ReferencedDataObject.class);
104 		assertNotEquals("When @BusinessKey used, the PK list and business key list should not have been equal",
105 				metadata.getPrimaryKeyAttributeNames(), metadata.getBusinessKeyAttributeNames());
106 
107 		assertEquals(
108 				"When a business key is defined, the primary display attribute should be the last field on that",
109 				"someOtherStringProperty", metadata.getPrimaryDisplayAttributeName());
110 	}
111 
112 	@Test
113 	public void testMergedAttribute() {
114 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
115 				TestDataObject.class);
116 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
117 		List<DataObjectAttribute> attributes = metadata.getAttributes();
118 		assertFalse("Attributes should still be present from the JPA metadata", attributes.isEmpty());
119 
120 		assertNotNull("getAttribute(stringProperty) should not have returned null",
121 				metadata.getAttribute("stringProperty"));
122 
123 		assertEquals("getAttribute(nonPersistedProperty) label incorrect", "Attribute Label From Annotation",
124 				metadata.getAttribute("stringProperty").getLabel());
125 	}
126 
127 	@Test
128 	public void testNonPersistableProperty() {
129 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
130 				TestDataObject.class);
131 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
132 		System.err.println(metadata);
133 		DataObjectAttribute attr = metadata.getAttribute("keyAndString");
134 		assertNotNull("keyAndString property does not exist", attr);
135 		assertEquals("keyAndString label incorrect", "Test Data Object", attr.getLabel());
136 	}
137 
138 	@Test
139 	public void testInheritedProperties() {
140 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
141 				TestDataObject.class);
142 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
143 		String propName = "referencedObject.someOtherStringProperty";
144 		DataObjectAttribute attr = metadata.getAttribute(propName);
145 		assertNotNull("getAttribute(" + propName + ") should not have returned null", attr);
146 		assertTrue("Attribute should have isInherited", attr.isInherited());
147 		assertEquals("Inherited data object type not set", ReferencedDataObject.class, attr.getInheritedFromType());
148 		assertEquals("Inherited data object parent attribute not set", "referencedObject",
149 				attr.getInheritedFromParentAttributeName());
150 		assertEquals("Inherited data object attribute not set", "someOtherStringProperty",
151 				attr.getInheritedFromAttributeName());
152 		assertEquals("Label incorrect", "RDOs Business Key", attr.getLabel());
153 	}
154 
155 	@Test
156 	public void testOrderingOfInheritedProperties() {
157 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(TestDataObject.class);
158 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
159 
160 		// Find the property right before this one - should be nonStandardDataType
161 		List<DataObjectAttribute> attributes = metadata.getAttributes();
162 		int indexOfPriorProperty = -1;
163 		for (int i = 0; i < attributes.size(); i++) {
164 			if (attributes.get(i).getName().equals("nonStandardDataType")) {
165 				indexOfPriorProperty = i;
166 				break;
167 			}
168 		}
169 		assertFalse("Unable to find nonStandardDataType Property", indexOfPriorProperty == -1);
170 		assertFalse("nonStandardDataType should not have been the last property",
171 				indexOfPriorProperty + 1 == attributes.size());
172 
173 		DataObjectAttribute attr = attributes.get(indexOfPriorProperty + 1);
174 		assertEquals("Property property after nonStandardDataType not correct: ",
175 				"referencedObject.someOtherStringProperty", attr.getName());
176 	}
177 
178 	@Test
179 	public void testInheritedProperties_labelOverride() {
180 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
181 				TestDataObject.class);
182 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
183 		assertNotNull(
184 				"getAttribute(anotherReferencedObject.someOtherStringProperty) should not have returned null",
185 				metadata.getAttribute("anotherReferencedObject.someOtherStringProperty"));
186 		assertEquals("Label incorrect", "Overridden Inherited Property Label",
187 				metadata.getAttribute("anotherReferencedObject.someOtherStringProperty").getLabel());
188 	}
189 
190 	@Test
191 	public void testReadOnlyAnnotation() {
192 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
193 				TestDataObject.class);
194 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
195 		assertNotNull("getAttribute(readOnlyProperty) should not have returned null",
196 				metadata.getAttribute("readOnlyProperty"));
197 		assertEquals("readonly flag not set", true, metadata.getAttribute("readOnlyProperty").isReadOnly());
198 	}
199 
200 	@Test
201 	public void testForceUppercaseAnnotation() {
202 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(TestDataObject.class);
203 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
204 		assertNotNull("getAttribute(primaryKeyProperty) should not have returned null",
205 				metadata.getAttribute("primaryKeyProperty"));
206 		assertTrue("forceUppercase flag not set", metadata.getAttribute("primaryKeyProperty")
207 				.isForceUppercase());
208 		assertNotNull("getAttribute(readOnlyProperty) should not have returned null",
209 				metadata.getAttribute("readOnlyProperty"));
210 		assertFalse("forceUppercase flag should not have been set", metadata.getAttribute("readOnlyProperty")
211 				.isForceUppercase());
212 	}
213 
214 	@Test
215 	public void testCollectionAnnotation_derivedType() {
216 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(
217 				TestDataObject.class);
218 		assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
219 		DataObjectCollection collection = metadata.getCollection("someOtherCollection");
220 		assertNotNull("someOtherCollection not defined", collection);
221 		System.err.println(collection);
222 		assertEquals("element class is incorrect", SomeOtherCollection.class, collection.getRelatedType());
223 		assertEquals("collection label incorrect", "Some Other Collection", collection.getLabel());
224 		assertEquals("collection item label incorrect", "Some Other Collection", collection.getElementLabel());
225 		assertNotNull("attribute relationships must not be null", collection.getAttributeRelationships());
226 		assertEquals("Wrong number of relationship columns", 1, collection.getAttributeRelationships().size());
227 		assertEquals("Parent attribute name incorrect", "dateProperty", collection.getAttributeRelationships().get(0)
228 				.getParentAttributeName());
229 		assertEquals("Child attribute name incorrect", "collectionDateProperty", collection.getAttributeRelationships()
230 				.get(0).getChildAttributeName());
231 		// assertEquals( "Sort order incorrect when unspecified", SortDirection.ASCENDING,
232 		// collection.getDefaultCollectionOrdering().get(0).getSortDirection() );
233 	}
234 }