Class Tm1EngineContainer

java.lang.Object
org.testcontainers.containers.GenericContainer<Tm1EngineContainer>
io.skipcheck.tm1.testcontainers.Tm1EngineContainer
All Implemented Interfaces:
AutoCloseable, org.testcontainers.containers.Container<Tm1EngineContainer>, org.testcontainers.containers.ContainerState, org.testcontainers.containers.traits.LinkableContainer, org.testcontainers.containers.wait.strategy.WaitStrategyTarget, org.testcontainers.lifecycle.Startable

public class Tm1EngineContainer extends org.testcontainers.containers.GenericContainer<Tm1EngineContainer>
A TM1 v12 engine (Planning Analytics 3.x) in Docker, for tests: the tm1-engine image with TM1 from a Tm1EngineSource, and the MongoDB it keeps its metadata in, on a network of their own. For TM1 11, use Tm1ServerContainer.

One engine holds instances, each holding databases. Most tests use the one instance, tm1, and a database of their own: RequiresTm1Engine gives each test class one. A container you make yourself has the databases you name with withDatabase(String), or one called tm1:

try (var engine = new Tm1EngineContainer(source).withDatabase("planning").withUser("analyst", "Analysts")) {
	engine.start();
	Tm1Database planning = engine.database("planning");
	// http://localhost:54321/tm1/api/v1/Databases('planning')/
	String url = planning.getRestUrl();
	Tm1Login analyst = planning.login("analyst");
	// ...

TM1 v12 has no passwords of its own. A test logs in through the instance's application (Tm1Login), which this container creates and keeps the credentials of.

Starting takes about 40 seconds, and a database about 10 more to create (createDatabase(String)). The image exists for x86-64 only, as TM1 does; on Apple Silicon it runs under Rosetta.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.testcontainers.containers.Container

    org.testcontainers.containers.Container.ExecResult
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The volume package installs are cached in, shared by every engine container on this Docker host.
    static final String
    The datastore's name on the engine's network.
    static final org.testcontainers.utility.DockerImageName
    The MongoDB image the engine's datastore runs.
    static final String
    The database a container has when none is named.
    static final org.testcontainers.utility.DockerImageName
    The tm1-engine image, without a tag.
    static final String
    The instance databases are in unless one is named.
    static final String
    Overrides the tm1-engine image (not used with Tm1EngineSource.PrivateImage), e.g.
    static final String
    The client of that realm a test asks for tokens as, and the audience its tokens carry.
    static final String
    The Keycloak realm an engine with withOAuth2(KeycloakContainer) takes tokens from.
    static final String
    The name a TM1 server has on the engine's network when logins are passed to it (withPassThrough(Tm1ServerContainer)).
    static final int
    The port of TM1's gateway inside the container: the REST API, logins and management.

    Fields inherited from class org.testcontainers.containers.GenericContainer

    CONTAINER_RUNNING_TIMEOUT_SEC, dependencies, dockerClient, INTERNAL_HOST_HOSTNAME, waitStrategy

    Fields inherited from interface org.testcontainers.containers.ContainerState

    STATE_HEALTHY
  • Constructor Summary

    Constructors
    Constructor
    Description
    A container that gets TM1 from source, with the default image for it.
    Tm1EngineContainer(org.testcontainers.utility.DockerImageName image, Tm1EngineSource source)
    A container from an image of your own, such as a locally built one.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
     
    protected void
    containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse info)
     
    Creates a database in the instance tm1 of the running engine, with the users of withUser(String, String...).
    createDatabase(String instance, String name)
    Creates a database in a named instance of the running engine, which must exist.
    A database of the instance tm1.
    database(String instance, String name)
    A database of a named instance.
    The databases the engine has now, in every instance.
    void
    Deletes a database of the running engine, with everything in it.
    org.testcontainers.containers.GenericContainer<?>
    The MongoDB the engine keeps its metadata in, started and stopped with it.
    The Keycloak the engine takes OAuth 2.0 tokens from.
    The management API, which lists and creates instances, applications and databases, e.g.
    The TM1 server logins are passed to.
    The client ID of the engine's root credentials, for the management API.
    The client secret of the engine's root credentials.
    Where TM1 comes from.
    The version of TM1 the engine runs.
    The gateway as seen from the host, e.g.
    The engine most tests share: one per test JVM, started on first use with the source Tm1EngineSource.discover() finds, and stopped by Testcontainers when the JVM exits.
    void
     
    void
     
    Adds a database, in the instance tm1.
    withDatabase(String instance, String name)
    Adds a database in a named instance, which is created with an application of its own.
    withLogin(String user, String password)
    Gives a user a password to log in with through OAuth 2.0 (withOAuth2(KeycloakContainer)).
    withNetwork(org.testcontainers.containers.Network network)
    Puts the engine, and its datastore with it, on a network.
    Takes OAuth 2.0 logins: tokens from a Keycloak, which gets a realm (tm1) with the users given a password by withLogin(String, String).
    Passes logins on to a TM1 11 server ("pass-through"): TM1 v12 asks it who a login is, so that server's users log in here with what they have there.
    withUser(String name, String... groups)
    Adds a user to every database, in exactly these groups (created if missing).

    Methods inherited from class org.testcontainers.containers.GenericContainer

    addEnv, addExposedPort, addExposedPorts, addFileSystemBind, addFixedExposedPort, addFixedExposedPort, addLink, canBeReused, containerIsCreated, containerIsStarted, containerIsStarting, containerIsStarting, containerIsStopped, containerIsStopping, copyFileFromContainer, createVolumeDirectory, dependsOn, dependsOn, dependsOn, doStart, equals, getBinds, getCommandParts, getContainerId, getContainerInfo, getContainerName, getCopyToFileContainerPathMap, getCreateContainerCmdModifiers, getDependencies, getDockerClient, getDockerImageName, getEnv, getEnvMap, getExposedPorts, getExtraHosts, getImage, getIpAddress, getLabels, getLinkedContainers, getLivenessCheckPort, getLivenessCheckPortNumbers, getLivenessCheckPorts, getLogConsumers, getNetwork, getNetworkAliases, getNetworkMode, getPortBindings, getShmSize, getStartupAttempts, getStartupCheckStrategy, getTestHostIpAddress, getTmpFsMapping, getVolumesFroms, getWaitStrategy, getWorkingDirectory, hashCode, isHostAccessible, isPrivilegedMode, isShouldBeReused, logger, setBinds, setCommand, setCommand, setCommandParts, setContainerDef, setCopyToFileContainerPathMap, setDockerImageName, setEnv, setExposedPorts, setExtraHosts, setHostAccessible, setImage, setLabels, setLinkedContainers, setLogConsumers, setNetwork, setNetworkAliases, setNetworkMode, setPortBindings, setPrivilegedMode, setShmSize, setStartupAttempts, setStartupCheckStrategy, setTmpFsMapping, setVolumesFroms, setWaitStrategy, setWorkingDirectory, toString, waitingFor, waitUntilContainerStarted, withAccessToHost, withClasspathResourceMapping, withClasspathResourceMapping, withCommand, withCommand, withCopyFileToContainer, withCopyToContainer, withCreateContainerCmdModifier, withEnv, withEnv, withExposedPorts, withExtraHost, withFileSystemBind, withImagePullPolicy, withLabel, withLabels, withLogConsumer, withMinimumRunningDuration, withNetworkAliases, withNetworkMode, withPrivilegedMode, withReuse, withSharedMemorySize, withStartupAttempts, withStartupCheckStrategy, withStartupTimeout, withTmpFs, withVolumesFrom, withWorkingDirectory

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.testcontainers.containers.Container

    addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBind

    Methods inherited from interface org.testcontainers.containers.ContainerState

    copyFileFromContainer, copyFileToContainer, copyFileToContainer, execInContainer, execInContainer, execInContainer, execInContainer, execInContainerWithUser, execInContainerWithUser, getBoundPortNumbers, getContainerIpAddress, getCurrentContainerInfo, getFirstMappedPort, getHost, getLogs, getLogs, getMappedPort, isCreated, isHealthy, isRunning

    Methods inherited from interface org.testcontainers.lifecycle.Startable

    close
  • Field Details

    • DEFAULT_IMAGE

      public static final org.testcontainers.utility.DockerImageName DEFAULT_IMAGE
      The tm1-engine image, without a tag.
    • ENGINE_IMAGE

      public static final String ENGINE_IMAGE
      Overrides the tm1-engine image (not used with Tm1EngineSource.PrivateImage), e.g. tm1-engine:local for a local build. Read as a system property, then an environment variable.
      See Also:
    • DATASTORE_IMAGE

      public static final org.testcontainers.utility.DockerImageName DATASTORE_IMAGE
      The MongoDB image the engine's datastore runs.
    • PORT

      public static final int PORT
      The port of TM1's gateway inside the container: the REST API, logins and management.
      See Also:
    • DEFAULT_INSTANCE

      public static final String DEFAULT_INSTANCE
      The instance databases are in unless one is named.
      See Also:
    • DEFAULT_DATABASE

      public static final String DEFAULT_DATABASE
      The database a container has when none is named.
      See Also:
    • DATASTORE_ALIAS

      public static final String DATASTORE_ALIAS
      The datastore's name on the engine's network. It's the same for every engine, so give each engine a network of its own, as it has unless you set one: two engines on one network would each find either's datastore under this name.
      See Also:
    • CACHE_VOLUME

      public static final String CACHE_VOLUME
      The volume package installs are cached in, shared by every engine container on this Docker host. Only the first container to see a package installs it. Remove the volume to clear it.
      See Also:
    • OAUTH2_REALM

      public static final String OAUTH2_REALM
      The Keycloak realm an engine with withOAuth2(KeycloakContainer) takes tokens from.
      See Also:
    • OAUTH2_CLIENT

      public static final String OAUTH2_CLIENT
      The client of that realm a test asks for tokens as, and the audience its tokens carry.
      See Also:
    • PASS_THROUGH_ALIAS

      public static final String PASS_THROUGH_ALIAS
      The name a TM1 server has on the engine's network when logins are passed to it (withPassThrough(Tm1ServerContainer)).
      See Also:
  • Constructor Details

    • Tm1EngineContainer

      public Tm1EngineContainer(Tm1EngineSource source)
      A container that gets TM1 from source, with the default image for it.
      Parameters:
      source - where TM1 comes from
    • Tm1EngineContainer

      public Tm1EngineContainer(org.testcontainers.utility.DockerImageName image, Tm1EngineSource source)
      A container from an image of your own, such as a locally built one.
      Parameters:
      image - the image to run
      source - where TM1 comes from
  • Method Details

    • withNetwork

      public Tm1EngineContainer withNetwork(org.testcontainers.containers.Network network)
      Puts the engine, and its datastore with it, on a network. They have one of their own unless this is called.
      Specified by:
      withNetwork in interface org.testcontainers.containers.Container<Tm1EngineContainer>
      Overrides:
      withNetwork in class org.testcontainers.containers.GenericContainer<Tm1EngineContainer>
      Parameters:
      network - the network
      Returns:
      this container
    • shared

      public static Tm1EngineContainer shared()
      The engine most tests share: one per test JVM, started on first use with the source Tm1EngineSource.discover() finds, and stopped by Testcontainers when the JVM exits. It has one database, tm1; RequiresTm1Engine creates one more for each test class.

      It isn't marked reusable: an engine and its datastore belong together, and a kept engine would keep every database tests left in it.

      Returns:
      the started shared engine
      Throws:
      IllegalStateException - if no source is configured
    • withDatabase

      public Tm1EngineContainer withDatabase(String name)
      Adds a database, in the instance tm1. Without any, the container has one called tm1.
      Parameters:
      name - the database's name: at least three letters, digits, _ or -. Sales and sales are two databases
      Returns:
      this container
    • withDatabase

      public Tm1EngineContainer withDatabase(String instance, String name)
      Adds a database in a named instance, which is created with an application of its own. An instance's users, sessions and applications are separate from another's.
      Parameters:
      instance - the instance's name
      name - the database's name
      Returns:
      this container
    • withUser

      public Tm1EngineContainer withUser(String name, String... groups)
      Adds a user to every database, in exactly these groups (created if missing). A new database has one user already: admin, in the ADMIN group.
      Parameters:
      name - the user's name
      groups - the groups the user is in
      Returns:
      this container
    • withOAuth2

      public Tm1EngineContainer withOAuth2(KeycloakContainer keycloak)
      Takes OAuth 2.0 logins: tokens from a Keycloak, which gets a realm (tm1) with the users given a password by withLogin(String, String). A test then logs in with Tm1Database.oauth2Login(String). Logins through the engine's application (Tm1Database.login()) keep working beside it.

      The Keycloak joins the engine's network, unless it has one already, which the engine then joins. It's yours to stop: the engine only starts it first.

      // Starting the engine starts Keycloak first; the Keycloak is yours to close
      try (var keycloak = new KeycloakContainer();
      		var engine = new Tm1EngineContainer(source).withUser("analyst", "Analysts")
      			.withLogin("analyst", "analystpw")
      			.withOAuth2(keycloak)) {
      	engine.start();
      	Tm1Database db = engine.database("tm1");
      	// A token from Keycloak, with the analyst's password, sent to TM1 as Bearer
      	HttpRequest request = db.oauth2Login("analyst")
      		.authorizeWithToken(HttpRequest.newBuilder(URI.create(db.getRestUrl() + "Cubes?$select=Name")))
      		.build();
      
      Parameters:
      keycloak - the Keycloak to take tokens from
      Returns:
      this container
    • withPassThrough

      public Tm1EngineContainer withPassThrough(Tm1ServerContainer server)
      Passes logins on to a TM1 11 server ("pass-through"): TM1 v12 asks it who a login is, so that server's users log in here with what they have there. A test logs in with Tm1Database.passThroughLogin(). Logins through the engine's application (Tm1Database.login()) keep working beside it.

      The server joins the engine's network, unless it has one already, which the engine and its datastore then join. Starting the engine starts the server first. Give it one that isn't started: it gets the name the engine reaches it by, PASS_THROUGH_ALIAS, so one network holds one such server.

      // Starting the engine starts the TM1 11 server first; the server is yours to
      // close
      try (var server = new Tm1ServerContainer(tm1).withSeed(Tm1Seed.SDATA);
      		var engine = new Tm1EngineContainer(source).withPassThrough(server)) {
      	engine.start();
      	Tm1Database db = engine.database("tm1");
      	// The sample database's admin / apple, which TM1 v12 asks the server about
      	HttpRequest request = db.passThroughLogin()
      		.authorize(HttpRequest.newBuilder(URI.create(db.getRestUrl() + "Cubes?$select=Name")))
      		.build();
      
      Parameters:
      server - the TM1 server that checks logins
      Returns:
      this container
    • getPassThrough

      public @Nullable Tm1ServerContainer getPassThrough()
      The TM1 server logins are passed to.
      Returns:
      the server, or null without withPassThrough(Tm1ServerContainer)
    • withLogin

      public Tm1EngineContainer withLogin(String user, String password)
      Gives a user a password to log in with through OAuth 2.0 (withOAuth2(KeycloakContainer)). TM1 v12 keeps no passwords itself: it's the Keycloak's. The user is one added with withUser(String, String...), or admin.
      Parameters:
      user - the user's name
      password - the password
      Returns:
      this container
    • getKeycloak

      public @Nullable KeycloakContainer getKeycloak()
      The Keycloak the engine takes OAuth 2.0 tokens from.
      Returns:
      the Keycloak, or null without withOAuth2(KeycloakContainer)
    • getSource

      public Tm1EngineSource getSource()
      Where TM1 comes from.
      Returns:
      the source
    • getDatastore

      public org.testcontainers.containers.GenericContainer<?> getDatastore()
      The MongoDB the engine keeps its metadata in, started and stopped with it.
      Returns:
      the datastore's container
    • getUrl

      public String getUrl()
      The gateway as seen from the host, e.g. http://localhost:54321/.
      Returns:
      the gateway's root, ending in /
    • getManagementUrl

      public String getManagementUrl()
      The management API, which lists and creates instances, applications and databases, e.g. http://localhost:54321/manage/v1/. It takes the root credentials as Basic authentication (getRootClientId(), getRootClientSecret()).
      Returns:
      the management API's root, ending in /
    • getRootClientId

      public String getRootClientId()
      The client ID of the engine's root credentials, for the management API.
      Returns:
      the client ID
    • getRootClientSecret

      public String getRootClientSecret()
      The client secret of the engine's root credentials.
      Returns:
      the client secret
    • getTm1Version

      public Tm1Version getTm1Version()
      The version of TM1 the engine runs. Only available once started.
      Returns:
      the version
      Throws:
      IllegalStateException - if the container isn't started yet
    • databases

      public List<Tm1Database> databases()
      The databases the engine has now, in every instance.
      Returns:
      the databases
    • database

      public Tm1Database database(String name)
      A database of the instance tm1.
      Parameters:
      name - the database's name
      Returns:
      the database
      Throws:
      IllegalArgumentException - if there's no such database
    • database

      public Tm1Database database(String instance, String name)
      A database of a named instance.
      Parameters:
      instance - the instance's name
      name - the database's name
      Returns:
      the database
      Throws:
      IllegalArgumentException - if there's no such database
    • createDatabase

      public Tm1Database createDatabase(String name)
      Creates a database in the instance tm1 of the running engine, with the users of withUser(String, String...). Takes about 10 seconds.
      Parameters:
      name - the new database's name
      Returns:
      the database
    • createDatabase

      public Tm1Database createDatabase(String instance, String name)
      Creates a database in a named instance of the running engine, which must exist.
      Parameters:
      instance - the instance's name
      name - the new database's name
      Returns:
      the database
    • dropDatabase

      public void dropDatabase(Tm1Database database)
      Deletes a database of the running engine, with everything in it. Takes about 5 seconds.
      Parameters:
      database - the database
    • start

      public void start()
      Specified by:
      start in interface org.testcontainers.lifecycle.Startable
      Overrides:
      start in class org.testcontainers.containers.GenericContainer<Tm1EngineContainer>
    • configure

      protected void configure()
      Overrides:
      configure in class org.testcontainers.containers.GenericContainer<Tm1EngineContainer>
    • containerIsStarted

      protected void containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse info)
      Overrides:
      containerIsStarted in class org.testcontainers.containers.GenericContainer<Tm1EngineContainer>
    • stop

      public void stop()
      Specified by:
      stop in interface org.testcontainers.lifecycle.Startable
      Overrides:
      stop in class org.testcontainers.containers.GenericContainer<Tm1EngineContainer>