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.json;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertSame;
28  import static org.junit.jupiter.api.Assertions.assertTrue;
29  
30  import com.fasterxml.jackson.databind.ObjectMapper;
31  import com.fasterxml.jackson.databind.type.MapType;
32  import org.eluder.coveralls.maven.plugin.domain.Git;
33  import org.eluder.coveralls.maven.plugin.domain.Job;
34  import org.eluder.coveralls.maven.plugin.domain.Source;
35  import org.eluder.coveralls.maven.plugin.util.TestIoUtil;
36  import org.junit.Before;
37  import org.junit.Rule;
38  import org.junit.Test;
39  import org.junit.rules.TemporaryFolder;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.sql.Date;
44  import java.text.SimpleDateFormat;
45  import java.util.Arrays;
46  import java.util.Collection;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  import java.util.Properties;
51  
52  
53  public class JsonWriterTest {
54  
55      private static final long TEST_TIME = 1357009200000l;
56  
57      @Rule
58      public TemporaryFolder folder = new TemporaryFolder();
59  
60      private File file;
61  
62      @Before
63      public void init() throws IOException {
64          file = folder.newFile();
65      }
66  
67      @Test
68      public void testSubDirectoryCreation() throws Exception {
69          File f = new File(new File(folder.getRoot(), "sub1"), "sub2");
70          Job job = job();
71          assertTrue(new JsonWriter(job, f).getCoverallsFile().getParentFile().isDirectory());
72      }
73  
74      @Test
75      @SuppressWarnings("resource")
76      public void testGetJob() throws Exception {
77          Job job = job();
78          assertSame(job, new JsonWriter(job, file).getJob());
79      }
80  
81      @Test
82      @SuppressWarnings("resource")
83      public void testGetCoverallsFile() throws Exception {
84          Job job = job();
85          assertSame(file, new JsonWriter(job, file).getCoverallsFile());
86  
87      }
88  
89      @SuppressWarnings("rawtypes")
90      @Test
91      public void testWriteStartAndEnd() throws Exception {
92          JsonWriter writer = new JsonWriter(job(), file);
93          try {
94              writer.onBegin();
95              writer.onComplete();
96          } finally {
97              writer.close();
98          }
99          String content = TestIoUtil.readFileContent(file);
100         Map<String, Object> jsonMap = stringToJsonMap(content);
101         assertEquals("service", jsonMap.get("service_name"));
102         assertEquals("job123", jsonMap.get("service_job_id"));
103         assertEquals("build5", jsonMap.get("service_number"));
104         assertEquals("http://ci.com/build5", jsonMap.get("service_build_url"));
105         assertEquals("foobar", ((Map) jsonMap.get("environment")).get("custom_property"));
106         assertEquals("master", jsonMap.get("service_branch"));
107         assertEquals("pull10", jsonMap.get("service_pull_request"));
108         assertEquals(new SimpleDateFormat(JsonWriter.TIMESTAMP_FORMAT).format(new Date(TEST_TIME)), jsonMap.get("run_at"));
109         assertEquals("af456fge34acd", ((Map) jsonMap.get("git")).get("branch"));
110         assertEquals("aefg837fge", ((Map) ((Map) jsonMap.get("git")).get("head")).get("id"));
111         assertEquals(0, ((Collection<?>) jsonMap.get("source_files")).size());
112     }
113 
114     @Test
115     public void testOnSource() throws Exception {
116         JsonWriter writer = new JsonWriter(job(), file);
117         try {
118             writer.onBegin();
119             writer.onSource(source());
120             writer.onComplete();
121         } finally {
122             writer.close();
123         }
124         String content = TestIoUtil.readFileContent(file);
125         Map<String, Object> jsonMap = stringToJsonMap(content);
126         jsonMap = ((List<Map<String, Object>>)jsonMap.get("source_files")).get(0);
127         assertEquals("Foo.java", jsonMap.get("name"));
128         assertEquals("6E0F89B516198DC6AB743EA5FBFB3108", jsonMap.get("source_digest"));
129         assertEquals(1, ((Collection<?>) jsonMap.get("coverage")).size());
130     }
131 
132     private Job job() {
133         Git.Head head = new Git.Head("aefg837fge", "john", "john@mail.com", "john", "john@mail.com", "test commit");
134         Git.Remote remote = new Git.Remote("origin", "git@git.com:foo.git");
135         Properties environment = new Properties();
136         environment.setProperty("custom_property", "foobar");
137         return new Job()
138             .withServiceName("service")
139             .withServiceJobId("job123")
140             .withServiceBuildNumber("build5")
141             .withServiceBuildUrl("http://ci.com/build5")
142             .withServiceEnvironment(environment)
143             .withBranch("master")
144             .withPullRequest("pull10")
145             .withTimestamp(new Date(TEST_TIME))
146             .withGit(new Git(null, head, "af456fge34acd", Arrays.asList(remote)));
147     }
148 
149     private Source source() {
150         return new Source("Foo.java", "public class Foo { }", "6E0F89B516198DC6AB743EA5FBFB3108");
151     }
152 
153     private Map<String, Object> stringToJsonMap(final String content) throws Exception {
154         ObjectMapper mapper = new ObjectMapper();
155         MapType type = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
156         return mapper.readValue(content, type);
157     }
158 }