/** * Helper that uses {@link AppInfoImpl#detectAppId()} and {@link AppInfoImpl#detectEnvironment()} to get the app ID * and environment values, then returns {@link #getAppInfoFutureWithAwsInfo(String, String, AsyncHttpClientHelper)} * using those values. If either app ID or environment cannot be determined then an {@link IllegalStateException} * will be thrown, therefore if you know that your app's app ID and/or environment will not be successfully * extracted using those methods then you should call {@link #getAppInfoFutureWithAwsInfo(String, String, * AsyncHttpClientHelper)} directly with the correct values. * <p/> * See {@link #getAppInfoFutureWithAwsInfo(String, String, AsyncHttpClientHelper)} for more details on how the * {@link AppInfo} returned by the {@link CompletableFuture} will be structured. */ public static CompletableFuture<AppInfo> getAppInfoFutureWithAwsInfo(AsyncHttpClientHelper asyncHttpClientHelper) { String appId = AppInfoImpl.detectAppId(); if (appId == null) throw new IllegalStateException( "Unable to autodetect app ID. Please call getAppInfoFutureWithAwsInfo(String, String, " + "AsyncHttpClientHelper) instead and pass the app ID and environment manually" ); String environment = AppInfoImpl.detectEnvironment(); if (environment == null) throw new IllegalStateException( "Unable to autodetect environment. Please call getAppInfoFutureWithAwsInfo(String, String, " + "AsyncHttpClientHelper) instead and pass the app ID and environment manually" ); return getAppInfoFutureWithAwsInfo(appId, environment, asyncHttpClientHelper); }
@Test public void getAppInfoFutureWithAwsInfo_with_minimal_args_throws_IllegalStateException_if_appId_is_missing() { // given setAppIdAndEnvironemntSystemProperties(null, UUID.randomUUID().toString()); // when Throwable ex = catchThrowable(() -> AwsUtil.getAppInfoFutureWithAwsInfo(asyncClientMock)); // then assertThat(ex).isInstanceOf(IllegalStateException.class); }
@Test public void getAppInfoFutureWithAwsInfo_with_minimal_args_throws_IllegalStateException_if_environment_is_missing() { // given setAppIdAndEnvironemntSystemProperties(UUID.randomUUID().toString(), null); // when Throwable ex = catchThrowable(() -> AwsUtil.getAppInfoFutureWithAwsInfo(asyncClientMock)); // then assertThat(ex).isInstanceOf(IllegalStateException.class); } }
@Test public void getAppInfoFutureWithAwsInfo_with_all_args_uses_data_from_getAwsRegion_and_getAwsInstanceId_to_build_result() { // given String appId = "appid-" + UUID.randomUUID().toString(); String environment = "environment-" + UUID.randomUUID().toString(); String expectedDataCenter = AwsUtil.getAwsRegion(asyncClientMock).join(); String expectedInstanceId = AwsUtil.getAwsInstanceId(asyncClientMock).join(); // when AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(appId, environment, asyncClientMock).join(); // then assertThat(result.appId()).isEqualTo(appId); assertThat(result.environment()).isEqualTo(environment); assertThat(result.dataCenter()).isEqualTo(expectedDataCenter); assertThat(result.instanceId()).isEqualTo(expectedInstanceId); }
@Test public void getAppInfoFutureWithAwsInfo_with_all_args_uses_InetAddress_local_hostname_if_getAwsInstanceId_returns_unknown() throws IOException { // given String appId = "appid-" + UUID.randomUUID().toString(); String environment = "environment-" + UUID.randomUUID().toString(); String expectedDataCenter = AwsUtil.getAwsRegion(asyncClientMock).join(); doReturn(null).when(responseMockForAwsInstanceId).getResponseBody(); String expectedInstanceId = InetAddress.getLocalHost().getHostName(); // when AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(appId, environment, asyncClientMock).join(); // then assertThat(result.appId()).isEqualTo(appId); assertThat(result.environment()).isEqualTo(environment); assertThat(result.dataCenter()).isEqualTo(expectedDataCenter); assertThat(result.instanceId()).isEqualTo(expectedInstanceId); }
@Test public void getAppInfoFutureWithAwsInfo_with_minimal_args_delegates_to_kitchen_sink_overload_method() { // given String appId = "appid-" + UUID.randomUUID().toString(); String environment = "environment-" + UUID.randomUUID().toString(); String expectedDataCenter = AwsUtil.getAwsRegion(asyncClientMock).join(); String expectedInstanceId = AwsUtil.getAwsInstanceId(asyncClientMock).join(); setAppIdAndEnvironemntSystemProperties(appId, environment); // when AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(asyncClientMock).join(); // then assertThat(result.appId()).isEqualTo(appId); assertThat(result.environment()).isEqualTo(environment); assertThat(result.dataCenter()).isEqualTo(expectedDataCenter); assertThat(result.instanceId()).isEqualTo(expectedInstanceId); }
@DataProvider(value = { "local", "compiletimetest" }) @Test public void getAppInfoFutureWithAwsInfo_with_all_args_returns_AppInfoImpl_createLocalInstance_if_environment_is_local_or_compiletimetest( String environment ) { // given String appId = "appid-" + UUID.randomUUID().toString(); AppInfo expectedResult = AppInfoImpl.createLocalInstance(appId); // when AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(appId, environment, asyncClientMock).join(); // then assertThat(result.appId()).isEqualTo(expectedResult.appId()).isEqualTo(appId); assertThat(result.environment()).isEqualTo(expectedResult.environment()); assertThat(result.dataCenter()).isEqualTo(expectedResult.dataCenter()); assertThat(result.instanceId()).isEqualTo(expectedResult.instanceId()); }