1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.crypto.store.wiki.internal

File DefaultSignatureStoreTest.java

 

Code metrics

0
40
5
1
186
129
5
0.12
8
5
1

Classes

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