-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create permission controller test
- Loading branch information
1 parent
7d662a7
commit 9c546c0
Showing
6 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/io/github/onecx/permission/operator/PermissionControllerResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.github.onecx.permission.operator; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.github.onecx.permission.operator.client.PermissionService; | ||
import io.github.onecx.permission.test.AbstractTest; | ||
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class PermissionControllerResponseTest extends AbstractTest { | ||
|
||
@InjectMock | ||
PermissionService productStoreService; | ||
|
||
@Inject | ||
PermissionController reconciler; | ||
|
||
@BeforeEach | ||
void beforeAll() { | ||
Mockito.when(productStoreService.updatePermission(any())).thenReturn(404); | ||
} | ||
|
||
@Test | ||
void testWrongResponse() throws Exception { | ||
|
||
var p1 = new PermissionSpec.PermissionItemSpec(); | ||
p1.setName("n2"); | ||
p1.setAction("a2"); | ||
p1.setDescription("d1"); | ||
p1.setResource("r1"); | ||
|
||
var s = new PermissionSpec(); | ||
s.setAppId("test-3"); | ||
s.setPermissions(List.of(p1)); | ||
|
||
Permission m = new Permission(); | ||
m.setSpec(s); | ||
|
||
UpdateControl<Permission> result = reconciler.reconcile(m, null); | ||
assertThat(result).isNotNull(); | ||
assertThat(result.getResource()).isNotNull(); | ||
assertThat(result.getResource().getStatus()).isNotNull(); | ||
assertThat(result.getResource().getStatus().getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.UNDEFINED); | ||
|
||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
src/test/java/io/github/onecx/permission/operator/PermissionControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package io.github.onecx.permission.operator; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.github.onecx.permission.test.AbstractTest; | ||
import io.javaoperatorsdk.operator.Operator; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class PermissionControllerTest extends AbstractTest { | ||
|
||
final static Logger log = LoggerFactory.getLogger(PermissionControllerTest.class); | ||
|
||
@Inject | ||
Operator operator; | ||
|
||
@Inject | ||
KubernetesClient client; | ||
|
||
@BeforeAll | ||
public static void init() { | ||
Awaitility.setDefaultPollDelay(2, SECONDS); | ||
Awaitility.setDefaultPollInterval(2, SECONDS); | ||
Awaitility.setDefaultTimeout(10, SECONDS); | ||
} | ||
|
||
@Test | ||
void productEmptySpecTest() { | ||
|
||
operator.start(); | ||
|
||
Permission data = new Permission(); | ||
data.setMetadata(new ObjectMetaBuilder().withName("empty-spec").withNamespace(client.getNamespace()).build()); | ||
data.setSpec(new PermissionSpec()); | ||
|
||
log.info("Creating test permission object: {}", data); | ||
client.resource(data).serverSideApply(); | ||
|
||
log.info("Waiting 4 seconds and status muss be still null"); | ||
|
||
await().pollDelay(2, SECONDS).untilAsserted(() -> { | ||
PermissionStatus mfeStatus = client.resource(data).get().getStatus(); | ||
assertThat(mfeStatus).isNotNull(); | ||
assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.ERROR); | ||
}); | ||
} | ||
|
||
@Test | ||
void productNullSpecTest() { | ||
|
||
operator.start(); | ||
|
||
Permission data = new Permission(); | ||
data.setMetadata(new ObjectMetaBuilder().withName("null-spec").withNamespace(client.getNamespace()).build()); | ||
data.setSpec(null); | ||
|
||
log.info("Creating test permission object: {}", data); | ||
client.resource(data).serverSideApply(); | ||
|
||
log.info("Waiting 4 seconds and status muss be still null"); | ||
|
||
await().pollDelay(4, SECONDS).untilAsserted(() -> { | ||
PermissionStatus mfeStatus = client.resource(data).get().getStatus(); | ||
assertThat(mfeStatus).isNull(); | ||
}); | ||
|
||
} | ||
|
||
@Test | ||
void productUpdateEmptySpecTest() { | ||
|
||
operator.start(); | ||
|
||
var p1 = new PermissionSpec.PermissionItemSpec(); | ||
p1.setName("n2"); | ||
p1.setAction("a2"); | ||
p1.setDescription("d1"); | ||
p1.setResource("r1"); | ||
|
||
var m = new PermissionSpec(); | ||
m.setAppId("test-3"); | ||
m.setPermissions(List.of(p1)); | ||
|
||
var data = new Permission(); | ||
data | ||
.setMetadata(new ObjectMetaBuilder().withName("to-update-spec").withNamespace(client.getNamespace()).build()); | ||
data.setSpec(m); | ||
|
||
log.info("Creating test permission object: {}", data); | ||
client.resource(data).serverSideApply(); | ||
|
||
log.info("Waiting 4 seconds and status muss be still null"); | ||
|
||
await().pollDelay(2, SECONDS).untilAsserted(() -> { | ||
PermissionStatus mfeStatus = client.resource(data).get().getStatus(); | ||
assertThat(mfeStatus).isNotNull(); | ||
assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.UPDATED); | ||
}); | ||
|
||
client.resource(data).inNamespace(client.getNamespace()) | ||
.edit(s -> { | ||
s.setSpec(null); | ||
return s; | ||
}); | ||
|
||
await().pollDelay(4, SECONDS).untilAsserted(() -> { | ||
PermissionStatus mfeStatus = client.resource(data).get().getStatus(); | ||
assertThat(mfeStatus).isNotNull(); | ||
assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.UPDATED); | ||
}); | ||
} | ||
|
||
@Test | ||
void productRestClientExceptionTest() { | ||
|
||
operator.start(); | ||
|
||
var p1 = new PermissionSpec.PermissionItemSpec(); | ||
p1.setName("n2"); | ||
p1.setAction("a2"); | ||
p1.setDescription("d1"); | ||
p1.setResource("r1"); | ||
|
||
var m = new PermissionSpec(); | ||
m.setAppId("test-error-1"); | ||
m.setPermissions(List.of(p1)); | ||
|
||
var data = new Permission(); | ||
data | ||
.setMetadata(new ObjectMetaBuilder().withName("client-error").withNamespace(client.getNamespace()).build()); | ||
data.setSpec(m); | ||
|
||
log.info("Creating test permission object: {}", data); | ||
client.resource(data).serverSideApply(); | ||
|
||
log.info("Waiting 4 seconds and status muss be still null"); | ||
|
||
await().pollDelay(2, SECONDS).untilAsserted(() -> { | ||
PermissionStatus mfeStatus = client.resource(data).get().getStatus(); | ||
assertThat(mfeStatus).isNotNull(); | ||
assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.ERROR); | ||
}); | ||
|
||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/test/java/io/github/onecx/permission/test/AbstractTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.github.onecx.permission.test; | ||
|
||
public abstract class AbstractTest { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters