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

File DefaultJobStatusStoreTest.java

 

Code metrics

0
44
8
1
166
109
8
0.18
5.5
8
1

Classes

Class Line # Actions
DefaultJobStatusStoreTest 52 44 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 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.job.internal;
21   
22    import java.io.File;
23    import java.util.Arrays;
24    import java.util.List;
25   
26    import org.apache.commons.io.FileUtils;
27    import org.junit.Assert;
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.cache.CacheManager;
32    import org.xwiki.cache.internal.MapCache;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.util.ReflectionUtils;
35    import org.xwiki.job.DefaultJobStatus;
36    import org.xwiki.job.DefaultRequest;
37    import org.xwiki.job.JobManagerConfiguration;
38    import org.xwiki.job.event.status.JobStatus;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40   
41    import static org.junit.Assert.assertNull;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.Mockito.mock;
44    import static org.mockito.Mockito.verifyNoMoreInteractions;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Unit tests for {@link DefaultJobStatusStore}.
49    *
50    * @version $Id: 0de8776c0f32f92bcbba7333fb79811d4b8ba228 $
51    */
 
52    public class DefaultJobStatusStoreTest
53    {
54    @Rule
55    public final MockitoComponentMockingRule<DefaultJobStatusStore> componentManager =
56    new MockitoComponentMockingRule<>(DefaultJobStatusStore.class);
57   
 
58  7 toggle @Before
59    public void before() throws Exception
60    {
61  7 JobManagerConfiguration jobManagerConfiguration =
62    this.componentManager.getInstance(JobManagerConfiguration.class);
63   
64  7 FileUtils.deleteDirectory(new File("target/test/jobs/"));
65  7 FileUtils.copyDirectory(new File("src/test/resources/jobs/"), new File("target/test/jobs/"));
66   
67  7 when(jobManagerConfiguration.getStorage()).thenReturn(new File("target/test/jobs/status"));
68  7 when(jobManagerConfiguration.getJobStatusCacheSize()).thenReturn(100);
69   
70  7 CacheManager cacheManagerMock = this.componentManager.getInstance(CacheManager.class);
71  7 when(cacheManagerMock.createNewCache(any())).thenReturn(new MapCache<>());
72    }
73   
 
74  1 toggle @Test
75    public void getJobStatusWithNullId() throws Exception
76    {
77  1 JobStatus jobStatus = this.componentManager.getComponentUnderTest().getJobStatus((List<String>) null);
78   
79  1 Assert.assertNotNull(jobStatus);
80  1 Assert.assertNull(jobStatus.getRequest().getId());
81  1 Assert.assertEquals(JobStatus.State.FINISHED, jobStatus.getState());
82   
83  1 Assert.assertSame(jobStatus, this.componentManager.getComponentUnderTest().getJobStatus((List<String>) null));
84    }
85   
 
86  1 toggle @Test
87    public void getJobStatusWithMultipleId() throws Exception
88    {
89  1 JobStatus jobStatus = this.componentManager.getComponentUnderTest().getJobStatus(Arrays.asList("id1", "id2"));
90   
91  1 Assert.assertNotNull(jobStatus);
92  1 Assert.assertEquals(Arrays.asList("id1", "id2"), jobStatus.getRequest().getId());
93  1 Assert.assertEquals(JobStatus.State.FINISHED, jobStatus.getState());
94   
95  1 Assert.assertSame(jobStatus,
96    this.componentManager.getComponentUnderTest().getJobStatus(Arrays.asList("id1", "id2")));
97    }
98   
 
99  1 toggle @Test
100    public void getJobStatusInOldPlace() throws Exception
101    {
102  1 JobStatus jobStatus =
103    this.componentManager.getComponentUnderTest().getJobStatus(Arrays.asList("id1", "id2", "id3"));
104   
105  1 Assert.assertNotNull(jobStatus);
106  1 Assert.assertEquals(Arrays.asList("id1", "id2", "id3"), jobStatus.getRequest().getId());
107  1 Assert.assertEquals(JobStatus.State.FINISHED, jobStatus.getState());
108    }
109   
 
110  1 toggle @Test
111    public void getJobStatusInWrongPlaceAndWithInvalidLogArgument() throws Exception
112    {
113  1 JobStatus jobStatus =
114    this.componentManager.getComponentUnderTest().getJobStatus(Arrays.asList("invalidlogargument"));
115   
116  1 Assert.assertEquals(3, jobStatus.getLog().size());
117    }
118   
 
119  1 toggle @Test
120    public void getJobStatusThatDoesNotExist() throws Exception
121    {
122  1 JobStatus jobStatus = this.componentManager.getComponentUnderTest().getJobStatus(Arrays.asList("nostatus"));
123   
124  1 assertNull(jobStatus);
125   
126  1 JobStatusSerializer mockSerializer = mock(JobStatusSerializer.class);
127  1 ReflectionUtils.setFieldValue(this.componentManager.getComponentUnderTest(), "serializer", mockSerializer);
128   
129  1 jobStatus = this.componentManager.getComponentUnderTest().getJobStatus(Arrays.asList("nostatus"));
130  1 assertNull(jobStatus);
131   
132  1 verifyNoMoreInteractions(mockSerializer);
133    }
134   
 
135  1 toggle @Test
136    public void removeJobStatus() throws ComponentLookupException
137    {
138  1 List<String> id = null;
139   
140  1 JobStatus jobStatus = this.componentManager.getComponentUnderTest().getJobStatus(id);
141   
142  1 Assert.assertNotNull(jobStatus);
143  1 Assert.assertNull(jobStatus.getRequest().getId());
144  1 Assert.assertEquals(JobStatus.State.FINISHED, jobStatus.getState());
145   
146  1 Assert.assertSame(jobStatus, this.componentManager.getComponentUnderTest().getJobStatus(id));
147   
148  1 this.componentManager.getComponentUnderTest().remove(id);
149   
150  1 Assert.assertSame(null, this.componentManager.getComponentUnderTest().getJobStatus(id));
151    }
152   
 
153  1 toggle @Test
154    public void storeJobStatus() throws ComponentLookupException
155    {
156  1 List<String> id = Arrays.asList("newstatus");
157   
158  1 DefaultRequest request = new DefaultRequest();
159  1 request.setId(id);
160  1 JobStatus jobStatus = new DefaultJobStatus(request, null, null, null);
161   
162  1 this.componentManager.getComponentUnderTest().store(jobStatus);
163   
164  1 Assert.assertSame(jobStatus, this.componentManager.getComponentUnderTest().getJobStatus(id));
165    }
166    }