001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2026 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.test.app.harness.api;
011
012import ca.cdr.test.util.UrlPathUtil;
013import jakarta.annotation.Nonnull;
014import jakarta.annotation.Nullable;
015
016/**
017 * Record class that holds configuration information for connecting to a Smile CDR instance.
018 * This context can be used by {@link SmileHarness} implementations to establish connections to the CDR.
019 *
020 * @param protocol The connection protocol. Values are `http` or `https`
021 * @param baseUrl       The base URL of the Smile CDR instance, e.g. `127.0.0.1` or `localhost`
022 * @param jsonAdminPort The port number for the administrative JSON API. If null, will attempt to auto-discover based on the properties file.
023 * @param username      The username for authentication
024 * @param password      The password for authentication
025 * @param jsonAdminContextPath The {@code context_path} the Admin JSON module is served under, such as
026 *                      {@code /admin-json}. It is needed before the harness can read any other
027 *                      module's configuration. Blank and {@code /} both mean the root, which this
028 *                      record stores as the empty string; use the five-argument constructor for a
029 *                      root-mounted Admin JSON module.
030 */
031public record HarnessContext(
032                String protocol,
033                String baseUrl,
034                @Nullable Integer jsonAdminPort,
035                String username,
036                String password,
037                @Nonnull String jsonAdminContextPath) {
038
039        public HarnessContext {
040                jsonAdminContextPath = UrlPathUtil.normalizeContextPath(jsonAdminContextPath);
041        }
042
043        /**
044         * Creates a context whose Admin JSON module is served at the root.
045         */
046        public HarnessContext(
047                        String protocol, String baseUrl, @Nullable Integer jsonAdminPort, String username, String password) {
048                this(protocol, baseUrl, jsonAdminPort, username, password, "");
049        }
050
051        /**
052         * Factory method that creates a default HarnessContext for connecting to a local Smile CDR instance.
053         * This method provides predefined values for a standard out-of-box-experience (OOBE) setup. If you haven't provided a custom properties file,
054         * this harness context should be used. If you have customized users/endpoints, you should build your own HarnessContext.
055         *
056         * @return A HarnessContext configured with default values for localhost connection
057         */
058        public static HarnessContext oobeHarnessContext() {
059                return new HarnessContext("http", "localhost", null, "admin", "password");
060        }
061
062        public String getContextRoot() {
063                return protocol + "://" + baseUrl;
064        }
065}