1+ /*
2+ * Copyright (c) 2019 IBM Corporation and others
3+ *
4+ * See the NOTICE file(s) distributed with this work for additional
5+ * information regarding copyright ownership.
6+ *
7+ * Licensed under the Apache License, Version 2.0 (the "License");
8+ * You may not use this file except in compliance with the License.
9+ * You may obtain a copy of the License at
10+ *
11+ * http://www.apache.org/licenses/LICENSE-2.0
12+ *
13+ * Unless required by applicable law or agreed to in writing, software
14+ * distributed under the License is distributed on an "AS IS" BASIS,
15+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ * See the License for the specific language governing permissions and
17+ * limitations under the License.
18+ */
19+ package org .microshed .testing .testcontainers .config ;
20+
21+ import static org .junit .jupiter .api .Assertions .assertEquals ;
22+ import static org .junit .jupiter .api .Assertions .assertTrue ;
23+
24+ import java .nio .file .Paths ;
25+ import java .util .Map ;
26+
27+ import org .junit .jupiter .api .Test ;
28+ import org .microshed .testing .ApplicationEnvironment ;
29+ import org .microshed .testing .jupiter .MicroShedTest ;
30+ import org .microshed .testing .testcontainers .ApplicationContainer ;
31+ import org .testcontainers .containers .MockServerContainer ;
32+ import org .testcontainers .junit .jupiter .Container ;
33+
34+ @ MicroShedTest
35+ public class HollowTestcontainersConfigurationTest {
36+
37+ // This cointainer never actually gets started, since we are running in hollow mode
38+ @ Container
39+ public static ApplicationContainer app = new ApplicationContainer (Paths .get ("src" , "test" , "resources" , "Dockerfile" ))
40+ .withEnv ("SVC_HOST" , "mockserver" )
41+ .withEnv ("SVC_PORT" , "1080" )
42+ .withEnv ("SVC_URL1" , "mockserver" )
43+ .withEnv ("SVC_URL2" , "mockserver:1080" )
44+ .withEnv ("SVC_URL3" , "http://mockserver:1080" )
45+ .withEnv ("SVC_URL4" , "http://mockserver:1080/hello/world" )
46+ .withEnv ("SVC_URL5" , "http://mockserver:1080/hello/mockserver" )
47+ .withEnv ("SVC_URL6" , oldValue -> "http://mockserver:1080" )
48+ .withMpRestClient ("com.foo.ExampleClass" , "http://mockserver:1080" );
49+
50+ @ Container
51+ public static MockServerContainer mockServer = new MockServerContainer ()
52+ .withNetworkAliases ("mockserver" )
53+ .withEnv ("STAYS_UNCHANGED" , "mockserver" );
54+
55+ @ Test
56+ public void testCorrectEnvironment () {
57+ assertEquals (HollowTestcontainersConfiguration .class , ApplicationEnvironment .load ().getClass ());
58+ assertTrue (ApplicationEnvironment .isSelected (HollowTestcontainersConfiguration .class ),
59+ "Expected HollowTestcontainersConfiguration to be selected but it was not" );
60+ assertTrue (HollowTestcontainersConfiguration .available (),
61+ "Expected HollowTestcontainersConfiguration to be available but it was not" );
62+ }
63+
64+ @ Test
65+ public void testFixedExposedPort () {
66+ assertEquals (9080 , app .getMappedPort (9080 ));
67+ assertEquals (1080 , mockServer .getMappedPort (1080 ));
68+ }
69+
70+ @ Test
71+ public void testEnvVarTranslation () {
72+ Map <String , String > envMap = app .getEnvMap ();
73+ assertEquals ("localhost" , envMap .get ("SVC_HOST" ));
74+ assertEquals ("localhost" , envMap .get ("SVC_URL1" ));
75+ assertEquals ("localhost:1080" , envMap .get ("SVC_URL2" ));
76+ assertEquals ("http://localhost:1080" , envMap .get ("SVC_URL3" ));
77+ assertEquals ("http://localhost:1080/hello/world" , envMap .get ("SVC_URL4" ));
78+ assertEquals ("http://localhost:1080/hello/mockserver" , envMap .get ("SVC_URL5" ));
79+ assertEquals ("http://localhost:1080" , envMap .get ("SVC_URL6" ));
80+ assertEquals ("http://localhost:1080" , envMap .get ("com_foo_ExampleClass_mp_rest_url" ));
81+ }
82+
83+ @ Test
84+ public void testEnvVarUnchanged () {
85+ assertEquals ("1080" , app .getEnvMap ().get ("SVC_PORT" ));
86+ assertEquals ("mockserver" , mockServer .getEnvMap ().get ("STAYS_UNCHANGED" ));
87+ }
88+
89+ @ Test
90+ public void testApplicationURL () {
91+ assertEquals ("http://localhost:9080/" , ApplicationEnvironment .load ().getApplicationURL ());
92+ }
93+
94+ }
0 commit comments