• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

goblint / GobPie / 9678350809

26 Jun 2024 10:54AM UTC coverage: 58.518%. First build
9678350809

Pull #72

github

web-flow
Merge 986be0326 into e8d777526
Pull Request #72: Add functional tests

123 of 233 branches covered (52.79%)

Branch coverage included in aggregate %.

128 of 160 new or added lines in 20 files covered. (80.0%)

406 of 671 relevant lines covered (60.51%)

2.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.22
/src/main/java/gobpie/GobPieConfReader.java
1
package gobpie;
2

3
import api.json.GobPieConfValidatorAdapterFactory;
4
import com.google.gson.*;
5
import magpiebridge.core.MagpieServer;
6
import org.apache.commons.lang3.exception.ExceptionUtils;
7
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
9
import org.eclipse.lsp4j.MessageParams;
10
import org.eclipse.lsp4j.MessageType;
11
import util.FileWatcher;
12

13
import java.io.FileNotFoundException;
14
import java.io.FileReader;
15
import java.nio.file.Files;
16
import java.nio.file.Path;
17

18
/**
19
 * The Class GobPieConfReader.
20
 * <p>
21
 * Class for parsing and reading GobPie configuration file.
22
 *
23
 * @author Karoliine Holter
24
 * @author Juhan Oskar Hennoste
25
 * @since 0.0.2
26
 */
27

28
public class GobPieConfReader {
29

30
    private final MagpieServer magpieServer;
31
    private final String gobPieConfFileName;
32
    private final Logger log = LogManager.getLogger(GobPieConfReader.class);
4✔
33

34
    public GobPieConfReader(MagpieServer magpieServer, String gobPieConfFileName) {
2✔
35
        this.magpieServer = magpieServer;
3✔
36
        this.gobPieConfFileName = gobPieConfFileName;
3✔
37
    }
1✔
38

39

40
    /**
41
     * Method for reading GobPie configuration.
42
     * <p>
43
     * Waits for the Gobpie configuration file to be created if one is not present in the project root.
44
     * Checks if all the required parameters are present in the configuration and
45
     * if not, waits for the file to be changed and reparses it until the user gives the parameters.
46
     *
47
     * @return GobPieConfiguration object.
48
     */
49
    public GobPieConfiguration readGobPieConfiguration() {
50

51
        // If GobPie configuration is not present, wait for it to be created
52
        Path gobPieConfPath = Path.of(gobPieConfFileName);
6✔
53
        try (FileWatcher gobPieConfWatcher = new FileWatcher(gobPieConfPath)) {
5✔
54
            if (!Files.exists(gobPieConfPath)) {
5✔
55
                String message = "GobPie configuration file is not found in the project root.";
2✔
56
                String terminalMessage = message + "\nPlease add GobPie configuration file into the project root.";
3✔
57
                forwardErrorMessageToClient(message, terminalMessage);
4✔
58
                gobPieConfWatcher.waitForModified();
2✔
59
            }
60

61
            // Parse the configuration file
62
            GobPieConfiguration gobpieConfiguration = parseGobPieConf();
3✔
63

64
            // Check if all required parameters have been set
65
            // If not, wait for change and reparse
66
            while (gobpieConfiguration.goblintConf() == null || gobpieConfiguration.goblintConf().isEmpty()) {
7!
67
                String message = "goblintConf parameter missing from GobPie configuration file.";
2✔
68
                String terminalMessage = message + "\nPlease add Goblint configuration file location into GobPie configuration as a parameter with name \"goblintConf\".";
3✔
69
                forwardErrorMessageToClient(message, terminalMessage);
4✔
70
                gobPieConfWatcher.waitForModified();
×
71
                gobpieConfiguration = parseGobPieConf();
×
72
            }
×
73

74
            return gobpieConfiguration;
4✔
75
        } catch (InterruptedException e) {
×
76
            return ExceptionUtils.rethrow(e);
×
77
        }
78

79
    }
80

81

82
    /**
83
     * Method for parsing GobPie configuration.
84
     * Deserializes json to GobPieConfiguration object.
85
     *
86
     * @return GobPieConfiguration object.
87
     * @throws GobPieException if
88
     *                         <ul>
89
     *                             <li>gobpie conf cannot be found to be read from;</li>
90
     *                             <li>gobpie conf json syntax is wrong.</li>
91
     *                         </ul>
92
     */
93

94
    public GobPieConfiguration parseGobPieConf() {
95
        try {
96
            log.debug("Reading GobPie configuration from json");
4✔
97
            Gson gson = new GsonBuilder()
6✔
98
                    .registerTypeAdapterFactory(new GobPieConfValidatorAdapterFactory())
1✔
99
                    .create();
2✔
100
            // Read json object
101
            JsonObject jsonObject = JsonParser.parseReader(new FileReader(gobPieConfFileName)).getAsJsonObject();
8✔
102
            // Convert json object to GobPieConfiguration object
103
            log.debug("GobPie configuration read from json");
4✔
104
            return gson.fromJson(jsonObject, GobPieConfiguration.class);
6✔
105
        } catch (JsonSyntaxException e) {
1✔
106
            throw new GobPieException("GobPie configuration file syntax is wrong.", e, GobPieExceptionType.GOBPIE_CONF_EXCEPTION);
7✔
107
        } catch (JsonParseException e) {
1✔
108
            throw new GobPieException("There was an unknown option \"" + e.getMessage() + "\" in the GobPie configuration. Please check for any typos.", e, GobPieExceptionType.GOBPIE_CONF_EXCEPTION);
9✔
NEW
109
        } catch (FileNotFoundException e) {
×
NEW
110
            throw new GobPieException("Could not locate GobPie configuration file.", e, GobPieExceptionType.GOBPIE_CONF_EXCEPTION);
×
111
        }
112
    }
113

114

115
    /**
116
     * Method for forwarding Error messages to MagpieServer.
117
     *
118
     * @param popUpMessage    The message shown on the pop-up message.
119
     * @param terminalMessage The message shown in the terminal.
120
     */
121

122
    private void forwardErrorMessageToClient(String popUpMessage, String terminalMessage) {
123
        magpieServer.forwardMessageToClient(
9✔
124
                new MessageParams(MessageType.Error, "Problem starting GobPie extension: " + popUpMessage + " Check the output terminal of GobPie extension for more information.")
125
        );
126
        log.error(terminalMessage);
4✔
127
    }
1✔
128

129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc