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

File DefaultJobStatusStoreTest.java

 

Code metrics

0
45
8
1
168
111
8
0.18
5.62
8
1

Classes

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