Clover icon

Magnolia Resources App Module 2.4.7

  1. Project Clover database Fri Sep 9 2016 16:27:56 CEST
  2. Package info.magnolia.resources.app.utils

File ResourceUtilsTest.java

 

Code metrics

0
39
4
1
146
80
4
0.1
9.75
4
1

Classes

Class Line # Actions
ResourceUtilsTest 63 39 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.resources.app.utils;
35   
36    import static org.hamcrest.CoreMatchers.is;
37    import static org.hamcrest.MatcherAssert.assertThat;
38    import static org.hamcrest.Matchers.hasSize;
39    import static org.mockito.BDDMockito.given;
40    import static org.mockito.Mockito.mock;
41   
42    import info.magnolia.context.Context;
43    import info.magnolia.jcr.util.NodeTypes;
44    import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
45    import info.magnolia.resources.app.workbench.ResourcesContainer;
46    import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
47    import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
48    import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
49   
50    import java.util.List;
51   
52    import javax.jcr.Item;
53    import javax.jcr.Node;
54    import javax.jcr.Session;
55    import javax.jcr.Workspace;
56    import javax.jcr.nodetype.NodeType;
57   
58    import org.junit.Before;
59    import org.junit.Test;
60   
61    import com.google.common.collect.Lists;
62   
 
63    public class ResourceUtilsTest {
64   
65    private JcrItemAdapter jcrItemAdapter;
66    private Node node;
67    private Item item;
68    private Context context;
69    private com.vaadin.data.Item vaadinItem;
70    private com.vaadin.data.Item vaadinItem2;
71   
 
72  3 toggle @Before
73    public void setUp() throws Exception {
74  3 jcrItemAdapter = mock(JcrItemAdapter.class);
75  3 node = mock(Node.class);
76  3 item = mock(Item.class);
77   
78  3 given(node.isNode()).willReturn(true);
79  3 given(node.getIdentifier()).willReturn("test-identifier");
80  3 NodeType nodeType = mock(NodeType.class);
81  3 given(nodeType.getName()).willReturn(NodeTypes.Content.NAME);
82  3 given(node.getPrimaryNodeType()).willReturn(nodeType);
83   
84  3 Node node2 = mock(Node.class);
85  3 given(node2.isNode()).willReturn(true);
86  3 given(node2.getIdentifier()).willReturn("test-identifier");
87  3 given(node2.getPrimaryNodeType()).willReturn(nodeType);
88  3 context = mock(Context.class);
89   
90  3 vaadinItem = mock(com.vaadin.data.Item.class);
91  3 given(vaadinItem.getItemProperty(ResourcesContainer.RESOURCE_PATH)).willReturn(new DefaultProperty<>("/node"));
92   
93  3 vaadinItem2 = mock(com.vaadin.data.Item.class);
94  3 given(vaadinItem2.getItemProperty(ResourcesContainer.RESOURCE_PATH)).willReturn(new DefaultProperty<>("/node/a"));
95   
96  3 Session session = mock(Session.class);
97  3 given(context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE)).willReturn(session);
98  3 given(session.getNode("/node")).willReturn(node);
99  3 given(session.getNode("/node/a")).willReturn(node2);
100  3 Workspace workspace = mock(Workspace.class);
101  3 given(workspace.getName()).willReturn(JcrResourceOrigin.RESOURCES_WORKSPACE);
102  3 given(session.getWorkspace()).willReturn(workspace);
103  3 given(node.getSession()).willReturn(session);
104  3 given(node2.getSession()).willReturn(session);
105    }
106   
 
107  1 toggle @Test
108    public void canGetJcrAdapters() throws Exception {
109    // GIVEN
110   
111    // WHEN
112  1 JcrNodeAdapter nodeAdapter = ResourceUtils.getJcrNodeAdapterFor(context, vaadinItem);
113   
114    // THEN
115  1 assertThat(nodeAdapter.getPrimaryNodeTypeName(), is(NodeTypes.Content.NAME));
116  1 assertThat(nodeAdapter.getWorkspace(), is(JcrResourceOrigin.RESOURCES_WORKSPACE));
117    }
118   
 
119  1 toggle @Test
120    public void canGetJcrAdapter() throws Exception {
121    // GIVEN
122   
123    // WHEN
124  1 List<JcrItemAdapter> adapterList = ResourceUtils.getJcrItemAdaptersFor(context, Lists.newArrayList(vaadinItem, vaadinItem2));
125   
126    // THEN
127  1 assertThat(adapterList, hasSize(2));
128  1 assertThat(adapterList.get(0).getWorkspace(), is(JcrResourceOrigin.RESOURCES_WORKSPACE));
129  1 assertThat(adapterList.get(1).getWorkspace(), is(JcrResourceOrigin.RESOURCES_WORKSPACE));
130    }
131   
 
132  1 toggle @Test
133    public void canExtractParentItem() throws Exception {
134    // GIVEN
135  1 given(jcrItemAdapter.getJcrItem()).willReturn(item);
136  1 given(item.getParent()).willReturn(node);
137   
138    // WHEN
139  1 Node extractedNode = ResourceUtils.extractParentItem(jcrItemAdapter);
140   
141    // THEN
142  1 assertThat(extractedNode.getIdentifier(), is(node.getIdentifier()));
143  1 assertThat(extractedNode.getPath(), is(node.getPath()));
144  1 assertThat(extractedNode.getName(), is(node.getName()));
145    }
146    }