public void AddNamedOptionsForCurrentTenantOnlyOnAdd(string name)
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Fail adding options under same name.
        result = cache.TryAdd(name, options);
        Assert.False(result);

        // Change the tenant id and confirm options can be added again.
        ti.Id  = "diff_id";
        result = cache.TryAdd(name, options);
        Assert.True(result);
    }
    public void GetOrAddNamedOptionForCurrentTenantOnly(string name)
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options  = new TestOptions();
        var options2 = new TestOptions();

        // Add new options.
        var result = cache.GetOrAdd(name, () => options);

        Assert.Same(options, result);

        // Get the existing options if exists.
        result = cache.GetOrAdd(name, () => options2);
        Assert.NotSame(options2, result);

        // Confirm different tenant on same object is an add (ie it didn't exist there).
        ti.Id  = "diff_id";
        result = cache.GetOrAdd(name, () => options2);
        Assert.Same(options2, result);
    }
    public void ThrowIfContructorParamIsNull()
    {
        var tc  = new MultiTenantContext <TenantInfo>();
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;

        Assert.Throws <ArgumentNullException>(() => new MultiTenantOptionsCache <TestOptions, TenantInfo>(null));
    }
    public void ThrowsIfGetOrAddFactoryIsNull()
    {
        var tc  = new MultiTenantContext <TenantInfo>();
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        Assert.Throws <ArgumentNullException>(() => cache.GetOrAdd("", null));
    }
Exemple #5
0
    public void ReturnNullIfNoHttpContext()
    {
        var httpContextAccessorMock = new Mock <IHttpContextAccessor>();

        httpContextAccessorMock.Setup(a => a.HttpContext).Returns((HttpContext)null);

        var accessor = new MultiTenantContextAccessor(httpContextAccessorMock.Object);

        Assert.Null(accessor.MultiTenantContext);
    }
    public void HandleNullMultiTenantContextOnGetOrAdd()
    {
        var tca   = new MultiTenantContextAccessor <TenantInfo>();
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options, ensure no exception caused by null MultiTenantContext.
        var result = cache.GetOrAdd("", () => options);

        Assert.NotNull(result);
    }
    public void RemoveNamedOptionsForCurrentTenantOnly(string name)
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Add under a different tenant.
        ti.Id  = "diff_id";
        result = cache.TryAdd(name, options);
        Assert.True(result);
        result = cache.TryAdd("diffname", options);
        Assert.True(result);

        // Remove named options for current tenant.
        result = cache.TryRemove(name);
        Assert.True(result);
        var tenantCache = (ConcurrentDictionary <string, IOptionsMonitorCache <TestOptions> >)cache.GetType().
                          GetField("map", BindingFlags.NonPublic | BindingFlags.Instance).
                          GetValue(cache);

        dynamic tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                                      .GetValue(tenantCache[ti.Id]);

        // Assert named options removed and other options on tenant left as-is.
        Assert.False(tenantInternalCache.Keys.Contains(name ?? ""));
        Assert.True(tenantInternalCache.Keys.Contains("diffname"));

        // Assert other tenant not affected.
        ti.Id = "test-id-123";
        tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                              .GetValue(tenantCache[ti.Id]);
        Assert.True(tenantInternalCache.ContainsKey(name ?? ""));
    }
    public void ClearOptionsForCurrentTenantOnly()
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options.
        var result = cache.TryAdd("", options);

        Assert.True(result);

        // Add under a different tenant.
        ti.Id  = "diff_id";
        result = cache.TryAdd("", options);
        Assert.True(result);

        // Clear options on first tenant.
        ti.Id = "test-id-123";
        cache.Clear();

        // Assert options cleared on this tenant.
        var tenantCache = (ConcurrentDictionary <string, IOptionsMonitorCache <TestOptions> >)cache.GetType().
                          GetField("map", BindingFlags.NonPublic | BindingFlags.Instance).
                          GetValue(cache);

        dynamic tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                                      .GetValue(tenantCache[ti.Id]);

        Assert.True(tenantInternalCache.IsEmpty);

        // Assert options still exist on other tenant.
        ti.Id = "diff_id";
        tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                              .GetValue(tenantCache[ti.Id]);
        Assert.False(tenantInternalCache.IsEmpty);
    }
Exemple #9
0
    public void GetMultiTenantContextFromIHttpAccessor()
    {
        var items = new Dictionary <object, object>();
        var ti    = new TenantInfo("test", null, null, null, null);
        var tc    = new MultiTenantContext();

        tc.TenantInfo = ti;
        items.Add(Finbuckle.MultiTenant.AspNetCore.Constants.HttpContextMultiTenantContext, tc);

        var httpContextMock = new Mock <HttpContext>();

        httpContextMock.Setup(c => c.Items).Returns(items);

        var httpContextAccessorMock = new Mock <IHttpContextAccessor>();

        httpContextAccessorMock.Setup(a => a.HttpContext).Returns(httpContextMock.Object);

        var accessor = new MultiTenantContextAccessor(httpContextAccessorMock.Object);

        Assert.Equal(ti.Id, accessor.MultiTenantContext.TenantInfo.Id);
    }