001/**
002 * Copyright 2005-2014 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/ecl2.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.krms.impl.repository;
017
018import org.junit.Before;
019import org.junit.Test;
020import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
021import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
022import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
023import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
024import org.kuali.rice.krms.test.AbstractBoTest;
025import org.kuali.rice.test.BaselineTestCase;
026
027import java.util.Iterator;
028import java.util.Map;
029
030/**
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 *
033 */
034@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
035public final class NaturalLanguageTemplateIntegrationGenTest extends AbstractBoTest {
036
037    NaturalLanguageTemplateBoServiceImpl naturalLanguageTemplateBoServiceImpl;
038    NaturalLanguageTemplate naturalLanguageTemplate;
039    KrmsAttributeDefinitionService krmsAttributeDefinitionService;
040    KrmsTypeBoServiceImpl krmsTypeBoServiceImpl;
041    NaturalLanguageUsageBoServiceImpl naturalLanguageUsageBoServiceImpl; // TODO gen
042
043    NaturalLanguageTemplate getNaturalLanguageTemplate() {
044        return naturalLanguageTemplate;
045    }
046
047    /**
048     * Note lower case u, do not override superclasses setUp
049     *
050     */
051    @Before
052    public void setup() {
053        naturalLanguageTemplateBoServiceImpl = new NaturalLanguageTemplateBoServiceImpl();
054        naturalLanguageTemplateBoServiceImpl.setNaturalLanguageTemplater(newStringReplaceTemplater()); // TODO gen?
055        krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
056        naturalLanguageTemplateBoServiceImpl.setAttributeDefinitionService(krmsAttributeDefinitionService);
057        naturalLanguageTemplateBoServiceImpl.setDataObjectService(getDataObjectService());
058        krmsTypeBoServiceImpl = new KrmsTypeBoServiceImpl();
059        krmsTypeBoServiceImpl.setDataObjectService(getDataObjectService());
060        naturalLanguageUsageBoServiceImpl = new NaturalLanguageUsageBoServiceImpl();
061        naturalLanguageUsageBoServiceImpl.setDataObjectService(getDataObjectService());
062    }
063
064    public static NaturalLanguageTemplaterContract newStringReplaceTemplater() {
065        return new NaturalLanguageTemplaterContract() {
066            @Override
067            public String translate(NaturalLanguageTemplate naturalLanguageTemplate, Map<String,Object>variables) {
068                String template = naturalLanguageTemplate.getTemplate();
069                Map<String, String> attributes = naturalLanguageTemplate.getAttributes();
070                Iterator<String> keys = attributes.keySet().iterator();
071                while (keys.hasNext()) {
072                    String key = keys.next();
073                    template.replace(key, attributes.get(key));
074                }
075                return template;
076            }
077        };
078    }
079
080    @Test(expected = java.lang.IllegalArgumentException.class)
081    public void test_getNaturalLanguageTemplatesByAttributes_null_fail() {
082        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_getNaturalLanguageTemplatesByAttributes_null_fail();
083    }
084
085    @Test(expected = java.lang.IllegalArgumentException.class)
086    public void test_getNaturalLanguageTemplatesByLanguageCode_null_fail() {
087        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_getNaturalLanguageTemplatesByLanguageCode_null_fail();
088    }
089
090    @Test(expected = java.lang.IllegalArgumentException.class)
091    public void test_getNaturalLanguageTemplatesByNaturalLanguageUsage_null_fail() {
092        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_getNaturalLanguageTemplatesByNaturalLanguageUsage_null_fail();
093    }
094
095    @Test(expected = java.lang.IllegalArgumentException.class)
096    public void test_getNaturalLanguageTemplatesByType_null_fail() {
097        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_getNaturalLanguageTemplatesByType_null_fail();
098    }
099
100    @Test(expected = java.lang.IllegalArgumentException.class)
101    public void test_getNaturalLanguageTemplatesByTemplate_null_fail() {
102        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_getNaturalLanguageTemplatesByTemplate_null_fail();
103    }
104
105    @Test
106    public void test_from_null_yields_null() {
107        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_from_null_yields_null();
108    }
109
110    @Test
111    public void test_to() {
112        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_to();
113    }
114
115    @Test
116    public void test_from() { // TODO gen
117        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_from();
118    }
119
120    public void test_createNaturalLanguageTemplate() {
121        KrmsTypeIntegrationGenTest krmsTypeTest = new KrmsTypeIntegrationGenTest();
122        krmsTypeTest.setup(); // Note lowercase u
123        krmsTypeTest.test_createKrmsType();
124        KrmsTypeDefinition krmsType = krmsTypeTest.getKrmsType();
125        NaturalLanguageUsageIntegrationGenTest naturalLanguageUsageTest = new NaturalLanguageUsageIntegrationGenTest();
126        naturalLanguageUsageTest.setup(); // Note lowercase u
127        naturalLanguageUsageTest.test_createNaturalLanguageUsage();
128        NaturalLanguageUsage naturalLanguageUsage = naturalLanguageUsageTest.getNaturalLanguageUsage();
129        NaturalLanguageTemplateBoServiceImplGenTest test = NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService);
130        test.createNaturalLanguageTemplate(naturalLanguageUsage, krmsType); // TODO gen order of params
131        naturalLanguageTemplate = test.getNaturalLanguageTemplate();
132        assert(naturalLanguageTemplate != null);
133        assert(naturalLanguageTemplate.getId() != null);
134        assert(naturalLanguageTemplate.getAttributes() != null && !naturalLanguageTemplate.getAttributes().isEmpty());
135    }
136
137    @Test // TODO gen use multiple catches
138    public void test_createNaturalLanguageTemplate_fail_existing() {
139        test_createNaturalLanguageTemplate();
140        try {
141            test_createNaturalLanguageTemplate();
142        } catch (IllegalStateException ise) {
143            return; // local
144        } catch (Exception e) {
145            return; // ci database specific Exception so catch Exception to avoid depending on driver classes
146        }
147        assert(false); // exception should be thrown
148    }
149
150    @Test(expected = java.lang.IllegalArgumentException.class)
151    public void test_createNaturalLanguageTemplate_null_fail() {
152        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_createNaturalLanguageTemplate_null_fail();
153    }
154
155    public void test_getNaturalLanguageTemplate() {
156        test_createNaturalLanguageTemplate();
157        NaturalLanguageTemplate def = getNaturalLanguageTemplate();
158        NaturalLanguageTemplate def2 = naturalLanguageTemplateBoServiceImpl.getNaturalLanguageTemplate(def.getId());
159        assert(def2 != null);
160        assert(def2.equals(def));
161    }
162
163    public void test_updateNaturalLanguageTemplate() {
164        test_createNaturalLanguageTemplate();
165        NaturalLanguageTemplate def = getNaturalLanguageTemplate();
166        String id = def.getId();
167        assert(!"UpdateTest".equals(def.getTemplate()));
168        NaturalLanguageTemplateBo bo = naturalLanguageTemplateBoServiceImpl.from(def);
169        bo.setTemplate("UpdateTest");
170        naturalLanguageTemplateBoServiceImpl.updateNaturalLanguageTemplate(NaturalLanguageTemplate.Builder.create(bo).build());
171        NaturalLanguageTemplate def2 = naturalLanguageTemplateBoServiceImpl.getNaturalLanguageTemplate(id);
172        assert("UpdateTest".equals(def2.getTemplate()));
173    }
174
175    @Test(expected = java.lang.IllegalArgumentException.class)
176    public void test_updateNaturalLanguageTemplate_null_fail() {
177        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_updateNaturalLanguageTemplate_null_fail();
178    }
179
180    public void test_deleteNaturalLanguageTemplate() {
181        test_createNaturalLanguageTemplate();
182        NaturalLanguageTemplate def = getNaturalLanguageTemplate();
183        String id = def.getId();
184        naturalLanguageTemplateBoServiceImpl.deleteNaturalLanguageTemplate(id);
185        NaturalLanguageTemplate def2 = naturalLanguageTemplateBoServiceImpl.getNaturalLanguageTemplate(id);
186        assert(def2 == null);
187    }
188
189    @Test(expected = java.lang.IllegalArgumentException.class)
190    public void test_deleteNaturalLanguageTemplate_null_fail() {
191        (NaturalLanguageTemplateBoServiceImplGenTest.create(naturalLanguageTemplateBoServiceImpl, krmsAttributeDefinitionService)).test_deleteNaturalLanguageTemplate_null_fail();
192    }
193}