|
| 1 | +// Package unit provides unit tests for ZPA services |
| 2 | +package unit |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/zscaler/zscaler-sdk-go/v3/tests/unit/common" |
| 12 | + "github.com/zscaler/zscaler-sdk-go/v3/zscaler/zpa/services/tag_controller/tag_group" |
| 13 | +) |
| 14 | + |
| 15 | +func TestTagGroup_Get_SDK(t *testing.T) { |
| 16 | + server := common.NewTestServer() |
| 17 | + defer server.Close() |
| 18 | + |
| 19 | + tagGroupID := "tg-12345" |
| 20 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/" + tagGroupID |
| 21 | + |
| 22 | + server.On("GET", path, common.SuccessResponse(tag_group.TagGroup{ |
| 23 | + ID: tagGroupID, |
| 24 | + Name: "Test Tag Group", |
| 25 | + Description: "Test description", |
| 26 | + })) |
| 27 | + |
| 28 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + result, resp, err := tag_group.Get(context.Background(), service, tagGroupID) |
| 32 | + |
| 33 | + require.NoError(t, err) |
| 34 | + require.NotNil(t, result) |
| 35 | + assert.NotNil(t, resp) |
| 36 | + assert.Equal(t, tagGroupID, result.ID) |
| 37 | + assert.Equal(t, "Test Tag Group", result.Name) |
| 38 | +} |
| 39 | + |
| 40 | +func TestTagGroup_GetByName_SDK(t *testing.T) { |
| 41 | + server := common.NewTestServer() |
| 42 | + defer server.Close() |
| 43 | + |
| 44 | + tagGroupName := "Production Tag Group" |
| 45 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/search" |
| 46 | + |
| 47 | + server.On("POST", path, common.SuccessResponse(map[string]interface{}{ |
| 48 | + "list": []tag_group.TagGroup{ |
| 49 | + {ID: "tg-001", Name: "Other Group"}, |
| 50 | + {ID: "tg-002", Name: tagGroupName}, |
| 51 | + }, |
| 52 | + "totalPages": 1, |
| 53 | + })) |
| 54 | + |
| 55 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + result, _, err := tag_group.GetByName(context.Background(), service, tagGroupName) |
| 59 | + |
| 60 | + require.NoError(t, err) |
| 61 | + require.NotNil(t, result) |
| 62 | + assert.Equal(t, "tg-002", result.ID) |
| 63 | + assert.Equal(t, tagGroupName, result.Name) |
| 64 | +} |
| 65 | + |
| 66 | +func TestTagGroup_Create_SDK(t *testing.T) { |
| 67 | + server := common.NewTestServer() |
| 68 | + defer server.Close() |
| 69 | + |
| 70 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup" |
| 71 | + |
| 72 | + server.On("POST", path, common.SuccessResponse(tag_group.TagGroup{ |
| 73 | + ID: "new-tg-123", |
| 74 | + Name: "New Tag Group", |
| 75 | + Description: "Created via unit test", |
| 76 | + })) |
| 77 | + |
| 78 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + result, _, err := tag_group.Create(context.Background(), service, tag_group.TagGroup{ |
| 82 | + Name: "New Tag Group", |
| 83 | + Description: "Created via unit test", |
| 84 | + }) |
| 85 | + |
| 86 | + require.NoError(t, err) |
| 87 | + require.NotNil(t, result) |
| 88 | + assert.Equal(t, "new-tg-123", result.ID) |
| 89 | + assert.Equal(t, "New Tag Group", result.Name) |
| 90 | +} |
| 91 | + |
| 92 | +func TestTagGroup_Update_SDK(t *testing.T) { |
| 93 | + server := common.NewTestServer() |
| 94 | + defer server.Close() |
| 95 | + |
| 96 | + tagGroupID := "tg-12345" |
| 97 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/" + tagGroupID |
| 98 | + |
| 99 | + server.On("PUT", path, common.NoContentResponse()) |
| 100 | + |
| 101 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + resp, err := tag_group.Update(context.Background(), service, tagGroupID, &tag_group.TagGroup{ |
| 105 | + ID: tagGroupID, |
| 106 | + Name: "Updated Tag Group", |
| 107 | + Description: "Updated description", |
| 108 | + }) |
| 109 | + |
| 110 | + require.NoError(t, err) |
| 111 | + assert.NotNil(t, resp) |
| 112 | +} |
| 113 | + |
| 114 | +func TestTagGroup_Delete_SDK(t *testing.T) { |
| 115 | + server := common.NewTestServer() |
| 116 | + defer server.Close() |
| 117 | + |
| 118 | + tagGroupID := "tg-12345" |
| 119 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/" + tagGroupID |
| 120 | + |
| 121 | + server.On("DELETE", path, common.NoContentResponse()) |
| 122 | + |
| 123 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + resp, err := tag_group.Delete(context.Background(), service, tagGroupID) |
| 127 | + |
| 128 | + require.NoError(t, err) |
| 129 | + assert.NotNil(t, resp) |
| 130 | +} |
| 131 | + |
| 132 | +func TestTagGroup_GetAll_SDK(t *testing.T) { |
| 133 | + server := common.NewTestServer() |
| 134 | + defer server.Close() |
| 135 | + |
| 136 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/search" |
| 137 | + |
| 138 | + server.On("POST", path, common.SuccessResponse(map[string]interface{}{ |
| 139 | + "list": []tag_group.TagGroup{ |
| 140 | + {ID: "tg-001", Name: "Group 1"}, |
| 141 | + {ID: "tg-002", Name: "Group 2"}, |
| 142 | + {ID: "tg-003", Name: "Group 3"}, |
| 143 | + }, |
| 144 | + "totalPages": 1, |
| 145 | + })) |
| 146 | + |
| 147 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 148 | + require.NoError(t, err) |
| 149 | + |
| 150 | + result, _, err := tag_group.GetAll(context.Background(), service) |
| 151 | + |
| 152 | + require.NoError(t, err) |
| 153 | + require.NotNil(t, result) |
| 154 | + assert.Len(t, result, 3) |
| 155 | +} |
| 156 | + |
| 157 | +func TestTagGroup_GetByName_NotFound_SDK(t *testing.T) { |
| 158 | + server := common.NewTestServer() |
| 159 | + defer server.Close() |
| 160 | + |
| 161 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/search" |
| 162 | + |
| 163 | + server.On("POST", path, common.SuccessResponse(map[string]interface{}{ |
| 164 | + "list": []tag_group.TagGroup{}, |
| 165 | + "totalPages": 0, |
| 166 | + })) |
| 167 | + |
| 168 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 169 | + require.NoError(t, err) |
| 170 | + |
| 171 | + result, _, err := tag_group.GetByName(context.Background(), service, "NonExistent") |
| 172 | + |
| 173 | + assert.Error(t, err) |
| 174 | + assert.Nil(t, result) |
| 175 | + assert.Contains(t, err.Error(), "no tag group named") |
| 176 | +} |
| 177 | + |
| 178 | +func TestTagGroup_Get_NotFound_SDK(t *testing.T) { |
| 179 | + server := common.NewTestServer() |
| 180 | + defer server.Close() |
| 181 | + |
| 182 | + tagGroupID := "nonexistent-id" |
| 183 | + path := "/zpa/mgmtconfig/v1/admin/customers/" + testCustomerID + "/tagGroup/" + tagGroupID |
| 184 | + |
| 185 | + server.On("GET", path, common.MockResponse{ |
| 186 | + StatusCode: http.StatusNotFound, |
| 187 | + Body: `{"id": "resource.not.found", "message": "Resource not found"}`, |
| 188 | + }) |
| 189 | + |
| 190 | + service, err := common.CreateTestService(context.Background(), server, testCustomerID) |
| 191 | + require.NoError(t, err) |
| 192 | + |
| 193 | + result, _, err := tag_group.Get(context.Background(), service, tagGroupID) |
| 194 | + |
| 195 | + assert.Error(t, err) |
| 196 | + assert.Nil(t, result) |
| 197 | +} |
0 commit comments