View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.logging;
26  
27  import com.fasterxml.jackson.core.JsonProcessingException;
28  import com.fasterxml.jackson.databind.ObjectMapper;
29  
30  import org.apache.maven.plugin.logging.Log;
31  import org.eluder.coveralls.maven.plugin.domain.Git;
32  import org.eluder.coveralls.maven.plugin.domain.Git.Head;
33  import org.eluder.coveralls.maven.plugin.domain.Job;
34  import org.eluder.coveralls.maven.plugin.logging.Logger.Position;
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.extension.ExtendWith;
38  import org.mockito.ArgumentMatchers;
39  import org.mockito.Mock;
40  import org.mockito.Mockito;
41  import org.mockito.junit.jupiter.MockitoExtension;
42  
43  /**
44   * The Class JobLoggerTest.
45   */
46  @ExtendWith(MockitoExtension.class)
47  class JobLoggerTest {
48  
49      /** The job mock. */
50      @Mock
51      Job jobMock;
52  
53      /** The log mock. */
54      @Mock
55      Log logMock;
56  
57      /** The json mapper mock. */
58      @Mock
59      ObjectMapper jsonMapperMock;
60  
61      /**
62       * Missing job.
63       */
64      @Test
65      void missingJob() {
66          Assertions.assertThrows(IllegalArgumentException.class, () -> new JobLogger(null));
67      }
68  
69      /**
70       * Test get position.
71       */
72      @Test
73      void position() {
74          Assertions.assertEquals(Position.BEFORE, new JobLogger(this.jobMock).getPosition());
75      }
76  
77      /**
78       * Log job with id.
79       */
80      @Test
81      void logJobWithId() {
82          final var git = new Git(null, new Head("ab679cf2d81ac", null, null, null, null, null), "master", null);
83          Mockito.when(this.jobMock.getServiceName()).thenReturn("service");
84          Mockito.when(this.jobMock.getServiceJobId()).thenReturn("666");
85          Mockito.when(this.jobMock.getRepoToken()).thenReturn("123456789");
86          Mockito.when(this.jobMock.isDryRun()).thenReturn(true);
87          Mockito.when(this.jobMock.getGit()).thenReturn(git);
88  
89          new JobLogger(this.jobMock).log(this.logMock);
90  
91          Mockito.verify(this.logMock).info("Starting Coveralls job for service (666) in dry run mode");
92          Mockito.verify(this.logMock).info("Using repository token <secret>");
93          Mockito.verify(this.logMock).info("Git commit ab679cf in master");
94          Mockito.verify(this.logMock).isDebugEnabled();
95          Mockito.verifyNoMoreInteractions(this.logMock);
96      }
97  
98      /**
99       * Log with build number and url.
100      */
101     @Test
102     void logWithBuildNumberAndUrl() {
103         Mockito.when(this.jobMock.getServiceName()).thenReturn("service");
104         Mockito.when(this.jobMock.getServiceBuildNumber()).thenReturn("10");
105         Mockito.when(this.jobMock.getServiceBuildUrl()).thenReturn("https://ci.com/build/10");
106 
107         new JobLogger(this.jobMock).log(this.logMock);
108 
109         Mockito.verify(this.logMock).info("Starting Coveralls job for service (10 / https://ci.com/build/10)");
110         Mockito.verify(this.logMock).isDebugEnabled();
111         Mockito.verifyNoMoreInteractions(this.logMock);
112     }
113 
114     /**
115      * Log with build number but no url.
116      */
117     @Test
118     void logWithBuildNumberOnly() {
119         Mockito.when(this.jobMock.getServiceName()).thenReturn("service");
120         Mockito.when(this.jobMock.getServiceBuildNumber()).thenReturn("10");
121 
122         new JobLogger(this.jobMock).log(this.logMock);
123 
124         Mockito.verify(this.logMock).info("Starting Coveralls job for service (10)");
125         Mockito.verify(this.logMock).isDebugEnabled();
126         Mockito.verifyNoMoreInteractions(this.logMock);
127     }
128 
129     /**
130      * Log dry run.
131      */
132     @Test
133     void logDryRun() {
134         Mockito.when(this.jobMock.isDryRun()).thenReturn(true);
135 
136         new JobLogger(this.jobMock).log(this.logMock);
137 
138         Mockito.verify(this.logMock).info("Starting Coveralls job in dry run mode");
139         Mockito.verify(this.logMock).isDebugEnabled();
140         Mockito.verifyNoMoreInteractions(this.logMock);
141     }
142 
143     /**
144      * Log parallel.
145      */
146     @Test
147     void logParallel() {
148         Mockito.when(this.jobMock.isParallel()).thenReturn(true);
149 
150         new JobLogger(this.jobMock).log(this.logMock);
151 
152         Mockito.verify(this.logMock).info("Starting Coveralls job with parallel option enabled");
153         Mockito.verify(this.logMock).isDebugEnabled();
154         Mockito.verifyNoMoreInteractions(this.logMock);
155 
156     }
157 
158     /**
159      * Log job with debug.
160      *
161      * @throws JsonProcessingException
162      *             the json processing exception
163      */
164     @Test
165     void logJobWithDebug() throws JsonProcessingException {
166         Mockito.when(this.logMock.isDebugEnabled()).thenReturn(true);
167         Mockito.when(this.jobMock.getServiceName()).thenReturn("service");
168         Mockito.when(this.jsonMapperMock.writeValueAsString(ArgumentMatchers.same(this.jobMock)))
169                 .thenReturn("{\"serviceName\":\"service\"}");
170 
171         new JobLogger(this.jobMock, this.jsonMapperMock).log(this.logMock);
172 
173         Mockito.verify(this.logMock).info("Starting Coveralls job for service");
174         Mockito.verify(this.logMock).isDebugEnabled();
175         Mockito.verify(this.logMock).debug("Complete Job description:\n{\"serviceName\":\"service\"}");
176         Mockito.verifyNoMoreInteractions(this.logMock);
177     }
178 
179     /**
180      * Log job with error in debug.
181      *
182      * @throws JsonProcessingException
183      *             the json processing exception
184      */
185     @Test
186     void logJobWithErrorInDebug() throws JsonProcessingException {
187         Mockito.when(this.logMock.isDebugEnabled()).thenReturn(true);
188         Mockito.when(this.jobMock.getServiceName()).thenReturn("service");
189         Mockito.when(this.jsonMapperMock.writeValueAsString(ArgumentMatchers.same(this.jobMock)))
190                 .thenThrow(JsonProcessingException.class);
191 
192         final var jobLogger = new JobLogger(this.jobMock, this.jsonMapperMock);
193         Assertions.assertThrows(RuntimeException.class, () -> jobLogger.log(this.logMock));
194     }
195 
196 }