This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This file Copyright (c) 2015 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.config.converters; | |
| import static com.google.common.collect.Lists.newArrayList; | |
| import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.Differentiator.foo; | |
| import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf; | |
| import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException; | |
| import static org.hamcrest.MatcherAssert.assertThat; | |
| import static org.mockito.BDDMockito.given; | |
| import static org.mockito.Mockito.*; | |
| import info.magnolia.config.dummy.DummyRegistry; | |
| import info.magnolia.config.dummy.DummyThing; | |
| import info.magnolia.config.map2bean.Map2BeanTransformer; | |
| import info.magnolia.config.registry.DefinitionProvider; | |
| import info.magnolia.config.registry.DefinitionRawView; | |
| import info.magnolia.config.source.jcr.JcrConfigurationSource; | |
| import info.magnolia.config.source.yaml.YamlConfigurationSource; | |
| import info.magnolia.config.source.yaml.YamlReader; | |
| import info.magnolia.context.MgnlContext; | |
| import info.magnolia.jcr.node2bean.TypeMapping; | |
| import info.magnolia.jcr.node2bean.impl.Node2BeanProcessorImpl; | |
| import info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl; | |
| import info.magnolia.jcr.node2bean.impl.PreConfiguredBeanUtils; | |
| import info.magnolia.jcr.node2bean.impl.TypeMappingImpl; | |
| import info.magnolia.module.ModuleRegistry; | |
| import info.magnolia.objectfactory.Components; | |
| import info.magnolia.repository.RepositoryConstants; | |
| import info.magnolia.resourceloader.ResourceOrigin; | |
| import info.magnolia.resourceloader.dummy.DummyResourceOrigin; | |
| import info.magnolia.test.ComponentsTestUtil; | |
| import info.magnolia.test.hamcrest.Execution; | |
| import info.magnolia.test.mock.MockComponentProvider; | |
| import info.magnolia.test.mock.MockUtil; | |
| import info.magnolia.test.mock.jcr.MockNode; | |
| import info.magnolia.test.mock.jcr.MockSession; | |
| import info.magnolia.test.mock.jcr.SessionTestUtil; | |
| import java.io.Reader; | |
| import java.net.URL; | |
| import java.util.LinkedHashSet; | |
| import java.util.List; | |
| import java.util.Set; | |
| import java.util.regex.Pattern; | |
| import javax.jcr.Node; | |
| import javax.jcr.RepositoryException; | |
| import org.apache.commons.io.IOUtils; | |
| import org.apache.jackrabbit.commons.predicate.Predicate; | |
| import org.hamcrest.Description; | |
| import org.hamcrest.Matcher; | |
| import org.hamcrest.Matchers; | |
| import org.hamcrest.TypeSafeDiagnosingMatcher; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import com.google.common.base.Charsets; | |
| import com.google.common.io.Resources; | |
| public class DefinitionRawViewToYamlConverterImplTest { | |
| private ModuleRegistry moduleRegistry; | |
| private DummyRegistry registry; | |
| private MockSession session; | |
| private final TypeMapping typeMapping = new TypeMappingImpl(); | |
| private Node2BeanProcessorImpl node2bean; | |
| private DefinitionRawViewToYamlConverterImpl converter; | |
| private DefinitionProvider definitionProvider; | |
| @Before | |
| public void setUp() throws Exception { | |
| this.converter = new DefinitionRawViewToYamlConverterImpl(); | |
| this.definitionProvider = mock(DefinitionProvider.class); | |
| MockUtil.initMockContext(); | |
| session = SessionTestUtil.createSession(RepositoryConstants.CONFIG, | |
| "/modules/fooModule/dummies/a.title=fooTitle", | |
| "/modules/fooModule/dummies/a.class=info.magnolia.config.dummy.DummierThing" | |
| ); | |
| final MockNode a = (MockNode) session.getNode("/modules/fooModule/dummies/a"); | |
| final Node someNumbers = a.addNode("someNumbers"); | |
| someNumbers.setProperty("first", "0.001"); | |
| someNumbers.setProperty("second", "0.002"); | |
| final Node dummierThings = a.addNode("dummierThings"); | |
| final Node dt1 = dummierThings.addNode("dt1"); | |
| final Node someBeans = dt1.addNode("someBeans"); | |
| someBeans.addNode("b1"); | |
| someBeans.addNode("b2"); | |
| final Node someOtherBeans = dt1.addNode("someOtherBeans"); | |
| someOtherBeans.addNode("b1"); | |
| someOtherBeans.addNode("b2"); | |
| dummierThings.addNode("dt2"); | |
| MockUtil.setSystemContextSessionAndHierarchyManager(session); | |
| MockUtil.setSystemContextSessionAndHierarchyManager(session); | |
| ComponentsTestUtil.setInstance(TypeMapping.class, typeMapping); | |
| Set<String> moduleNames = new LinkedHashSet<>(); | |
| moduleNames.add("fooModule"); | |
| moduleRegistry = mock(ModuleRegistry.class); | |
| when(moduleRegistry.getModuleNames()).thenReturn(moduleNames); | |
| registry = new DummyRegistry(); | |
| node2bean = new Node2BeanProcessorImpl(typeMapping, new Node2BeanTransformerImpl(new PreConfiguredBeanUtils())); | |
| } | |
| @After | |
| public void tearDown() throws Exception { | |
| ComponentsTestUtil.clear(); | |
| Components.setComponentProvider(null); | |
| MgnlContext.setInstance(null); | |
| } | |
| @Test | |
| public void crossConfigSourceConversionTest() throws Exception { | |
| final JcrConfigurationSource<DummyThing> source = new JcrConfigurationSource<>(registry, "dummies", Predicate.TRUE, node2bean, moduleRegistry); | |
| final YamlConfigurationSource<DummyThing> yamlSource = | |
| new YamlConfigurationSource<>( | |
| mock(ResourceOrigin.class), | |
| new Map2BeanTransformer( | |
| new MockComponentProvider(), | |
| new TypeMappingImpl(), | |
| new PreConfiguredBeanUtils()), | |
| registry, | |
| Pattern.compile(".*\\.yaml"), | |
| new YamlReader()); | |
| source.start(); | |
| yamlSource.start(); | |
| final DefinitionProvider<DummyThing> provider = registry.getProvider("fooModule:a"); | |
| yamlSource.loadAndRegister(DummyResourceOrigin.singleResource(foo, "/modules/dummies/yamlA.yaml", converter.convert(provider.getRaw()))); | |
| final DefinitionProvider<DummyThing> provider1 = registry.getProvider("yaml:modules/dummies/yamlA"); | |
| assertThat(provider1.get(), Matchers.equalTo(provider.get())); | |
| } | |
| private Matcher<? super String> sameAsResource(final String resourceFile) throws Exception { | |
| return new TypeSafeDiagnosingMatcher<String>() { | |
| final String resource = loadResourceFile(resourceFile); | |
| @Override | |
| protected boolean matchesSafely(String yamlText, Description description) { | |
| boolean isEqual = resource.equals(yamlText); | |
| if (!isEqual) { | |
| description.appendValue(yamlText); | |
| } | |
| return isEqual; | |
| } | |
| @Override | |
| public void describeTo(Description description) { | |
| description.appendValue(resource); | |
| } | |
| }; | |
| } | |
| private String loadResourceFile(String resourceName) throws Exception { | |
| final URL resourceUrl = Resources.getResource("info.magnolia.export/" + resourceName); | |
| return Resources.toString(resourceUrl, Charsets.UTF_8); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment