1717import com .squareup .moshi .JsonAdapter ;
1818import com .squareup .moshi .JsonReader ;
1919import com .squareup .moshi .Moshi ;
20- import com .squareup .moshi .Moshi .Builder ;
2120import com .squareup .moshi .Types ;
2221import okio .Buffer ;
2322import org .junit .*;
@@ -31,7 +30,11 @@ public class ConfigurationTest {
3130 public EnvironmentVariables envVars = new EnvironmentVariables ();
3231
3332 private static Configuration loadConfiguration () throws IOException {
34- CharSource json = Resources .asCharSource (Resources .getResource ("applicationinsights.json" ), Charsets .UTF_8 );
33+ return loadConfiguration ("applicationinsights.json" );
34+ }
35+
36+ private static Configuration loadConfiguration (String resourceName ) throws IOException {
37+ CharSource json = Resources .asCharSource (Resources .getResource (resourceName ), Charsets .UTF_8 );
3538 Moshi moshi = MoshiBuilderFactory .createBuilderWithAdaptor ();
3639 JsonAdapter <Configuration > jsonAdapter = moshi .adapter (Configuration .class );
3740 return jsonAdapter .fromJson (json .read ());
@@ -154,6 +157,9 @@ public void shouldParseProcessorConfiguration() throws IOException {
154157
155158 @ Test
156159 public void shouldUseDefaults () throws IOException {
160+ envVars .set ("WEBSITE_SITE_NAME" , "role name from website env" );
161+ envVars .set ("WEBSITE_INSTANCE_ID" , "role instance from website env" );
162+
157163 Configuration configuration = loadConfiguration ();
158164
159165 assertEquals ("InstrumentationKey=00000000-0000-0000-0000-000000000000" , configuration .connectionString );
@@ -176,6 +182,68 @@ public void shouldOverrideConnectionString() throws IOException {
176182 assertEquals ("InstrumentationKey=11111111-1111-1111-1111-111111111111" , configuration .connectionString );
177183 }
178184
185+ @ Test
186+ public void shouldOverrideRoleName () throws IOException {
187+ envVars .set ("APPLICATIONINSIGHTS_ROLE_NAME" , "role name from env" );
188+ envVars .set ("WEBSITE_SITE_NAME" , "role name from website env" );
189+
190+ Configuration configuration = loadConfiguration ();
191+ ConfigurationBuilder .overlayEnvVars (configuration );
192+
193+ assertEquals ("role name from env" , configuration .role .name );
194+ }
195+
196+ @ Test
197+ public void shouldOverrideRoleNameWithWebsiteEnvVar () throws IOException {
198+ envVars .set ("WEBSITE_SITE_NAME" , "role name from website env" );
199+
200+ Configuration configuration = loadConfiguration ("applicationinsights_NoRole.json" );
201+ ConfigurationBuilder .overlayEnvVars (configuration );
202+
203+ assertEquals ("role name from website env" , configuration .role .name );
204+ }
205+
206+ @ Test
207+ public void shouldNotOverrideRoleNameWithWebsiteEnvVar () throws IOException {
208+ envVars .set ("WEBSITE_SITE_NAME" , "role name from website env" );
209+
210+ Configuration configuration = loadConfiguration ();
211+ ConfigurationBuilder .overlayEnvVars (configuration );
212+
213+ assertEquals ("Something Good" , configuration .role .name );
214+ }
215+
216+ @ Test
217+ public void shouldOverrideRoleInstance () throws IOException {
218+ envVars .set ("APPLICATIONINSIGHTS_ROLE_INSTANCE" , "role instance from env" );
219+ envVars .set ("WEBSITE_INSTANCE_ID" , "role instance from website env" );
220+
221+ Configuration configuration = loadConfiguration ();
222+ ConfigurationBuilder .overlayEnvVars (configuration );
223+
224+ assertEquals ("role instance from env" , configuration .role .instance );
225+ }
226+
227+ @ Test
228+ public void shouldOverrideRoleInstanceWithWebsiteEnvVar () throws IOException {
229+ envVars .set ("WEBSITE_INSTANCE_ID" , "role instance from website env" );
230+
231+ Configuration configuration = loadConfiguration ("applicationinsights_NoRole.json" );
232+ ConfigurationBuilder .overlayEnvVars (configuration );
233+
234+ assertEquals ("role instance from website env" , configuration .role .instance );
235+ }
236+
237+ @ Test
238+ public void shouldNotOverrideRoleInstanceWithWebsiteEnvVar () throws IOException {
239+ envVars .set ("WEBSITE_INSTANCE_ID" , "role instance from website env" );
240+
241+ Configuration configuration = loadConfiguration ();
242+ ConfigurationBuilder .overlayEnvVars (configuration );
243+
244+ assertEquals ("xyz123" , configuration .role .instance );
245+ }
246+
179247 @ Test
180248 public void shouldOverrideSamplingPercentage () throws IOException {
181249 envVars .set ("APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE" , "0.25" );
0 commit comments