|
| 1 | +package presenters |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + "gorm.io/datatypes" |
| 9 | + |
| 10 | + "github.com/openshift-hyperfleet/hyperfleet-api/pkg/api" |
| 11 | + "github.com/openshift-hyperfleet/hyperfleet-api/pkg/api/openapi" |
| 12 | +) |
| 13 | + |
| 14 | +func TestConvertResource(t *testing.T) { |
| 15 | + RegisterTestingT(t) |
| 16 | + |
| 17 | + labels := map[string]string{"env": "prod"} |
| 18 | + req := &openapi.ResourceCreateRequest{ |
| 19 | + Kind: "Channel", |
| 20 | + Name: "stable", |
| 21 | + Spec: map[string]any{"is_default": true, "enabled_regex": "4\\.17\\..*"}, |
| 22 | + Labels: &labels, |
| 23 | + } |
| 24 | + |
| 25 | + resource, err := ConvertResource(req) |
| 26 | + Expect(err).NotTo(HaveOccurred()) |
| 27 | + Expect(resource.Kind).To(Equal("Channel")) |
| 28 | + Expect(resource.Name).To(Equal("stable")) |
| 29 | + Expect(resource.Spec).NotTo(BeEmpty()) |
| 30 | + Expect(string(resource.Labels)).To(ContainSubstring("env")) |
| 31 | +} |
| 32 | + |
| 33 | +func TestConvertResource_NilLabels(t *testing.T) { |
| 34 | + RegisterTestingT(t) |
| 35 | + |
| 36 | + req := &openapi.ResourceCreateRequest{ |
| 37 | + Kind: "Channel", |
| 38 | + Name: "test", |
| 39 | + Spec: map[string]any{"key": "value"}, |
| 40 | + } |
| 41 | + |
| 42 | + resource, err := ConvertResource(req) |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + Expect(string(resource.Labels)).To(Equal("{}")) |
| 45 | +} |
| 46 | + |
| 47 | +func TestConvertResourceWithOwner(t *testing.T) { |
| 48 | + RegisterTestingT(t) |
| 49 | + |
| 50 | + req := &openapi.ResourceCreateRequest{ |
| 51 | + Kind: "Version", |
| 52 | + Name: "4-17-3", |
| 53 | + Spec: map[string]any{"raw_version": "4.17.3"}, |
| 54 | + } |
| 55 | + |
| 56 | + resource, err := ConvertResourceWithOwner( |
| 57 | + req, |
| 58 | + "parent-id", "Channel", "/api/hyperfleet/v1/channels/parent-id", |
| 59 | + ) |
| 60 | + Expect(err).NotTo(HaveOccurred()) |
| 61 | + Expect(*resource.OwnerID).To(Equal("parent-id")) |
| 62 | + Expect(*resource.OwnerKind).To(Equal("Channel")) |
| 63 | + Expect(*resource.OwnerHref).To(Equal("/api/hyperfleet/v1/channels/parent-id")) |
| 64 | +} |
| 65 | + |
| 66 | +func TestPresentResource(t *testing.T) { |
| 67 | + RegisterTestingT(t) |
| 68 | + |
| 69 | + now := time.Now() |
| 70 | + resource := &api.Resource{ |
| 71 | + Meta: api.Meta{ID: "test-id", CreatedTime: now, UpdatedTime: now}, |
| 72 | + Kind: "Channel", |
| 73 | + Name: "stable", |
| 74 | + Href: "/api/hyperfleet/v1/channels/test-id", |
| 75 | + Spec: datatypes.JSON(`{"is_default":true}`), |
| 76 | + Labels: datatypes.JSON(`{"env":"prod"}`), |
| 77 | + Generation: 1, |
| 78 | + CreatedBy: "user@test.com", |
| 79 | + UpdatedBy: "user@test.com", |
| 80 | + } |
| 81 | + |
| 82 | + resp := PresentResource(resource) |
| 83 | + Expect(resp.Id).To(Equal("test-id")) |
| 84 | + Expect(resp.Kind).To(Equal("Channel")) |
| 85 | + Expect(resp.Name).To(Equal("stable")) |
| 86 | + Expect(*resp.Href).To(Equal("/api/hyperfleet/v1/channels/test-id")) |
| 87 | + Expect(resp.Spec).To(HaveKeyWithValue("is_default", true)) |
| 88 | + Expect(*resp.Labels).To(HaveKeyWithValue("env", "prod")) |
| 89 | + Expect(resp.Generation).To(Equal(int32(1))) |
| 90 | + Expect(string(resp.CreatedBy)).To(Equal("user@test.com")) |
| 91 | + Expect(resp.OwnerReferences).To(BeNil()) |
| 92 | +} |
| 93 | + |
| 94 | +func TestPresentResource_WithOwner(t *testing.T) { |
| 95 | + RegisterTestingT(t) |
| 96 | + |
| 97 | + now := time.Now() |
| 98 | + ownerID := "parent-id" |
| 99 | + ownerKind := "Channel" |
| 100 | + ownerHref := "/api/hyperfleet/v1/channels/parent-id" |
| 101 | + resource := &api.Resource{ |
| 102 | + Meta: api.Meta{ID: "child-id", CreatedTime: now, UpdatedTime: now}, |
| 103 | + Kind: "Version", |
| 104 | + Name: "4-17-1", |
| 105 | + Spec: datatypes.JSON(`{}`), |
| 106 | + OwnerID: &ownerID, |
| 107 | + OwnerKind: &ownerKind, |
| 108 | + OwnerHref: &ownerHref, |
| 109 | + CreatedBy: "user@test.com", |
| 110 | + UpdatedBy: "user@test.com", |
| 111 | + } |
| 112 | + |
| 113 | + resp := PresentResource(resource) |
| 114 | + Expect(resp.OwnerReferences).NotTo(BeNil()) |
| 115 | + Expect(*resp.OwnerReferences.Id).To(Equal("parent-id")) |
| 116 | + Expect(*resp.OwnerReferences.Kind).To(Equal("Channel")) |
| 117 | +} |
| 118 | + |
| 119 | +func TestPresentResource_EmptySpec(t *testing.T) { |
| 120 | + RegisterTestingT(t) |
| 121 | + |
| 122 | + now := time.Now() |
| 123 | + resource := &api.Resource{ |
| 124 | + Meta: api.Meta{ID: "id", CreatedTime: now, UpdatedTime: now}, |
| 125 | + Kind: "Channel", |
| 126 | + Name: "test", |
| 127 | + Spec: datatypes.JSON(`{}`), |
| 128 | + CreatedBy: "user@test.com", |
| 129 | + UpdatedBy: "user@test.com", |
| 130 | + } |
| 131 | + |
| 132 | + resp := PresentResource(resource) |
| 133 | + Expect(resp.Spec).To(BeEmpty()) |
| 134 | +} |
| 135 | + |
| 136 | +func TestPresentResourceList(t *testing.T) { |
| 137 | + RegisterTestingT(t) |
| 138 | + |
| 139 | + now := time.Now() |
| 140 | + resources := api.ResourceList{ |
| 141 | + &api.Resource{ |
| 142 | + Meta: api.Meta{ID: "id1", CreatedTime: now, UpdatedTime: now}, |
| 143 | + Kind: "Channel", Name: "stable", |
| 144 | + Spec: datatypes.JSON(`{}`), CreatedBy: "u@t.com", UpdatedBy: "u@t.com", |
| 145 | + }, |
| 146 | + &api.Resource{ |
| 147 | + Meta: api.Meta{ID: "id2", CreatedTime: now, UpdatedTime: now}, |
| 148 | + Kind: "Channel", Name: "candidate", |
| 149 | + Spec: datatypes.JSON(`{}`), CreatedBy: "u@t.com", UpdatedBy: "u@t.com", |
| 150 | + }, |
| 151 | + } |
| 152 | + paging := &api.PagingMeta{Page: 1, Size: 2, Total: 2} |
| 153 | + |
| 154 | + result := PresentResourceList(resources, paging) |
| 155 | + Expect(result.Kind).To(Equal("ResourceList")) |
| 156 | + Expect(result.Items).To(HaveLen(2)) |
| 157 | + Expect(result.Page).To(Equal(int32(1))) |
| 158 | + Expect(result.Size).To(Equal(int32(2))) |
| 159 | + Expect(result.Total).To(Equal(int64(2))) |
| 160 | +} |
0 commit comments