Skip to content

Commit 358aee1

Browse files
committed
implement RoleCreate and update the related tests
1 parent a5d3081 commit 358aee1

5 files changed

Lines changed: 295 additions & 168 deletions

File tree

src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/Given.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.fabric8.kubernetes.client.KubernetesClient;
44
import it.aboutbits.postgresql._support.testdata.persisted.creator.ClusterConnectionCreate;
5+
import it.aboutbits.postgresql._support.testdata.persisted.creator.RoleCreate;
56
import it.aboutbits.postgresql._support.testdata.persisted.creator.SecretRefCreate;
67
import jakarta.enterprise.context.ApplicationScoped;
78
import lombok.AccessLevel;
@@ -82,6 +83,14 @@ public ClusterConnectionCreate clusterConnection() {
8283
dbConnectionDetails()
8384
);
8485
}
86+
87+
public RoleCreate role() {
88+
return new RoleCreate(
89+
numberOfItems,
90+
given,
91+
kubernetesClient
92+
);
93+
}
8594
}
8695

8796
public record DBConnectionDetails(

src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/ClusterConnectionCreate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.concurrent.TimeUnit;
2020

2121
@NullMarked
22-
@Getter
2322
@Setter
2423
@Accessors(fluent = true, chain = true)
2524
public class ClusterConnectionCreate extends TestDataCreator<ClusterConnection> {
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package it.aboutbits.postgresql._support.testdata.persisted.creator;
2+
3+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
4+
import io.fabric8.kubernetes.client.KubernetesClient;
5+
import it.aboutbits.postgresql._support.testdata.base.TestDataCreator;
6+
import it.aboutbits.postgresql._support.testdata.persisted.Given;
7+
import it.aboutbits.postgresql.core.ClusterReference;
8+
import it.aboutbits.postgresql.core.SecretRef;
9+
import it.aboutbits.postgresql.crd.role.Role;
10+
import it.aboutbits.postgresql.crd.role.RoleSpec;
11+
import lombok.AccessLevel;
12+
import lombok.Getter;
13+
import lombok.Setter;
14+
import lombok.experimental.Accessors;
15+
import org.jspecify.annotations.NullMarked;
16+
import org.jspecify.annotations.Nullable;
17+
18+
import java.util.Objects;
19+
import java.util.concurrent.TimeUnit;
20+
21+
@NullMarked
22+
@Setter
23+
@Accessors(fluent = true, chain = true)
24+
public class RoleCreate extends TestDataCreator<Role> {
25+
private final Given given;
26+
27+
private final KubernetesClient kubernetesClient;
28+
29+
@Nullable
30+
private String withNamespace;
31+
@Setter(AccessLevel.NONE)
32+
private boolean withoutNamespace = false;
33+
34+
@Nullable
35+
private String withName;
36+
37+
@Nullable
38+
private String withComment;
39+
40+
@Nullable
41+
private String withClusterConnectionName;
42+
43+
@Nullable
44+
private String withClusterConnectionNamespace;
45+
46+
@Nullable
47+
private SecretRef withPasswordSecretRef;
48+
49+
private RoleSpec.@Nullable Flags withFlags;
50+
51+
public RoleCreate withLogin(boolean login) {
52+
if (!login) {
53+
withPasswordSecretRef = null;
54+
return this;
55+
}
56+
57+
if (withPasswordSecretRef != null) {
58+
return this;
59+
}
60+
61+
withPasswordSecretRef = given.one()
62+
.secretRef()
63+
.returnFirst();
64+
65+
return this;
66+
}
67+
68+
public RoleCreate withoutNamespace() {
69+
withoutNamespace = true;
70+
return this;
71+
}
72+
73+
public RoleCreate(
74+
int numberOfItems,
75+
Given given,
76+
KubernetesClient kubernetesClient
77+
) {
78+
super(numberOfItems);
79+
this.given = given;
80+
this.kubernetesClient = kubernetesClient;
81+
}
82+
83+
@Override
84+
protected Role create(int index) {
85+
var namespace = getNamespace();
86+
var name = getName();
87+
88+
var item = new Role();
89+
90+
item.setMetadata(new ObjectMetaBuilder()
91+
.withName(name)
92+
.withNamespace(namespace)
93+
.build()
94+
);
95+
96+
var spec = new RoleSpec();
97+
spec.setName(name);
98+
spec.setComment(withComment);
99+
100+
var clusterRef = new ClusterReference();
101+
clusterRef.setName(getClusterConnectionName());
102+
clusterRef.setNamespace(withClusterConnectionNamespace);
103+
spec.setClusterRef(clusterRef);
104+
105+
spec.setPasswordSecretRef(withPasswordSecretRef);
106+
107+
if (withFlags != null) {
108+
spec.setFlags(withFlags);
109+
}
110+
111+
item.setSpec(spec);
112+
113+
kubernetesClient.resources(Role.class)
114+
.inNamespace(namespace)
115+
.resource(item)
116+
.serverSideApply();
117+
118+
//noinspection ConstantConditions
119+
return kubernetesClient.resources(Role.class)
120+
.inNamespace(namespace)
121+
.withName(name)
122+
.waitUntilCondition(
123+
role -> role.getStatus() != null,
124+
10,
125+
TimeUnit.SECONDS
126+
);
127+
}
128+
129+
@Nullable
130+
private String getNamespace() {
131+
if (withoutNamespace) {
132+
return null;
133+
}
134+
135+
if (withNamespace != null) {
136+
return withNamespace;
137+
}
138+
139+
return kubernetesClient.getNamespace();
140+
}
141+
142+
private String getName() {
143+
if (withName != null) {
144+
return withName;
145+
}
146+
147+
return randomKubernetesNameSuffix("test-role");
148+
}
149+
150+
private String getClusterConnectionName() {
151+
return Objects.requireNonNullElse(
152+
withClusterConnectionName,
153+
"test-cluster-connection"
154+
);
155+
}
156+
}

src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/SecretRefCreate.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import io.fabric8.kubernetes.client.KubernetesClient;
55
import it.aboutbits.postgresql._support.testdata.base.TestDataCreator;
66
import it.aboutbits.postgresql.core.SecretRef;
7+
import lombok.AccessLevel;
8+
import lombok.Setter;
9+
import lombok.experimental.Accessors;
710
import org.jspecify.annotations.NullMarked;
811
import org.jspecify.annotations.Nullable;
912

@@ -12,22 +15,27 @@
1215
import static it.aboutbits.postgresql.core.KubernetesService.SECRET_TYPE_BASIC_AUTH;
1316

1417
@NullMarked
18+
@Setter
19+
@Accessors(fluent = true, chain = true)
1520
public class SecretRefCreate extends TestDataCreator<SecretRef> {
1621
private final KubernetesClient kubernetesClient;
1722

1823
@Nullable
1924
private String withNamespace;
25+
@Setter(AccessLevel.NONE)
2026
private boolean withoutNamespace = false;
2127

2228
@Nullable
2329
private String withName;
2430

2531
@Nullable
2632
private String withUsername;
33+
@Setter(AccessLevel.NONE)
2734
private boolean withoutUsername = false;
2835

2936
@Nullable
3037
private String withPassword;
38+
@Setter(AccessLevel.NONE)
3139
private boolean withoutPassword = false;
3240

3341
public SecretRefCreate(
@@ -38,42 +46,18 @@ public SecretRefCreate(
3846
this.kubernetesClient = kubernetesClient;
3947
}
4048

41-
@SuppressWarnings("unused")
42-
public SecretRefCreate withNamespace(String namespace) {
43-
withNamespace = namespace;
44-
return this;
45-
}
46-
4749
@SuppressWarnings("unused")
4850
public SecretRefCreate withoutNamespace() {
4951
withoutNamespace = true;
5052
return this;
5153
}
5254

53-
@SuppressWarnings("unused")
54-
public SecretRefCreate withName(String name) {
55-
withName = name;
56-
return this;
57-
}
58-
59-
@SuppressWarnings("unused")
60-
public SecretRefCreate withUsername(String username) {
61-
withUsername = username;
62-
return this;
63-
}
64-
6555
@SuppressWarnings("unused")
6656
public SecretRefCreate withoutUsername() {
6757
withoutUsername = true;
6858
return this;
6959
}
7060

71-
@SuppressWarnings("unused")
72-
public SecretRefCreate withPassword(String password) {
73-
withPassword = password;
74-
return this;
75-
}
76-
7761
@SuppressWarnings("unused")
7862
public SecretRefCreate withoutPassword() {
7963
withoutPassword = true;

0 commit comments

Comments
 (0)