View Javadoc
1   /**
2    * Copyright 2005-2014 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.datadictionary;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.util.KeyValue;
20  import org.kuali.rice.krad.messages.MessageService;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.test.TestDictionaryBean;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.kuali.rice.test.data.PerSuiteUnitTestData;
25  import org.kuali.rice.test.data.UnitTestData;
26  import org.kuali.rice.test.data.UnitTestFile;
27  import org.kuali.rice.krad.test.KRADTestCase;
28  import org.kuali.rice.krad.test.KRADTestConstants;
29  import org.kuali.rice.krad.test.TestDictionaryConfig;
30  
31  import java.util.List;
32  
33  import static org.junit.Assert.assertEquals;
34  
35  /**
36   * Test cases for org.kuali.rice.krad.datadictionary.MessageBeanProcessor
37   *
38   * <p>
39   * Note these test methods are simply verifying (not assembling and invoking) because the actual
40   * processing of external messages will be done by the creation of the data dictionary in the KRAD
41   * base test case
42   * </p>
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  @PerSuiteUnitTestData(
47          value = @UnitTestData(
48                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
49                  sqlFiles = {@UnitTestFile(filename = "classpath:testExternalMessages.sql", delimiter = ";")}))
50  @TestDictionaryConfig(
51          namespaceCode = "TEST",
52          dataDictionaryFiles = "classpath:org/kuali/rice/krad/datadictionary/MessageProcessorTestBeans.xml")
53  public class MessageBeanProcessorTest extends KRADTestCase {
54  
55      /**
56       * Verifies a message setup with component equal to a bean id and message key
57       * a simple property path (non-nested) is picked up and inserted into the correct bean definition
58       * and property
59       */
60      @Test
61      public void testMatchBeanIdSimpleKey() throws Exception {
62          TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesSimpleProperty");
63  
64          assertEquals("Property1 was not overridden with external message", "ext p1 value", tObject.getProperty1());
65          assertEquals("Property2 was not overridden with external message", "ext p2 value", tObject.getProperty2());
66      }
67  
68      @Test
69      public void testMatchExplicitKey() throws Exception {
70          TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesMessageKey");
71  
72          assertEquals("Property1 was not overridden with explicit message key (with default)", "ext key p1 value",
73                  tObject.getProperty1());
74          assertEquals("Property2 was not overridden with explicit message key (without default)", "ext key p2 value",
75                  tObject.getProperty2());
76      }
77  
78      @Test
79      public void testMatchExplicitListKey() throws Exception {
80          TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesListMessageKey");
81  
82          assertEquals("List1 entry 2 was not overridden with explicit message key", "ext list key value",
83                  tObject.getList1().get(1));
84      }
85  
86      @Test
87      public void testMatchExplicitMapKey() throws Exception {
88          TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesMapMessageKey");
89  
90          assertEquals("Map1 key 'k2' was not overridden with explicit message key", "ext map key value",
91                  tObject.getMap1().get("k2"));
92      }
93  
94      @Test
95      public void testMessageExpressionMerge() throws Exception {
96          TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesExpressionMerge");
97  
98          assertEquals("Expression for property 1 was not merged", "Value '@{expr1}' is invalid",
99                  tObject.getExpressionGraph().get("property1"));
100         assertEquals("Expression for property 2 was not merged", "The @{expr1} code should not equal @{expr2 + 3}",
101                 tObject.getExpressionGraph().get("property2"));
102 
103         tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesKeyExprMerge");
104 
105         assertEquals("Expression for property 1 was not merged with explicit message key",
106                 "Expr @{expr1} then expr @{expr2}", tObject.getExpressionGraph().get("property1"));
107     }
108 
109     @Test
110     public void testMessagesForListBean() throws Exception {
111         List<KeyValue> keyValues = (List<KeyValue>) getTestDictionaryObject("TestMessagesOptions");
112 
113         assertEquals("First key value not overridden by message", "Ext Summer", keyValues.get(0).getValue());
114         assertEquals("Second key value was overriden and should not be", "Fall", keyValues.get(1).getValue());
115         assertEquals("Third key value not overridden by message", "Ext Spring", keyValues.get(2).getValue());
116     }
117 
118     @Test
119     public void testModuleResourcesMessages() throws Exception {
120         TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestMessagesResources");
121 
122         assertEquals("Property 1 value was not overridden with message resource", "Message for Property 1",
123                 tObject.getProperty1());
124         assertEquals("Property 2 value with explicit key was not overridden with message resource",
125                 "Message for explicit message key", tObject.getProperty2());
126 
127         // get validation message by key
128         String messageText = getMessageService().getMessageText(KRADTestConstants.TEST_NAMESPACE_CODE, "",
129                 "validation.test.error");
130         assertEquals("Validation message text not correct", "Error found for {0}",
131                 messageText);
132     }
133     
134     @Test
135     public void testApplicationResourcesMessages() throws Exception {
136         TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestApplicationMessagesResources");
137 
138         assertEquals("Property 1 value was not overridden with message resource", "App Message for Property 1",
139                 tObject.getProperty1());
140         assertEquals("Property 2 value with explicit key was not overridden with message resource",
141                 "App Message for explicit message key", tObject.getProperty2());
142 
143         // get validation message by key
144         String messageText = getMessageService().getMessageText(KRADConstants.DEFAULT_NAMESPACE, "",
145                 "validation.test.error");
146         assertEquals("Validation message text not correct", "App Error found for {0}",
147                 messageText);
148     }
149 
150     /**
151      * Verifies a message keys within an expression get replaced with the text
152      */
153     @Test
154     public void testExpressionMessages() throws Exception {
155         TestDictionaryBean tObject = (TestDictionaryBean) getTestDictionaryObject("TestExpressionMessages");
156 
157         assertEquals("Messages in expression for property 1 not replaced",
158                 "@{foo eq '1' ? 'ext key p1 value' : 'ext key p2 value'}", tObject.getExpressionGraph().get(
159                 "property1"));
160     }
161 
162     public MessageService getMessageService() {
163         return KRADServiceLocatorWeb.getMessageService();
164 
165     }
166 
167 }