/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.workflow.kaleo.upgrade.v1_3_4.test;
import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.portal.kernel.dao.db.DB;
import com.liferay.portal.kernel.dao.db.DBManagerUtil;
import com.liferay.portal.kernel.dao.jdbc.DataAccess;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
import com.liferay.portal.kernel.test.util.GroupTestUtil;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.kernel.test.util.TestPropsValues;
import com.liferay.portal.kernel.upgrade.UpgradeProcess;
import com.liferay.portal.kernel.upgrade.UpgradeStep;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import com.liferay.portal.upgrade.registry.UpgradeStepRegistrator;
import com.liferay.portal.workflow.kaleo.model.KaleoDefinitionVersion;
import com.liferay.portal.workflow.kaleo.service.KaleoDefinitionVersionLocalServiceUtil;
import com.liferay.registry.Registry;
import com.liferay.registry.RegistryUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* @author InĂ¡cio Nery
*/
@RunWith(Arquillian.class)
public class UpgradeKaleoDefinitionVersionTest {
@ClassRule
@Rule
public static final AggregateTestRule aggregateTestRule =
new LiferayIntegrationTestRule();
@Before
public void setUp() throws Exception {
_group = GroupTestUtil.addGroup();
_name = StringUtil.randomString();
_timestamp = new Timestamp(System.currentTimeMillis());
setUpUpgradeKaleoDefinitionVersion();
}
@After
public void tearDown() throws Exception {
deleteKaleoDefinitionVersion(_name);
}
@Test
public void testCreateKaleoDefinitionVersion() throws Exception {
addKaleoDefinition(_name, 1);
_upgradeKaleoDefinitionVersion.upgrade();
getKaleoDefinitionVersion(_name, 1);
}
protected void addKaleoDefinition(String name, int version)
throws Exception {
StringBundler sb = new StringBundler(5);
sb.append("insert into KaleoDefinition (kaleoDefinitionId, groupId, ");
sb.append("companyId, userId, userName, createDate, modifiedDate, ");
sb.append("name, title, description, content, version, active_, ");
sb.append("startKaleoNodeId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
sb.append("?, ?, ?, ?)");
String sql = sb.toString();
try (Connection con = DataAccess.getUpgradeOptimizedConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setLong(1, RandomTestUtil.randomLong());
ps.setLong(2, _group.getGroupId());
ps.setLong(3, _group.getCompanyId());
ps.setLong(4, TestPropsValues.getUserId());
ps.setString(5, TestPropsValues.getUser().getFullName());
ps.setTimestamp(6, _timestamp);
ps.setTimestamp(7, _timestamp);
ps.setString(8, name);
ps.setString(9, StringUtil.randomString());
ps.setString(10, StringUtil.randomString());
ps.setString(11, StringUtil.randomString());
ps.setInt(12, version);
ps.setBoolean(13, true);
ps.setLong(14, 0);
ps.executeUpdate();
}
}
protected void deleteKaleoDefinitionVersion(String name) throws Exception {
DB db = DBManagerUtil.getDB();
db.runSQL(
"delete from KaleoDefinitionVersion where name = '" + name + "'");
db.runSQL("delete from KaleoDefinition where name = '" + name + "'");
}
protected KaleoDefinitionVersion getKaleoDefinitionVersion(
String name, int version)
throws PortalException {
return KaleoDefinitionVersionLocalServiceUtil.getKaleoDefinitionVersion(
_group.getCompanyId(), name, getVersion(version));
}
protected String getVersion(int version) {
return version + StringPool.PERIOD + 0;
}
protected void setUpUpgradeKaleoDefinitionVersion() {
Registry registry = RegistryUtil.getRegistry();
UpgradeStepRegistrator upgradeStepRegistror = registry.getService(
"com.liferay.portal.workflow.kaleo.internal.upgrade." +
"KaleoServiceUpgrade");
upgradeStepRegistror.register(
new UpgradeStepRegistrator.Registry() {
@Override
public void register(
String bundleSymbolicName, String fromSchemaVersionString,
String toSchemaVersionString, UpgradeStep... upgradeSteps) {
for (UpgradeStep upgradeStep : upgradeSteps) {
Class<?> clazz = upgradeStep.getClass();
String className = clazz.getName();
if (className.contains(
"UpgradeKaleoDefinitionVersion")) {
_upgradeKaleoDefinitionVersion =
(UpgradeProcess)upgradeStep;
}
}
}
});
}
@DeleteAfterTestRun
private Group _group;
private String _name;
private Timestamp _timestamp;
private UpgradeProcess _upgradeKaleoDefinitionVersion;
}