1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.reference

File SolrEntityReferenceResolverTest.java

 

Code metrics

0
27
4
1
117
77
4
0.15
6.75
4
1

Classes

Class Line # Actions
SolrEntityReferenceResolverTest 52 27 0% 4 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    package org.xwiki.search.solr.internal.reference;
21   
22    import java.util.Arrays;
23    import java.util.Locale;
24   
25    import org.apache.solr.common.SolrDocument;
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.invocation.InvocationOnMock;
30    import org.mockito.stubbing.Answer;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.AttachmentReference;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.model.reference.EntityReferenceResolver;
36    import org.xwiki.model.reference.ObjectPropertyReference;
37    import org.xwiki.model.reference.ObjectReference;
38    import org.xwiki.model.reference.SpaceReference;
39    import org.xwiki.model.reference.WikiReference;
40    import org.xwiki.search.solr.internal.api.FieldUtils;
41    import org.xwiki.test.mockito.MockitoComponentMockingRule;
42   
43    import static org.junit.Assert.*;
44    import static org.mockito.ArgumentMatchers.*;
45    import static org.mockito.Mockito.*;
46   
47    /**
48    * Unit tests for {@link SolrEntityReferenceResolver}.
49    *
50    * @version $Id: 58cf48ebf07a0025e35779ba681275990d3e7f57 $
51    */
 
52    public class SolrEntityReferenceResolverTest
53    {
54    @Rule
55    public MockitoComponentMockingRule<EntityReferenceResolver<SolrDocument>> mocker =
56    new MockitoComponentMockingRule<EntityReferenceResolver<SolrDocument>>(SolrEntityReferenceResolver.class);
57   
58    private SolrDocument solrDocument;
59   
 
60  1 toggle @Before
61    public void configure() throws Exception
62    {
63  1 solrDocument = new SolrDocument();
64  1 solrDocument.setField(FieldUtils.WIKI, "chess");
65  1 solrDocument.setField(FieldUtils.SPACES, Arrays.asList("Path", "To", "Success"));
66  1 solrDocument.setField(FieldUtils.NAME, "WebHome");
67  1 solrDocument.setField(FieldUtils.DOCUMENT_LOCALE, "fr");
68    // The file name field can have multiple values.
69  1 solrDocument.addField(FieldUtils.FILENAME, "image.png");
70  1 solrDocument.addField(FieldUtils.FILENAME, "presentation.odp");
71    // The class name field can have multiple values too.
72  1 solrDocument.addField(FieldUtils.CLASS, "App.Code.PlayerClass");
73  1 solrDocument.addField(FieldUtils.CLASS, "App.Code.TrainerClass");
74  1 solrDocument.setField(FieldUtils.NUMBER, 13);
75  1 solrDocument.setField(FieldUtils.PROPERTY_NAME, "age");
76   
77  1 EntityReferenceResolver<EntityReference> explicitReferenceEntityReferenceResolver =
78    this.mocker.getInstance(EntityReferenceResolver.TYPE_REFERENCE, "explicit");
79  1 doAnswer(new Answer<EntityReference>()
80    {
 
81  6 toggle @Override
82    public EntityReference answer(InvocationOnMock invocation) throws Throwable
83    {
84  6 EntityReference reference = invocation.getArgument(0);
85  6 EntityType type = invocation.getArgument(1);
86  6 return reference.extractReference(type);
87    }
88    }).when(explicitReferenceEntityReferenceResolver).resolve(any(EntityReference.class), any(EntityType.class));
89    }
90   
 
91  1 toggle @Test
92    public void resolve() throws Exception
93    {
94  1 WikiReference wikiReference = new WikiReference("chess");
95  1 assertReference(wikiReference);
96   
97  1 assertReference(new SpaceReference("Success", new SpaceReference("To",
98    new SpaceReference("Path", wikiReference))));
99   
100  1 DocumentReference documentReference =
101    new DocumentReference("chess", Arrays.asList("Path", "To", "Success"), "WebHome", Locale.FRENCH);
102  1 assertReference(documentReference);
103   
104  1 assertReference(new AttachmentReference("image.png", documentReference));
105   
106  1 ObjectReference objectReference = new ObjectReference("App.Code.PlayerClass[13]", documentReference);
107  1 assertReference(objectReference);
108   
109  1 assertReference(new ObjectPropertyReference("age", objectReference));
110    }
111   
 
112  6 toggle private void assertReference(EntityReference reference) throws Exception
113    {
114  6 solrDocument.setField(FieldUtils.TYPE, reference.getType().name());
115  6 assertEquals(reference, this.mocker.getComponentUnderTest().resolve(solrDocument, reference.getType()));
116    }
117    }