1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.store.wiki.internal

File DefaultSignatureStoreTest.java

 

Code metrics

0
40
5
1
185
128
5
0.12
8
5
1

Classes

Class Line # Actions
DefaultSignatureStoreTest 62 40 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

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    package org.xwiki.crypto.store.wiki.internal;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.crypto.BinaryStringEncoder;
28    import org.xwiki.crypto.store.SignatureStore;
29    import org.xwiki.model.EntityType;
30    import org.xwiki.model.reference.BlockReference;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.model.reference.EntityReferenceProvider;
34    import org.xwiki.model.reference.WikiReference;
35    import org.xwiki.test.annotation.ComponentList;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import com.xpn.xwiki.XWiki;
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
42    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
43    import com.xpn.xwiki.objects.BaseObject;
44   
45    import static org.hamcrest.CoreMatchers.equalTo;
46    import static org.hamcrest.CoreMatchers.nullValue;
47    import static org.junit.Assert.assertThat;
48    import static org.mockito.ArgumentMatchers.any;
49    import static org.mockito.ArgumentMatchers.eq;
50    import static org.mockito.Mockito.mock;
51    import static org.mockito.Mockito.never;
52    import static org.mockito.Mockito.verify;
53    import static org.mockito.Mockito.when;
54   
55    /**
56    * Unit tests for {@link org.xwiki.crypto.store.wiki.internal.DefaultSignatureStore}
57    *
58    * @version $Id: 2856a656056d0c403ba23cadcc156777cb150053 $
59    * @since 6.0
60    */
61    @ComponentList({ CurrentReferenceDocumentReferenceResolver.class, CurrentReferenceEntityReferenceResolver.class })
 
62    public class DefaultSignatureStoreTest
63    {
64    private static final byte[] SIGNATURE = "signature".getBytes();
65   
66    private static final String ENCODED_SIGNATURE = "encoded_signature";
67   
68    private static final WikiReference WIKI_REFERENCE = new WikiReference("wiki");
69    private static final EntityReference SPACE_REFERENCE = new EntityReference("space", EntityType.WIKI);
70    private static final EntityReference DOCUMENT_REFERENCE = new EntityReference("documents", EntityType.DOCUMENT);
71   
72    @Rule
73    public MockitoComponentMockingRule<SignatureStore> mocker =
74    new MockitoComponentMockingRule<SignatureStore>(DefaultSignatureStore.class);
75   
76    private XWikiContext xcontext;
77   
78    private XWiki xwiki;
79   
80    private SignatureStore store;
81   
 
82  4 toggle @Before
83    public void setUp() throws Exception
84    {
85  4 EntityReferenceProvider valueProvider = mock(EntityReferenceProvider.class);
86  4 when(valueProvider.getDefaultReference(EntityType.WIKI)).thenReturn(WIKI_REFERENCE);
87  4 when(valueProvider.getDefaultReference(EntityType.SPACE)).thenReturn(SPACE_REFERENCE);
88  4 when(valueProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DOCUMENT_REFERENCE);
89   
90  4 this.mocker.registerComponent(EntityReferenceProvider.class, "current", valueProvider);
91   
92  4 Provider<XWikiContext> xcontextProvider =
93    this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
94  4 this.xcontext = mock(XWikiContext.class);
95  4 when(xcontextProvider.get()).thenReturn(this.xcontext);
96  4 this.xwiki = mock(com.xpn.xwiki.XWiki.class);
97  4 when(this.xcontext.getWiki()).thenReturn(this.xwiki);
98   
99  4 BinaryStringEncoder encoder = this.mocker.getInstance(BinaryStringEncoder.class, "Base64");
100  4 when(encoder.encode(SIGNATURE, 64)).thenReturn(ENCODED_SIGNATURE);
101  4 when(encoder.decode(ENCODED_SIGNATURE)).thenReturn(SIGNATURE);
102   
103  4 this.store = this.mocker.getComponentUnderTest();
104    }
105   
 
106  1 toggle @Test
107    public void testStoringNewSignature() throws Exception
108    {
109  1 XWikiDocument sourceDocument = mock(XWikiDocument.class);
110  1 when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext))
111    .thenReturn(sourceDocument);
112   
113  1 BaseObject signatureObject = mock(BaseObject.class);
114  1 when(sourceDocument.newXObject(DefaultSignatureStore.SIGNATURECLASS, this.xcontext))
115    .thenReturn(signatureObject);
116   
117  1 this.store.store(new BlockReference("block", new DocumentReference("wiki", "space", "document")), SIGNATURE);
118   
119  1 verify(signatureObject).setStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE, "block");
120  1 verify(signatureObject).setLargeStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_SIGNATURE,
121    ENCODED_SIGNATURE);
122   
123  1 verify(this.xwiki).saveDocument(sourceDocument, this.xcontext);
124    }
125   
 
126  1 toggle @Test
127    public void testUpdatingSignature() throws Exception
128    {
129  1 XWikiDocument sourceDocument = mock(XWikiDocument.class);
130  1 when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext))
131    .thenReturn(sourceDocument);
132   
133  1 BaseObject signatureObject = mock(BaseObject.class);
134  1 when(sourceDocument.getXObject(
135    new DocumentReference(DefaultSignatureStore.SIGNATURECLASS, new WikiReference("wiki")),
136    DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE,
137    "block")).thenReturn(signatureObject);
138   
139  1 this.store.store(new BlockReference("block", new DocumentReference("wiki", "space", "document")), SIGNATURE);
140   
141  1 verify(signatureObject, never()).setStringValue(eq(DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE),
142    any(String.class));
143  1 verify(signatureObject).setLargeStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_SIGNATURE,
144    ENCODED_SIGNATURE);
145   
146  1 verify(this.xwiki).saveDocument(sourceDocument, this.xcontext);
147    }
148   
 
149  1 toggle @Test
150    public void testRetrievingExistingSignature() throws Exception
151    {
152  1 XWikiDocument sourceDocument = mock(XWikiDocument.class);
153  1 when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext))
154    .thenReturn(sourceDocument);
155   
156  1 BaseObject signatureObject = mock(BaseObject.class);
157  1 when(sourceDocument.getXObject(
158    new DocumentReference(DefaultSignatureStore.SIGNATURECLASS, new WikiReference("wiki")),
159    DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE,
160    "block")).thenReturn(signatureObject);
161   
162  1 when(signatureObject.getLargeStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_SIGNATURE)).thenReturn(
163    ENCODED_SIGNATURE);
164   
165  1 assertThat(
166    this.store.retrieve(new BlockReference("block", new DocumentReference("wiki", "space", "document"))),
167    equalTo(SIGNATURE));
168    }
169   
 
170  1 toggle @Test
171    public void testRetrievingMissingSignature() throws Exception
172    {
173  1 XWikiDocument sourceDocument = mock(XWikiDocument.class);
174  1 when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext))
175    .thenReturn(sourceDocument);
176   
177  1 assertThat(
178    this.store.retrieve(new BlockReference("block", new DocumentReference("wiki", "space", "document"))),
179    nullValue());
180  1 verify(sourceDocument).getXObject(
181    new DocumentReference(DefaultSignatureStore.SIGNATURECLASS, new WikiReference("wiki")),
182    DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE,
183    "block");
184    }
185    }