/**
* 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.asset.publisher.util.test;
import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.asset.kernel.model.AssetQueryRule;
import com.liferay.asset.publisher.web.util.AssetPublisherUtil;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.kernel.test.rule.Sync;
import com.liferay.portal.kernel.test.rule.SynchronousDestinationTestRule;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.portlet.PortletPreferences;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mock.web.portlet.MockPortletPreferences;
/**
* @author Eudaldo Alonso
*/
@RunWith(Arquillian.class)
@Sync
public class AssetPublisherUtilTest {
@ClassRule
@Rule
public static final AggregateTestRule aggregateTestRule =
new AggregateTestRule(
new LiferayIntegrationTestRule(),
SynchronousDestinationTestRule.INSTANCE);
@Test
public void testGetAssetCategoryIdsContainsAllCategories()
throws Exception {
long assetCategoryId1 = RandomTestUtil.nextLong();
long assetCategoryId2 = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, true, "assetCategories",
new String[] {
String.valueOf(assetCategoryId1),
String.valueOf(assetCategoryId2)
});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 2, assetCategoryIds.length);
Assert.assertEquals(assetCategoryId1, assetCategoryIds[0]);
Assert.assertEquals(assetCategoryId2, assetCategoryIds[1]);
}
@Test
public void testGetAssetCategoryIdsContainsAllCategory() throws Exception {
long assetCategoryId = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, true, "assetCategories",
new String[] {String.valueOf(assetCategoryId)});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 1, assetCategoryIds.length);
Assert.assertEquals(assetCategoryId, assetCategoryIds[0]);
}
@Test
public void testGetAssetCategoryIdsContainsAnyCategories()
throws Exception {
long assetCategoryId1 = RandomTestUtil.nextLong();
long assetCategoryId2 = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, false, "assetCategories",
new String[] {
String.valueOf(assetCategoryId1),
String.valueOf(assetCategoryId2)
});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 0, assetCategoryIds.length);
}
@Test
public void testGetAssetCategoryIdsContainsAnyCategory() throws Exception {
long assetCategoryId = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, false, "assetCategories",
new String[] {String.valueOf(assetCategoryId)});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 1, assetCategoryIds.length);
Assert.assertEquals(assetCategoryId, assetCategoryIds[0]);
}
@Test
public void testGetAssetCategoryIdsNotContainsAllCategories()
throws Exception {
long assetCategoryId1 = RandomTestUtil.nextLong();
long assetCategoryId2 = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, true, "assetCategories",
new String[] {
String.valueOf(assetCategoryId1),
String.valueOf(assetCategoryId2)
});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 0, assetCategoryIds.length);
}
@Test
public void testGetAssetCategoryIdsNotContainsAllCategory()
throws Exception {
long assetCategoryId = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, true, "assetCategories",
new String[] {String.valueOf(assetCategoryId)});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 0, assetCategoryIds.length);
}
@Test
public void testGetAssetCategoryIdsNotContainsAnyCategories()
throws Exception {
long assetCategoryId1 = RandomTestUtil.nextLong();
long assetCategoryId2 = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, false, "assetCategories",
new String[] {
String.valueOf(assetCategoryId1),
String.valueOf(assetCategoryId2)
});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 0, assetCategoryIds.length);
}
@Test
public void testGetAssetCategoryIdsNotContainsAnyCategory()
throws Exception {
long assetCategoryId = RandomTestUtil.nextLong();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, false, "assetCategories",
new String[] {String.valueOf(assetCategoryId)});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
long[] assetCategoryIds = AssetPublisherUtil.getAssetCategoryIds(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetCategoryIds), 0, assetCategoryIds.length);
}
@Test
public void testGetAssetTagNamesContainsAllTagName() throws Exception {
String assetTagName = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, true, "assetTags", new String[] {assetTagName});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 1, assetTagNames.length);
Assert.assertEquals(assetTagName, assetTagNames[0]);
}
@Test
public void testGetAssetTagNamesContainsAllTagNames() throws Exception {
String assetTagName1 = RandomTestUtil.randomString();
String assetTagName2 = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, true, "assetTags",
new String[] {assetTagName1, assetTagName2});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 2, assetTagNames.length);
Assert.assertEquals(assetTagName1, assetTagNames[0]);
Assert.assertEquals(assetTagName2, assetTagNames[1]);
}
@Test
public void testGetAssetTagNamesContainsAnyTagName() throws Exception {
String assetTagName = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, false, "assetTags", new String[] {assetTagName});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 1, assetTagNames.length);
Assert.assertEquals(assetTagName, assetTagNames[0]);
}
@Test
public void testGetAssetTagNamesContainsAnyTagNames() throws Exception {
String assetTagName1 = RandomTestUtil.randomString();
String assetTagName2 = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
true, false, "assetTags",
new String[] {assetTagName1, assetTagName2});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 0, assetTagNames.length);
}
@Test
public void testGetAssetTagNamesNotContainsAllTagName() throws Exception {
String assetTagName = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, true, "assetTags", new String[] {assetTagName});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 0, assetTagNames.length);
}
@Test
public void testGetAssetTagNamesNotContainsAllTagNames() throws Exception {
String assetTagName1 = RandomTestUtil.randomString();
String assetTagName2 = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, true, "assetTags",
new String[] {assetTagName1, assetTagName2});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 0, assetTagNames.length);
}
@Test
public void testGetAssetTagNamesNotContainsAnyTagName() throws Exception {
String assetTagName = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, false, "assetTags", new String[] {assetTagName});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 0, assetTagNames.length);
}
@Test
public void testGetAssetTagNamesNotContainsAnyTagNames() throws Exception {
String assetTagName1 = RandomTestUtil.randomString();
String assetTagName2 = RandomTestUtil.randomString();
AssetQueryRule assetQueryRule = new AssetQueryRule(
false, false, "assetTags",
new String[] {assetTagName1, assetTagName2});
List<AssetQueryRule> assetQueryRules = new ArrayList();
assetQueryRules.add(assetQueryRule);
PortletPreferences portletPreferences =
getAssetPublisherPortletPreferences(assetQueryRules);
String[] assetTagNames = AssetPublisherUtil.getAssetTagNames(
portletPreferences);
Assert.assertEquals(
Arrays.toString(assetTagNames), 0, assetTagNames.length);
}
protected PortletPreferences getAssetPublisherPortletPreferences(
List<AssetQueryRule> assetQueryRules)
throws Exception {
PortletPreferences portletPreferences = new MockPortletPreferences();
for (int i = 0; i < assetQueryRules.size(); i++) {
AssetQueryRule assetQueryRule = assetQueryRules.get(i);
portletPreferences.setValue(
"queryAndOperator" + i,
String.valueOf(assetQueryRule.isAndOperator()));
portletPreferences.setValue(
"queryContains" + i,
String.valueOf(assetQueryRule.isContains()));
portletPreferences.setValue(
"queryName" + i, assetQueryRule.getName());
portletPreferences.setValues(
"queryValues" + i, assetQueryRule.getValues());
}
return portletPreferences;
}
}