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.portal.lar;
016    
017    import com.liferay.portal.kernel.lar.ManifestSummary;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataContextFactoryUtil;
020    import com.liferay.portal.kernel.lar.PortletDataHandler;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.util.LongWrapper;
023    import com.liferay.portal.kernel.util.Time;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.kernel.zip.ZipWriter;
027    import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.test.DeleteAfterTestRun;
030    import com.liferay.portal.util.test.GroupTestUtil;
031    import com.liferay.portlet.PortletPreferencesImpl;
032    
033    import java.util.Date;
034    import java.util.HashMap;
035    import java.util.LinkedHashMap;
036    import java.util.Map;
037    
038    import org.junit.Assert;
039    import org.junit.Before;
040    import org.junit.Test;
041    
042    /**
043     * @author Zsolt Berentey
044     */
045    public abstract class BasePortletDataHandlerTestCase {
046    
047            @Before
048            public void setUp() throws Exception {
049                    stagingGroup = GroupTestUtil.addGroup();
050    
051                    portletDataHandler = createPortletDataHandler();
052                    portletId = getPortletId();
053            }
054    
055            @Test
056            public void testPrepareManifestSummary() throws Exception {
057                    initExport();
058    
059                    addStagedModels();
060    
061                    portletDataContext.setEndDate(getEndDate());
062    
063                    portletDataHandler.prepareManifestSummary(portletDataContext);
064    
065                    ManifestSummary manifestSummary =
066                            portletDataContext.getManifestSummary();
067    
068                    Map<String, LongWrapper> modelAdditionCounters =
069                            manifestSummary.getModelAdditionCounters();
070    
071                    Map<String, LongWrapper> expectedModelAdditionCounters =
072                            new HashMap<String, LongWrapper>(modelAdditionCounters);
073    
074                    modelAdditionCounters.clear();
075    
076                    portletDataHandler.exportData(
077                            portletDataContext, portletId, new PortletPreferencesImpl());
078    
079                    checkManifestSummary(expectedModelAdditionCounters);
080            }
081    
082            protected void addBooleanParameter(
083                    Map<String, String[]> parameterMap, String namespace, String name,
084                    boolean value) {
085    
086                    PortletDataHandlerBoolean portletDataHandlerBoolean =
087                            new PortletDataHandlerBoolean(namespace, name);
088    
089                    parameterMap.put(
090                            portletDataHandlerBoolean.getNamespacedControlName(),
091                            new String[] {String.valueOf(value)});
092            }
093    
094            protected void addParameters(Map<String, String[]> parameterMap) {
095            }
096    
097            protected abstract void addStagedModels() throws Exception;
098    
099            protected void checkManifestSummary(
100                    Map<String, LongWrapper> expectedModelAdditionCounters) {
101    
102                    ManifestSummary manifestSummary =
103                            portletDataContext.getManifestSummary();
104    
105                    Map<String, LongWrapper> modelAdditionCounters =
106                            manifestSummary.getModelAdditionCounters();
107    
108                    int expectedModelAdditionCountersSize =
109                            expectedModelAdditionCounters.size();
110    
111                    for (String manifestSummaryKey :
112                                    expectedModelAdditionCounters.keySet()) {
113    
114                            LongWrapper expectedModelAdditionCounter =
115                                    expectedModelAdditionCounters.get(manifestSummaryKey);
116                            LongWrapper modelAdditionCounter = modelAdditionCounters.get(
117                                    manifestSummaryKey);
118    
119                            if ((expectedModelAdditionCounter.getValue() == 0) &&
120                                    (modelAdditionCounter == null)) {
121    
122                                    expectedModelAdditionCountersSize--;
123                            }
124                            else {
125                                    Assert.assertEquals(
126                                            expectedModelAdditionCounter.getValue(),
127                                            modelAdditionCounter.getValue());
128                            }
129                    }
130    
131                    Assert.assertEquals(
132                            expectedModelAdditionCountersSize, modelAdditionCounters.size());
133            }
134    
135            protected abstract PortletDataHandler createPortletDataHandler();
136    
137            protected Date getEndDate() {
138                    return new Date();
139            }
140    
141            protected abstract String getPortletId();
142    
143            protected Date getStartDate() {
144                    return new Date(System.currentTimeMillis() - Time.HOUR);
145            }
146    
147            protected void initExport() throws Exception {
148                    Map<String, String[]> parameterMap =
149                            new LinkedHashMap<String, String[]>();
150    
151                    addParameters(parameterMap);
152    
153                    zipWriter = ZipWriterFactoryUtil.getZipWriter();
154    
155                    portletDataContext =
156                            PortletDataContextFactoryUtil.createExportPortletDataContext(
157                                    stagingGroup.getCompanyId(), stagingGroup.getGroupId(),
158                                    parameterMap, getStartDate(), getEndDate(), zipWriter);
159    
160                    rootElement = SAXReaderUtil.createElement("root");
161    
162                    portletDataContext.setExportDataRootElement(rootElement);
163    
164                    missingReferencesElement = SAXReaderUtil.createElement(
165                            "missing-references");
166    
167                    portletDataContext.setMissingReferencesElement(
168                            missingReferencesElement);
169            }
170    
171            protected Element missingReferencesElement;
172            protected PortletDataContext portletDataContext;
173            protected PortletDataHandler portletDataHandler;
174            protected String portletId;
175            protected Element rootElement;
176    
177            @DeleteAfterTestRun
178            protected Group stagingGroup;
179    
180            protected ZipWriter zipWriter;
181    
182    }