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