1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.authorization.testwikis.internal.parser

File TestDefinitionParserTest.java

 

Code metrics

0
66
1
1
213
149
1
0.02
66
1
1

Classes

Class Line # Actions
TestDefinitionParserTest 70 66 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package org.xwiki.security.authorization.testwikis.internal.parser;
22   
23    import java.io.File;
24    import java.util.ArrayList;
25    import java.util.Collection;
26    import java.util.List;
27   
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.model.internal.DefaultModelConfiguration;
31    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
32    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
33    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
34    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.model.reference.EntityReferenceResolver;
37    import org.xwiki.model.reference.EntityReferenceSerializer;
38    import org.xwiki.model.reference.WikiReference;
39    import org.xwiki.security.authorization.Right;
40    import org.xwiki.security.authorization.RuleState;
41    import org.xwiki.security.authorization.testwikis.TestAccessRule;
42    import org.xwiki.security.authorization.testwikis.TestDefinition;
43    import org.xwiki.security.authorization.testwikis.TestDefinitionParser;
44    import org.xwiki.security.authorization.testwikis.TestDocument;
45    import org.xwiki.security.authorization.testwikis.TestGroup;
46    import org.xwiki.security.authorization.testwikis.TestSpace;
47    import org.xwiki.security.authorization.testwikis.TestWiki;
48    import org.xwiki.test.ComponentManagerRule;
49    import org.xwiki.test.annotation.ComponentList;
50   
51    import static org.hamcrest.CoreMatchers.equalTo;
52    import static org.hamcrest.CoreMatchers.hasItems;
53    import static org.hamcrest.CoreMatchers.is;
54    import static org.hamcrest.CoreMatchers.notNullValue;
55    import static org.junit.Assert.assertThat;
56   
57    /**
58    * Test of the test definition parser itself.
59    *
60    * @version $Id: 692d893b3e87f6260cb063aa4c08a638ac55cdb7 $
61    * @since 5.0M2
62    */
63    @ComponentList({
64    DefaultStringEntityReferenceResolver.class,
65    DefaultStringEntityReferenceSerializer.class,
66    DefaultEntityReferenceProvider.class,
67    DefaultModelConfiguration.class,
68    DefaultSymbolScheme.class
69    })
 
70    public class TestDefinitionParserTest
71    {
72    @Rule
73    public final ComponentManagerRule componentManager = new ComponentManagerRule();
74   
 
75  1 toggle @Test
76    public void testDefinitionTestParser() throws Exception
77    {
78  1 TestDefinitionParser parser = new DefaultTestDefinitionParser();
79   
80  1 EntityReferenceResolver<String> resolver =
81    componentManager.getInstance(EntityReferenceResolver.TYPE_STRING);
82  1 EntityReferenceSerializer<String> serializer =
83    componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
84   
85  1 TestDefinition testDefinition =
86    parser.parse("testwikis" + File.separatorChar + "parserTester.xml", resolver, serializer);
87   
88  1 Collection<TestWiki> testWikis = testDefinition.getWikis();
89   
90  1 assertThat("There should be some wikis", testWikis, notNullValue());
91  1 assertThat("The wikis count should be 3", testWikis.size(), equalTo(3));
92   
93  1 TestWiki mainwiki = testDefinition.getMainWiki();
94   
95  1 assertThat("Main wiki should be defined", mainwiki, notNullValue());
96  1 assertThat("Main wiki should be named 'wiki'", mainwiki.getWikiReference(), equalTo(new WikiReference("wiki")));
97  1 assertThat("Main wiki should be main wiki", mainwiki.isMainWiki(), is(true));
98  1 assertThat("Main wiki owner should be XWiki.Admin", mainwiki.getOwner(), equalTo(
99    new DocumentReference("wiki", "XWiki", "Admin")));
100  1 assertThat("Main wiki should have 4 users (2 groups, and 2 users)", mainwiki.getUsers().size(), equalTo(4));
101  1 assertThat("Main wiki should have 2 groups", mainwiki.getGroups().size(), equalTo(2));
102  1 assertThat("Main wiki should have a groupA", mainwiki.getGroup("groupA"), notNullValue());
103  1 assertThat("Main wiki should have a userA", mainwiki.getUser("userA"), notNullValue());
104   
105  1 Collection<TestGroup> groups = mainwiki.getUser("userA").getGroups();
106   
107  1 assertThat("UserA of Main wiki should be in 2 groups", groups.size(), equalTo(2));
108   
109  1 List<DocumentReference> groupRefs = new ArrayList<DocumentReference>();
110  1 for (TestGroup group : groups) {
111  2 groupRefs.add(group.getGroupReference());
112    }
113   
114  1 assertThat("User A is in GroupA of the main wiki and the subwiki", groupRefs,
115    hasItems(new DocumentReference("wiki", "XWiki", "groupA"),
116    new DocumentReference("wiki1", "XWiki", "groupA")));
117   
118  1 Collection<TestAccessRule> rules = mainwiki.getAccessRules();
119   
120  1 assertThat("There must be 26 access rules on main wiki", rules.size(), equalTo(26));
121   
122  1 List<DocumentReference> userRefs = new ArrayList<DocumentReference>();
123  1 List<Right> rights = new ArrayList<Right>();
124  1 List<RuleState> states = new ArrayList<RuleState>();
125   
126  1 for (TestAccessRule rule : rules) {
127  26 userRefs.add(rule.getUser());
128  26 rights.add(rule.getRight());
129  26 states.add(rule.getState());
130    }
131   
132  1 assertThat("Users in access rules of main wiki mismatch", userRefs, hasItems(
133    new DocumentReference("wiki", "XWiki", "userA"),
134    new DocumentReference("wiki", "XWiki", "userB"),
135    new DocumentReference("wiki", "XWiki", "groupA"),
136    new DocumentReference("wiki", "XWiki", "groupB")
137    ));
138  1 assertThat("Rights in access rules of main wiki mismatch", rights, hasItems(
139    Right.VIEW, Right.LOGIN, Right.EDIT, Right.COMMENT, Right.DELETE, Right.REGISTER, Right.ADMIN, Right.PROGRAM
140    ));
141  1 assertThat("State in access rules of main wiki mismatch", states, hasItems(
142    RuleState.ALLOW, RuleState.DENY
143    ));
144   
145  1 assertThat("Main wiki should have 3 spaces (2 plus XWiki)", mainwiki.getSpaces().size(), equalTo(3));
146   
147  1 TestSpace space = mainwiki.getSpace("space1");
148   
149  1 assertThat("Main wiki should have a space named 'space1'", space, notNullValue());
150  1 assertThat("'space1' of main wiki should have description 'space 1'", space.getDescription(), equalTo("space 1"));
151   
152  1 rules = space.getAccessRules();
153   
154  1 assertThat("There must be 8 access rules on space 1", rules.size(), equalTo(8));
155   
156  1 userRefs = new ArrayList<DocumentReference>();
157  1 rights = new ArrayList<Right>();
158  1 states = new ArrayList<RuleState>();
159   
160  1 for (TestAccessRule rule : rules) {
161  8 userRefs.add(rule.getUser());
162  8 rights.add(rule.getRight());
163  8 states.add(rule.getState());
164    }
165   
166  1 assertThat("Users in access rules of space 1 of main wiki mismatch", userRefs, hasItems(
167    new DocumentReference("wiki", "XWiki", "userA"),
168    new DocumentReference("wiki", "XWiki", "userB"),
169    new DocumentReference("wiki", "XWiki", "groupB")
170    ));
171  1 assertThat("Rights in access rules of space 1 of main wiki mismatch", rights, hasItems(
172    Right.VIEW, Right.EDIT, Right.COMMENT, Right.DELETE, Right.ADMIN
173    ));
174  1 assertThat("State in access rules of space 1 of main wiki mismatch", states, hasItems(
175    RuleState.DENY
176    ));
177   
178  1 assertThat("Space 1 of main wiki should have 2 documents", space.getDocuments().size(), equalTo(2));
179   
180  1 TestDocument document = space.getDocument("document1");
181   
182  1 assertThat("Space 1 of main wiki should have a document named 'document1'", document, notNullValue());
183  1 assertThat("'document1' of 'space1' of main wiki should have description 'Document 1'", document.getDescription(), equalTo("Document 1"));
184   
185  1 rules = document.getAccessRules();
186   
187  1 assertThat("There must be 7 access rules on document 1", rules.size(), equalTo(7));
188   
189  1 userRefs = new ArrayList<DocumentReference>();
190  1 rights = new ArrayList<Right>();
191  1 states = new ArrayList<RuleState>();
192   
193  1 for (TestAccessRule rule : rules) {
194  7 userRefs.add(rule.getUser());
195  7 rights.add(rule.getRight());
196  7 states.add(rule.getState());
197    }
198   
199  1 assertThat("Users in access rules of document 1 of space 1 of main wiki mismatch", userRefs, hasItems(
200    new DocumentReference("wiki", "XWiki", "userA"),
201    new DocumentReference("wiki", "XWiki", "userB"),
202    new DocumentReference("wiki", "XWiki", "groupA")
203    ));
204  1 assertThat("Rights in access rules of document 1 of space 1 of main wiki mismatch", rights, hasItems(
205    Right.VIEW, Right.EDIT, Right.COMMENT, Right.DELETE
206    ));
207  1 assertThat("State in access rules of document 1 of space 1 of main wiki mismatch", states, hasItems(
208    RuleState.ALLOW
209    ));
210   
211    }
212    }
213