/**
* This file Copyright (c) 2011-2012 Magnolia International
* Ltd. (http://www.magnolia-cms.com). All rights reserved.
*
*
* This file is dual-licensed under both the Magnolia
* Network Agreement and the GNU General Public License.
* You may elect to use one or the other of these licenses.
*
* This file is distributed in the hope that it will be
* useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
* Redistribution, except as permitted by whichever of the GPL
* or MNA you select, is prohibited.
*
* 1. For the GPL license (GPL), you can redistribute and/or
* modify this file under the terms of the GNU General
* Public License, Version 3, as published by the Free Software
* Foundation. You should have received a copy of the GNU
* General Public License, Version 3 along with this program;
* if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* 2. For the Magnolia Network Agreement (MNA), this file
* and the accompanying materials are made available under the
* terms of the MNA which accompanies this distribution, and
* is available at http://www.magnolia-cms.com/mna.html
*
* Any modifications to this file must keep this entire header
* intact.
*
*/
package info.magnolia.rendering.generator;
import static info.magnolia.rendering.template.AutoGenerationConfiguration.NODE_TYPE;
import static info.magnolia.rendering.template.AutoGenerationConfiguration.TEMPLATE_ID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import info.magnolia.cms.security.User;
import info.magnolia.context.Context;
import info.magnolia.context.MgnlContext;
import info.magnolia.jcr.util.NodeTypes;
import info.magnolia.rendering.engine.RenderException;
import info.magnolia.rendering.template.AutoGenerationConfiguration;
import info.magnolia.repository.RepositoryConstants;
import info.magnolia.test.RepositoryTestCase;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* When Mock objects refactoring will be completed, we will probably be able to get rid of it.
* Clean up and simplify messy creation of nested map returned by AutoGenerationConfiguration.
*/
public class CopyGeneratorTest extends RepositoryTestCase {
protected static final String TEMPLATE_ID_VALUE = "foo:/bar/baz";
protected static final String USER_NAME = "leobrouwer";
protected Session session;
@Override
@Before
public void setUp() throws Exception{
super.setUp();
session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
session.getRootNode().addNode("foo", NodeTypes.Content.NAME);
Context context = mock(Context.class);
MgnlContext.setInstance(context);
User user = mock(User.class);
when(user.getName()).thenReturn(USER_NAME);
when(context.getUser()).thenReturn(user);
}
/*
* We expect a structure like the following will be created, where "foo" already exists.
*
* + foo
* + autogen-foo
*/
@Test
public void testSameLevelNodesCreation() throws Exception{
//GIVEN
Node parent = session.getNode("/foo");
AutoGenerationConfiguration config = mock(AutoGenerationConfiguration.class);
Map<String, Object> content = new HashMap<String, Object>();
Map<String, Object> firstNodeProps = new HashMap<String, Object>();
firstNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
firstNodeProps.put(TEMPLATE_ID, null);
firstNodeProps.put("anotherProp", "some value");
content.put("autogen-foo", firstNodeProps);
Map<String, Object> secondNodeProps = new HashMap<String, Object>();
secondNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
secondNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
secondNodeProps.put("someProp", "a different value");
content.put("same-level-autogen", secondNodeProps);
when(config.getContent()).thenReturn(content);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN
Node newNode = session.getNode("/foo/autogen-foo");
assertNodeAndMetaData(newNode, null, USER_NAME);
assertPropertyEquals(newNode, "anotherProp", "some value", PropertyType.STRING);
Node secondNode = session.getNode("/foo/same-level-autogen");
assertNodeAndMetaData(secondNode, TEMPLATE_ID_VALUE, USER_NAME);
assertPropertyEquals(secondNode, "someProp", "a different value", PropertyType.STRING);
}
/*
* We expect a structure like the following will be created, where "foo" already exists.
*
* + foo
* + autogen-foo
* + nested-autogen
*/
@Test
public void testNestedNodesCreation() throws Exception {
//GIVEN
Node parent = session.getNode("/foo");
AutoGenerationConfiguration config = mock(AutoGenerationConfiguration.class);
Map<String, Object> content = new HashMap<String, Object>();
Map<String, Object> firstNodeProps = new HashMap<String, Object>();
firstNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
firstNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
firstNodeProps.put("anotherProp", "some value");
Map<String, Object> nestedNodeProps = new HashMap<String, Object>();
nestedNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
nestedNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
nestedNodeProps.put("someProp", "a different value");
Map<String, Object> nestedSubNodeProps = new HashMap<String, Object>();
nestedSubNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
nestedSubNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
nestedNodeProps.put("nestedSubNode-autogen", nestedSubNodeProps);
firstNodeProps.put("nested-autogen", nestedNodeProps);
content.put("autogen-foo", firstNodeProps);
when(config.getContent()).thenReturn(content);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN
Node newNode = session.getNode("/foo/autogen-foo");
assertNodeAndMetaData(newNode, TEMPLATE_ID_VALUE, USER_NAME);
assertPropertyEquals(newNode, "anotherProp", "some value", PropertyType.STRING);
Node secondNode = session.getNode("/foo/autogen-foo/nested-autogen");
assertNodeAndMetaData(secondNode, TEMPLATE_ID_VALUE, USER_NAME);
assertPropertyEquals(secondNode, "someProp", "a different value", PropertyType.STRING);
Node secondSubNode = session.getNode("/foo/autogen-foo/nested-autogen/nestedSubNode-autogen");
assertNodeAndMetaData(secondSubNode, TEMPLATE_ID_VALUE, USER_NAME);
}
/*
* We expect a structure like the following will be created, where "foo" already exists.
*
* + foo
* + autogen-foo
* + same-level-autogen-foo
* + nested-autogen
* + same-level-as-nested
*/
@Test
public void testSameLevelNestedNodesCreation() throws Exception {
//GIVEN
Node parent = session.getNode("/foo");
AutoGenerationConfiguration config = mock(AutoGenerationConfiguration.class);
Map<String, Object> content = new HashMap<String, Object>();
Map<String, Object> firstNodeProps = new HashMap<String, Object>();
firstNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
firstNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
content.put("autogen-foo", firstNodeProps);
Map<String, Object> sameLevelNodeProps = new HashMap<String, Object>();
sameLevelNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
sameLevelNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
content.put("same-level-autogen-foo", sameLevelNodeProps);
Map<String, Object> nestedNodeProps = new HashMap<String, Object>();
nestedNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
nestedNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
firstNodeProps.put("nested-autogen", nestedNodeProps);
Map<String, Object> sameLevelNestedNodeProps = new HashMap<String, Object>();
sameLevelNestedNodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
sameLevelNestedNodeProps.put(TEMPLATE_ID, TEMPLATE_ID_VALUE);
firstNodeProps.put("same-level-as-nested", sameLevelNestedNodeProps);
when(config.getContent()).thenReturn(content);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN
Node newNode = session.getNode("/foo/autogen-foo");
assertNodeAndMetaData(newNode, TEMPLATE_ID_VALUE, USER_NAME);
Node secondNode = session.getNode("/foo/same-level-autogen-foo");
assertNodeAndMetaData(secondNode, TEMPLATE_ID_VALUE, USER_NAME);
Node nestedNode = session.getNode("/foo/autogen-foo/nested-autogen");
assertNodeAndMetaData(nestedNode, TEMPLATE_ID_VALUE, USER_NAME);
Node sameLevelAsNested = session.getNode("/foo/autogen-foo/same-level-as-nested");
assertNodeAndMetaData(sameLevelAsNested, TEMPLATE_ID_VALUE, USER_NAME);
}
@Test(expected=RenderException.class)
public void testGenerateThrowsRenderExceptionIfNodeTypeIsNotFound() throws Exception {
//GIVEN
Node parent = session.getNode("/foo");
AutoGenerationConfiguration config = mock(AutoGenerationConfiguration.class);
Map<String, Object> content = new HashMap<String, Object>();
Map<String, Object> nodeConfig = new HashMap<String, Object>();
nodeConfig.put("foo", "bar");
content.put("autogen-foo", nodeConfig);
when(config.getContent()).thenReturn(content);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN throws RenderException
}
@Test
public void testNewPropertyValueIsNotOverwritten() throws Exception{
//GIVEN
Node parent = session.getNode("/foo");
AutoGenerationConfiguration config = mock(AutoGenerationConfiguration.class);
Map<String, Object> content = new HashMap<String, Object>();
Map<String, Object> nodeProps = new HashMap<String, Object>();
nodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
nodeProps.put(TEMPLATE_ID, null);
nodeProps.put("someProp", "original value");
content.put("autogen-foo", nodeProps);
when(config.getContent()).thenReturn(content);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN
Node newNode = session.getNode("/foo/autogen-foo");
assertPropertyEquals(newNode, "someProp", "original value", PropertyType.STRING);
//GIVEN
newNode.getProperty("someProp").setValue("a different value");
newNode.getSession().save();
assertPropertyEquals(newNode, "someProp", "a different value", PropertyType.STRING);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN
assertPropertyEquals(newNode, "someProp", "a different value", PropertyType.STRING);
}
@Test
public void testCreateDifferentPropertyTypes() throws Exception{
//GIVEN
Node parent = session.getNode("/foo");
AutoGenerationConfiguration config = mock(AutoGenerationConfiguration.class);
Map<String, Object> content = new HashMap<String, Object>();
Map<String, Object> nodeProps = new HashMap<String, Object>();
nodeProps.put(NODE_TYPE, NodeTypes.ContentNode.NAME);
nodeProps.put(TEMPLATE_ID, null);
nodeProps.put("stringProp", "a string");
nodeProps.put("booleanProp", true);
nodeProps.put("longProp", 100L);
nodeProps.put("doubleProp", 3.14d);
nodeProps.put("calendarProp", Calendar.getInstance());
content.put("autogen-foo", nodeProps);
when(config.getContent()).thenReturn(content);
//WHEN
new CopyGenerator(parent).generate(config);
//THEN
Node newNode = session.getNode("/foo/autogen-foo");
assertPropertyEquals(newNode, "stringProp", "a string", PropertyType.STRING);
assertPropertyEquals(newNode, "booleanProp", true, PropertyType.BOOLEAN);
assertPropertyEquals(newNode, "longProp", 100L, PropertyType.LONG);
assertPropertyEquals(newNode, "doubleProp", 3.14d, PropertyType.DOUBLE);
assertPropertyEquals(newNode, "calendarProp", null, PropertyType.DATE);
}
@Override
@After
public void tearDown() throws Exception {
session = null;
MgnlContext.setInstance(null);
super.tearDown();
}
protected void assertNodeAndMetaData(Node node, String template, String authorId) throws RepositoryException {
assertTrue(node.isNodeType(NodeTypes.ContentNode.NAME));
/*MetaData metaData = MetaDataUtil.getMetaData(node);
assertEquals(template, metaData.getTemplate());
assertEquals(authorId, metaData.getAuthorId());
assertNotNull(metaData.getModificationDate());
assertFalse(metaData.getIsActivated());*/
}
protected void assertPropertyEquals(Node node, String relPath, Object value, int type) throws PathNotFoundException, RepositoryException, ValueFormatException {
Property prop = node.getProperty(relPath);
assertEquals(prop.getType(), type);
switch(type) {
case PropertyType.STRING:
assertEquals(value, prop.getValue().getString());
break;
case PropertyType.BOOLEAN:
assertEquals(value, prop.getValue().getBoolean());
break;
case PropertyType.LONG:
assertEquals(value, prop.getValue().getLong());
break;
case PropertyType.DOUBLE:
assertEquals(value, prop.getValue().getDouble());
break;
default:
//ignore for the moment
}
}
}