001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.service.test;
016    
017    import com.liferay.portal.kernel.locale.test.LocaleTestUtil;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
020    import com.liferay.portal.kernel.test.util.GroupTestUtil;
021    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
022    import com.liferay.portal.kernel.test.util.TestPropsValues;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayout;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
033    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
034    import com.liferay.portlet.dynamicdatamapping.storage.StorageType;
035    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
036    import com.liferay.portlet.dynamicdatamapping.util.test.DDMStructureLayoutTestHelper;
037    import com.liferay.portlet.dynamicdatamapping.util.test.DDMStructureTestHelper;
038    
039    import org.junit.Before;
040    
041    /**
042     * @author Eduardo Garcia
043     */
044    public class BaseDDMServiceTestCase {
045    
046            @Before
047            public void setUp() throws Exception {
048                    group = GroupTestUtil.addGroup();
049    
050                    ddmStructureTestHelper = new DDMStructureTestHelper(group);
051                    ddmStructureLayoutTestHelper = new DDMStructureLayoutTestHelper(group);
052            }
053    
054            protected DDMTemplate addDisplayTemplate(
055                            long classNameId, long classPK, long sourceClassNameId, String name,
056                            String description)
057                    throws Exception {
058    
059                    String language = TemplateConstants.LANG_TYPE_VM;
060    
061                    return addTemplate(
062                            classNameId, classPK, sourceClassNameId, StringPool.BLANK, name,
063                            description, DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
064                            StringPool.BLANK, language, getTestTemplateScript(language));
065            }
066    
067            protected DDMTemplate addDisplayTemplate(
068                            long classNameId, long classPK, String name)
069                    throws Exception {
070    
071                    String language = TemplateConstants.LANG_TYPE_VM;
072    
073                    return addTemplate(
074                            classNameId, classPK, name,
075                            DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, StringPool.BLANK,
076                            language, getTestTemplateScript(language));
077            }
078    
079            protected DDMTemplate addDisplayTemplate(long classPK, String name)
080                    throws Exception {
081    
082                    return addDisplayTemplate(
083                            PortalUtil.getClassNameId(DDMStructure.class), classPK, name);
084            }
085    
086            protected DDMTemplate addFormTemplate(long classPK, String name)
087                    throws Exception {
088    
089                    return addFormTemplate(classPK, name, getTestTemplateScript("xsd"));
090            }
091    
092            protected DDMTemplate addFormTemplate(
093                            long classPK, String name, String definition)
094                    throws Exception {
095    
096                    return addTemplate(
097                            PortalUtil.getClassNameId(DDMStructure.class), classPK, name,
098                            DDMTemplateConstants.TEMPLATE_TYPE_FORM,
099                            DDMTemplateConstants.TEMPLATE_MODE_CREATE, "xsd", definition);
100            }
101    
102            protected DDMStructure addStructure(
103                            long parentStructureId, long classNameId, String structureKey,
104                            String name, String definition, String storageType, int type)
105                    throws Exception {
106    
107                    return addStructure(
108                            parentStructureId, classNameId, structureKey, name,
109                            StringPool.BLANK, definition, storageType, type);
110            }
111    
112            protected DDMStructure addStructure(
113                            long parentStructureId, long classNameId, String structureKey,
114                            String name, String description, String definition,
115                            String storageType, int type)
116                    throws Exception {
117    
118                    DDMForm ddmForm = ddmStructureTestHelper.toDDMForm(definition);
119    
120                    DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
121    
122                    return ddmStructureTestHelper.addStructure(
123                            parentStructureId, classNameId, structureKey, name, description,
124                            ddmForm, ddmFormLayout, storageType, type);
125            }
126    
127            protected DDMStructure addStructure(long classNameId, String name)
128                    throws Exception {
129    
130                    return addStructure(classNameId, name, null);
131            }
132    
133            protected DDMStructure addStructure(
134                            long classNameId, String name, String description)
135                    throws Exception {
136    
137                    return addStructure(
138                            0, classNameId, null, name, description, read("test-structure.xsd"),
139                            StorageType.JSON.getValue(), DDMStructureConstants.TYPE_DEFAULT);
140            }
141    
142            protected DDMStructure addStructure(
143                            long classNameId, String structureKey, String name,
144                            String definition, String storageType, int type)
145                    throws Exception {
146    
147                    DDMForm ddmForm = ddmStructureTestHelper.toDDMForm(definition);
148    
149                    return ddmStructureTestHelper.addStructure(
150                            classNameId, structureKey, name, ddmForm, storageType, type);
151            }
152    
153            protected DDMTemplate addTemplate(
154                            long classNameId, long classPK, long sourceClassNameId,
155                            String templateKey, String name, String description, String type,
156                            String mode, String language, String script)
157                    throws Exception {
158    
159                    return DDMTemplateLocalServiceUtil.addTemplate(
160                            TestPropsValues.getUserId(), group.getGroupId(), classNameId,
161                            classPK, sourceClassNameId, templateKey,
162                            LocaleTestUtil.getDefaultLocaleMap(name),
163                            LocaleTestUtil.getDefaultLocaleMap(description), type, mode,
164                            language, script, false, false, null, null,
165                            ServiceContextTestUtil.getServiceContext());
166            }
167    
168            protected DDMTemplate addTemplate(
169                            long classNameId, long classPK, String name, String type,
170                            String mode, String language, String script)
171                    throws Exception {
172    
173                    return addTemplate(
174                            classNameId, classPK, null, name, type, mode, language, script);
175            }
176    
177            protected DDMTemplate addTemplate(
178                            long classNameId, long classPK, String templateKey, String name,
179                            String type, String mode, String language, String script)
180                    throws Exception {
181    
182                    return addTemplate(
183                            classNameId, classPK, 0, templateKey, name, StringPool.BLANK, type,
184                            mode, language, script);
185            }
186    
187            protected String getBasePath() {
188                    return "com/liferay/portlet/dynamicdatamapping/dependencies/";
189            }
190    
191            protected String getTestTemplateScript(String language) throws Exception {
192                    String text = StringPool.BLANK;
193    
194                    if (language.equals(TemplateConstants.LANG_TYPE_FTL)) {
195                            text = "${variable}";
196                    }
197                    else if (language.equals(TemplateConstants.LANG_TYPE_VM)) {
198                            text = "#set ($preferences = $renderRequest.getPreferences())";
199                    }
200                    else if (language.equals("xsd")) {
201                            text = read("test-template.xsd");
202                    }
203    
204                    return text;
205            }
206    
207            protected String read(String fileName) throws Exception {
208                    Class<?> clazz = getClass();
209    
210                    return StringUtil.read(
211                            clazz.getClassLoader(), getBasePath() + fileName);
212            }
213    
214            protected DDMStructureLayoutTestHelper ddmStructureLayoutTestHelper;
215            protected DDMStructureTestHelper ddmStructureTestHelper;
216    
217            @DeleteAfterTestRun
218            protected Group group;
219    
220    }