private static async Task <AuthenticationResult> CreateAppAndGetTokenAsync(
            CacheType cacheType,
            bool addTokenMock    = true,
            bool addInstanceMock = false)
        {
            using MockHttpClientFactory mockHttp = new MockHttpClientFactory();
            using var discoveryHandler           = MockHttpCreator.CreateInstanceDiscoveryMockHandler();
            using var tokenHandler = MockHttpCreator.CreateClientCredentialTokenHandler();
            if (addInstanceMock)
            {
                mockHttp.AddMockHandler(discoveryHandler);
            }

            // for when the token is requested from ESTS
            if (addTokenMock)
            {
                mockHttp.AddMockHandler(tokenHandler);
            }

            var confidentialApp = ConfidentialClientApplicationBuilder
                                  .Create(TestConstants.ClientId)
                                  .WithAuthority(TestConstants.AuthorityCommonTenant)
                                  .WithHttpClientFactory(mockHttp)
                                  .WithClientSecret(TestConstants.ClientSecret)
                                  .Build();

            switch (cacheType)
            {
            case CacheType.InMemory:
                confidentialApp.AddInMemoryTokenCache();
                break;

            case CacheType.DistributedInMemory:
                confidentialApp.AddDistributedTokenCache(services =>
                {
                    services.AddDistributedMemoryCache();
                    services.AddLogging(configure => configure.AddConsole())
                    .Configure <LoggerFilterOptions>(options => options.MinLevel = Microsoft.Extensions.Logging.LogLevel.Warning);
                });
                break;
            }

            var result = await confidentialApp.AcquireTokenForClient(new[] { TestConstants.s_scopeForApp })
                         .ExecuteAsync().ConfigureAwait(false);

            return(result);
        }