diff --git a/internal/service/pinpoint/app.go b/internal/service/pinpoint/app.go index 944572ad808..b354a97cdd3 100644 --- a/internal/service/pinpoint/app.go +++ b/internal/service/pinpoint/app.go @@ -313,7 +313,7 @@ func findAppSettingsByID(ctx context.Context, conn *pinpoint.Pinpoint, id string } func expandCampaignHook(configs []interface{}) *pinpoint.CampaignHook { - if len(configs) == 0 { + if len(configs) == 0 || configs[0] == nil { return nil } @@ -351,7 +351,7 @@ func flattenCampaignHook(ch *pinpoint.CampaignHook) []interface{} { } func expandCampaignLimits(configs []interface{}) *pinpoint.CampaignLimits { - if len(configs) == 0 { + if len(configs) == 0 || configs[0] == nil { return nil } @@ -394,7 +394,7 @@ func flattenCampaignLimits(cl *pinpoint.CampaignLimits) []interface{} { } func expandQuietTime(configs []interface{}) *pinpoint.QuietTime { - if len(configs) == 0 { + if len(configs) == 0 || configs[0] == nil { return nil } diff --git a/internal/service/pinpoint/app_test.go b/internal/service/pinpoint/app_test.go index 6540916a673..0077b962b4e 100644 --- a/internal/service/pinpoint/app_test.go +++ b/internal/service/pinpoint/app_test.go @@ -12,7 +12,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/statecheck" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" "github.com/hashicorp/terraform-provider-aws/internal/acctest" "github.com/hashicorp/terraform-provider-aws/internal/conns" tfpinpoint "github.com/hashicorp/terraform-provider-aws/internal/service/pinpoint" @@ -38,13 +41,34 @@ func TestAccPinpointApp_basic(t *testing.T) { testAccCheckAppExists(ctx, resourceName, &application), resource.TestCheckResourceAttrSet(resourceName, names.AttrApplicationID), resource.TestCheckResourceAttrSet(resourceName, names.AttrARN), - resource.TestCheckResourceAttr(resourceName, "campaign_hook.#", acctest.Ct1), - resource.TestCheckResourceAttr(resourceName, "limits.#", acctest.Ct1), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, names.AttrNamePrefix, ""), - resource.TestCheckResourceAttr(resourceName, "quiet_time.#", acctest.Ct1), - resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("campaign_hook"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "lambda_function_name": knownvalue.StringExact(""), + names.AttrMode: knownvalue.StringExact(""), + "web_url": knownvalue.StringExact(""), + }), + })), + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("limits"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "daily": knownvalue.Int64Exact(0), + "maximum_duration": knownvalue.Int64Exact(0), + "messages_per_second": knownvalue.Int64Exact(0), + "total": knownvalue.Int64Exact(0), + }), + })), + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("quiet_time"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "end": knownvalue.StringExact(""), + "start": knownvalue.StringExact(""), + }), + })), + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTags), knownvalue.Null()), + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTagsAll), knownvalue.MapExact(map[string]knownvalue.Check{})), + }, }, { ResourceName: resourceName, @@ -187,9 +211,53 @@ func TestAccPinpointApp_campaignHookLambda(t *testing.T) { Config: testAccAppConfig_campaignHookLambda(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAppExists(ctx, resourceName, &application), - resource.TestCheckResourceAttr(resourceName, "campaign_hook.#", acctest.Ct1), - resource.TestCheckResourceAttr(resourceName, "campaign_hook.0.mode", "DELIVERY"), + resource.TestCheckResourceAttrPair(resourceName, "campaign_hook.0.lambda_function_name", "aws_lambda_function.test", names.AttrARN), ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("campaign_hook"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "lambda_function_name": knownvalue.NotNull(), // Should be a Pair function, waiting on https://github.com/hashicorp/terraform-plugin-testing/pull/330 + names.AttrMode: knownvalue.StringExact(pinpoint.ModeDelivery), + "web_url": knownvalue.StringExact(""), + }), + })), + }, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccPinpointApp_campaignHookEmpty(t *testing.T) { + ctx := acctest.Context(t) + var application pinpoint.ApplicationResponse + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_pinpoint_app.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckApp(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.PinpointServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckAppDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccAppConfig_campaignHookEmpty(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAppExists(ctx, resourceName, &application), + ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("campaign_hook"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "lambda_function_name": knownvalue.StringExact(""), + names.AttrMode: knownvalue.StringExact(""), + "web_url": knownvalue.StringExact(""), + }), + })), + }, }, { ResourceName: resourceName, @@ -216,9 +284,54 @@ func TestAccPinpointApp_limits(t *testing.T) { Config: testAccAppConfig_limits(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAppExists(ctx, resourceName, &application), - resource.TestCheckResourceAttr(resourceName, "limits.#", acctest.Ct1), - resource.TestCheckResourceAttr(resourceName, "limits.0.total", "100"), ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("limits"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "daily": knownvalue.Int64Exact(3), + "maximum_duration": knownvalue.Int64Exact(600), + "messages_per_second": knownvalue.Int64Exact(50), + "total": knownvalue.Int64Exact(100), + }), + })), + }, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccPinpointApp_limitsEmpty(t *testing.T) { + ctx := acctest.Context(t) + var application pinpoint.ApplicationResponse + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_pinpoint_app.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckApp(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.PinpointServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckAppDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccAppConfig_limitsEmpty(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAppExists(ctx, resourceName, &application), + ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("limits"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "daily": knownvalue.Int64Exact(0), + "maximum_duration": knownvalue.Int64Exact(0), + "messages_per_second": knownvalue.Int64Exact(0), + "total": knownvalue.Int64Exact(0), + }), + })), + }, }, { ResourceName: resourceName, @@ -245,9 +358,50 @@ func TestAccPinpointApp_quietTime(t *testing.T) { Config: testAccAppConfig_quietTime(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAppExists(ctx, resourceName, &application), - resource.TestCheckResourceAttr(resourceName, "quiet_time.#", acctest.Ct1), - resource.TestCheckResourceAttr(resourceName, "quiet_time.0.start", "00:00"), ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("quiet_time"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "end": knownvalue.StringExact("03:00"), + "start": knownvalue.StringExact("00:00"), + }), + })), + }, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccPinpointApp_quietTimeEmpty(t *testing.T) { + ctx := acctest.Context(t) + var application pinpoint.ApplicationResponse + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_pinpoint_app.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckApp(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.PinpointServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckAppDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccAppConfig_quietTimeEmpty(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAppExists(ctx, resourceName, &application), + ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("quiet_time"), knownvalue.ListExact([]knownvalue.Check{ + knownvalue.ObjectExact(map[string]knownvalue.Check{ + "end": knownvalue.StringExact(""), + "start": knownvalue.StringExact(""), + }), + })), + }, }, { ResourceName: resourceName, @@ -415,6 +569,16 @@ resource "aws_lambda_permission" "test" { `, rName) } +func testAccAppConfig_campaignHookEmpty(rName string) string { + return fmt.Sprintf(` +resource "aws_pinpoint_app" "test" { + name = %[1]q + + campaign_hook {} +} +`, rName) +} + func testAccAppConfig_limits(rName string) string { return fmt.Sprintf(` resource "aws_pinpoint_app" "test" { @@ -430,6 +594,16 @@ resource "aws_pinpoint_app" "test" { `, rName) } +func testAccAppConfig_limitsEmpty(rName string) string { + return fmt.Sprintf(` +resource "aws_pinpoint_app" "test" { + name = %[1]q + + limits {} +} +`, rName) +} + func testAccAppConfig_quietTime(rName string) string { return fmt.Sprintf(` resource "aws_pinpoint_app" "test" { @@ -442,3 +616,13 @@ resource "aws_pinpoint_app" "test" { } `, rName) } + +func testAccAppConfig_quietTimeEmpty(rName string) string { + return fmt.Sprintf(` +resource "aws_pinpoint_app" "test" { + name = %[1]q + + quiet_time {} +} +`, rName) +}