View Javadoc

1   /**
2    * Copyright 2005-2013 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.krms.api.repository;
17  
18  import org.junit.Assert;
19  import org.junit.Test;
20  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
21  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
22  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
23  import javax.xml.bind.JAXBContext;
24  import javax.xml.bind.Marshaller;
25  import javax.xml.bind.Unmarshaller;
26  import java.io.StringReader;
27  import java.io.StringWriter;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public final class NaturalLanguageTemplateGenTest {
36  
37      private final static String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
38              + "<ns2:naturalLanguageTemplate xmlns:ns2=\"http://rice.kuali.org/krms/v2_0\" xmlns=\"http://rice.kuali.org/core/v2_0\">\n"
39              + "    <ns2:attributes/>\n"
40              + "    <ns2:languageCode>NL</ns2:languageCode>\n"
41              + "    <ns2:naturalLanguageUsageId>NATURAL_LANGUAGE_USAGE_ID</ns2:naturalLanguageUsageId>\n"
42              + "    <ns2:typeId>TYPE_ID</ns2:typeId>\n"
43              + "    <ns2:template>TEMPLATE</ns2:template>\n"
44              + "    <ns2:id>ID</ns2:id>\n"
45              + "    <ns2:active>false</ns2:active>\n"
46              + "</ns2:naturalLanguageTemplate>";
47      private final static boolean ACTIVE = false;
48      private final static String ID = "ID";
49      private final static String LANGUAGE_CODE = "NL";
50      private final static String NATURAL_LANGUAGE_USAGE_ID = "NATURAL_LANGUAGE_USAGE_ID";
51      private final static String TEMPLATE = "TEMPLATE";
52      private final static String TYPE_ID = "TYPE_ID";
53      private final static Long VERSION_NUMBER = 0L;
54  
55      @Test(expected = java.lang.IllegalArgumentException.class)
56      public void test_NaturalLanguageTemplate_Builder_create_fail_all_null() {
57          NaturalLanguageTemplate.Builder.create(null, null, null, null);
58      }
59  
60      @Test
61      public void test_NaturalLanguageTemplate_set_validation_id_success_null() {
62          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
63          builder.setId(null);
64      }
65  
66      @Test(expected = java.lang.IllegalArgumentException.class)
67      public void test_NaturalLanguageTemplate_set_validation_id_fail_empty() {
68          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
69          builder.setId("");
70      }
71  
72      @Test(expected = java.lang.IllegalArgumentException.class)
73      public void test_NaturalLanguageTemplate_set_validation_id_fail_whitespace() {
74          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
75          builder.setId("    ");
76      }
77  
78      @Test(expected = java.lang.IllegalArgumentException.class)
79      public void test_NaturalLanguageTemplate_set_validation_language_fail_null() {
80          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
81          builder.setLanguageCode(null);
82      }
83  
84      @Test(expected = java.lang.IllegalArgumentException.class)
85      public void test_NaturalLanguageTemplate_set_validation_language_fail_empty() {
86          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
87          builder.setLanguageCode("");
88      }
89  
90      @Test(expected = java.lang.IllegalArgumentException.class)
91      public void test_NaturalLanguageTemplate_set_validation_language_fail_whitespace() {
92          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
93          builder.setLanguageCode("    ");
94      }
95  
96      @Test(expected = java.lang.IllegalArgumentException.class)
97      public void test_NaturalLanguageTemplate_set_validation_naturalLanguageUsageId_fail_null() {
98          NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
99          builder.setNaturalLanguageUsageId(null);
100     }
101 
102     @Test(expected = java.lang.IllegalArgumentException.class)
103     public void test_NaturalLanguageTemplate_set_validation_naturalLanguageUsageId_fail_empty() {
104         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
105         builder.setNaturalLanguageUsageId("");
106     }
107 
108     @Test(expected = java.lang.IllegalArgumentException.class)
109     public void test_NaturalLanguageTemplate_set_validation_naturalLanguageUsageId_fail_whitespace() {
110         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
111         builder.setNaturalLanguageUsageId("    ");
112     }
113 
114     @Test(expected = java.lang.IllegalArgumentException.class)
115     public void test_NaturalLanguageTemplate_set_validation_template_fail_null() {
116         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
117         builder.setTemplate(null);
118     }
119 
120     @Test(expected = java.lang.IllegalArgumentException.class)
121     public void test_NaturalLanguageTemplate_set_validation_template_fail_empty() {
122         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
123         builder.setTemplate("");
124     }
125 
126     @Test(expected = java.lang.IllegalArgumentException.class)
127     public void test_NaturalLanguageTemplate_set_validation_template_fail_whitespace() {
128         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
129         builder.setTemplate("    ");
130     }
131 
132     @Test(expected = java.lang.IllegalArgumentException.class)
133     public void test_NaturalLanguageTemplate_set_validation_typeId_fail_null() {
134         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
135         builder.setTypeId(null);
136     }
137 
138     @Test(expected = java.lang.IllegalArgumentException.class)
139     public void test_NaturalLanguageTemplate_set_validation_typeId_fail_empty() {
140         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
141         builder.setTypeId("");
142     }
143 
144     @Test(expected = java.lang.IllegalArgumentException.class)
145     public void test_NaturalLanguageTemplate_set_validation_typeId_fail_whitespace() {
146         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
147         builder.setTypeId("    ");
148     }
149 
150     @Test
151     public void test_NaturalLanguageTemplate_Builder_create() {
152         NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
153     }
154 
155     @Test
156     public void test_NaturalLanguageTemplate_Builder_create_and_build() {
157         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE,
158                 NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
159         builder.build();
160     }
161 
162     @Test
163     public void test_NaturalLanguageTemplate_xml_marshaling()
164         throws Exception
165     {
166         NaturalLanguageTemplate naturalLanguageTemplate = buildFullNaturalLanguageTemplate();
167 
168         assertXmlMarshaling(naturalLanguageTemplate, XML);
169     }
170 
171     public void assertXmlMarshaling(Object naturalLanguageTemplate, String expectedXml)
172         throws Exception
173     {
174         JAXBContext jc = JAXBContext.newInstance(NaturalLanguageTemplate.class);
175 
176         Marshaller marshaller = jc.createMarshaller();
177         StringWriter stringWriter = new StringWriter();
178         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
179         // marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new CustomNamespacePrefixMapper());
180         marshaller.marshal(naturalLanguageTemplate, stringWriter);
181         String xml = stringWriter.toString();
182 
183 //        System.out.println(xml); // run test, paste xml output into XML, comment out this line.
184 
185         Unmarshaller unmarshaller = jc.createUnmarshaller();
186         Object actual = unmarshaller.unmarshal(new StringReader(xml));
187         Object expected = unmarshaller.unmarshal(new StringReader(expectedXml));
188         Assert.assertEquals(expected, actual);
189     }
190 
191     public static NaturalLanguageTemplate buildFullNaturalLanguageTemplate() {
192         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, NATURAL_LANGUAGE_USAGE_ID, TEMPLATE, TYPE_ID);
193         builder.setId(ID);
194         NaturalLanguageTemplate naturalLanguageTemplate = builder.build();
195         return naturalLanguageTemplate;
196     }
197 
198     public static NaturalLanguageTemplate buildFullNaturalLanguageTemplate(NaturalLanguageUsage depDef, KrmsTypeDefinition typeDef) { // TODO gen
199         NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(LANGUAGE_CODE, depDef.getId(), TEMPLATE, typeDef.getId());
200         builder.setId(ID);
201         Map<String, String> attributes = new HashMap<String, String>();
202         attributes.put("TEMPLATE", "template");
203         builder.setAttributes(attributes);
204         NaturalLanguageTemplate naturalLanguageTemplate = builder.build();
205         return naturalLanguageTemplate;
206     }
207 }