From f47b942432ee972b6bb76da77194e56214752ad5 Mon Sep 17 00:00:00 2001
From: cgeorgilakis-grnet <cgeorgilakis@admin.grnet.gr>
Date: Thu, 22 May 2025 14:41:57 +0300
Subject: [PATCH] fixes related to DomainGroup, add tests for DomainGroup
---
build.gradle | 1 -
.../portal/persistent/entity/DomainGroup.java | 2 +-
.../service/impl/DomainGroupServiceTest.java | 144 ++++++++++++++++++
.../service/impl/DomainServiceTest.java | 5 +-
4 files changed, 146 insertions(+), 6 deletions(-)
create mode 100644 src/test/java/net/geant/nmaas/portal/service/impl/DomainGroupServiceTest.java
diff --git a/build.gradle b/build.gradle
index 796507dc6..574373708 100644
--- a/build.gradle
+++ b/build.gradle
@@ -88,7 +88,6 @@ dependencies {
implementation('org.springframework.boot:spring-boot-starter-mail')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-actuator')
-// implementation('org.springframework.boot:spring-boot-devtools')
implementation('org.springframework.boot:spring-boot-starter-quartz')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation 'org.springframework.boot:spring-boot-starter-cache'
diff --git a/src/main/java/net/geant/nmaas/portal/persistent/entity/DomainGroup.java b/src/main/java/net/geant/nmaas/portal/persistent/entity/DomainGroup.java
index 186f96dcf..74d554474 100644
--- a/src/main/java/net/geant/nmaas/portal/persistent/entity/DomainGroup.java
+++ b/src/main/java/net/geant/nmaas/portal/persistent/entity/DomainGroup.java
@@ -67,7 +67,7 @@ public class DomainGroup implements Serializable {
public DomainGroup(String name, String codename) {
super();
this.name = name;
- this.codename = name;
+ this.codename = codename;
}
public DomainGroup(Long id, String name, String codename) {
diff --git a/src/test/java/net/geant/nmaas/portal/service/impl/DomainGroupServiceTest.java b/src/test/java/net/geant/nmaas/portal/service/impl/DomainGroupServiceTest.java
new file mode 100644
index 000000000..2b1c86427
--- /dev/null
+++ b/src/test/java/net/geant/nmaas/portal/service/impl/DomainGroupServiceTest.java
@@ -0,0 +1,144 @@
+package net.geant.nmaas.portal.service.impl;
+
+import net.geant.nmaas.orchestration.jobs.DomainGroupJob;
+import net.geant.nmaas.portal.api.domain.DomainGroupView;
+import net.geant.nmaas.portal.persistent.entity.DomainGroup;
+import net.geant.nmaas.portal.persistent.entity.WebhookEvent;
+import net.geant.nmaas.portal.persistent.entity.WebhookEventType;
+import net.geant.nmaas.portal.persistent.repositories.DomainGroupRepository;
+import net.geant.nmaas.portal.persistent.repositories.WebhookEventRepository;
+import net.geant.nmaas.portal.service.ApplicationStatePerDomainService;
+import net.geant.nmaas.portal.service.DomainGroupService;
+import net.geant.nmaas.scheduling.ScheduleManager;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.modelmapper.ModelMapper;
+import org.quartz.JobListener;
+import org.quartz.ListenerManager;
+import org.quartz.Matcher;
+import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
+
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DomainGroupServiceTest {
+
+ DomainGroupRepository domainGroupRepository = mock(DomainGroupRepository.class);
+ ApplicationStatePerDomainService applicationStatePerDomainService = mock(ApplicationStatePerDomainService.class);
+ WebhookEventRepository webhookEventRepository = mock(WebhookEventRepository.class);
+ Scheduler scheduler = mock(Scheduler.class);
+ ListenerManager listenerManager = mock(ListenerManager.class);
+ ScheduleManager scheduleManager;
+ ModelMapper modelMapper = new ModelMapper();
+ DomainGroupService domainGroupService;
+
+ @BeforeEach
+ void setup() {
+ scheduleManager = new ScheduleManager(scheduler);
+ domainGroupService = new DomainGroupServiceImpl(domainGroupRepository, applicationStatePerDomainService, webhookEventRepository, scheduleManager, modelMapper);
+ }
+
+ @Test
+ void shouldCreateDomainGroup() throws SchedulerException {
+ // Setup webhook event
+ WebhookEvent webhookEvent = new WebhookEvent(1L, "webhook", "https://example.com/webhook", WebhookEventType.DOMAIN_GROUP_CHANGE, null, null);
+ when(webhookEventRepository.findIdByEventType(WebhookEventType.DOMAIN_GROUP_CHANGE))
+ .thenReturn(Stream.of(1L));
+ when(webhookEventRepository.findById(1L))
+ .thenReturn(Optional.of(webhookEvent));
+
+ // Setup domain group
+ String name = "testgroup";
+ String codename = "testgrp";
+ DomainGroup domainGroup = new DomainGroup(name, codename);
+ domainGroup.setId(10L);
+ when(domainGroupRepository.save(any(DomainGroup.class))).thenReturn(domainGroup);
+ when(domainGroupRepository.findById(10L)).thenReturn(Optional.of(domainGroup));
+ when(scheduler.getListenerManager()).thenReturn(listenerManager);
+ doNothing().when(listenerManager).addJobListener(any(JobListener.class), any(Matcher.class));
+
+ // Create domain group
+ DomainGroupView domainGroupView = new DomainGroupView();
+ domainGroupView.setName(name);
+ domainGroupView.setCodename(codename);
+ DomainGroupView result = this.domainGroupService.createDomainGroup(domainGroupView);
+
+ // Verify webhook job was scheduled with correct parameters for creation
+ verify(scheduler, times(1)).scheduleJob(
+ argThat(jobDetail ->
+ jobDetail.getKey().getName().startsWith("DomainGroup_1_10_") &&
+ jobDetail.getJobClass().equals(DomainGroupJob.class)
+ ),
+ argThat(trigger ->
+ trigger.getKey().getName().startsWith("DomainGroup_1_10_")
+ )
+ );
+
+ // Verify domain group was created correctly
+ assertThat("Codenames are not the same", result.getCodename().equals(codename));
+ assertThat("Names are not the same", result.getName().equals(name));
+
+ // Update domain group
+ when(webhookEventRepository.findIdByEventType(WebhookEventType.DOMAIN_GROUP_CHANGE))
+ .thenReturn(Stream.of(1L));
+ when(webhookEventRepository.findById(1L))
+ .thenReturn(Optional.of(webhookEvent));
+ domainGroupView.setCodename(codename + "2");
+ domainGroupView.setId(10L);
+ result = this.domainGroupService.updateDomainGroup(10L, domainGroupView);
+
+ // Verify webhook job was scheduled with correct parameters for update
+ verify(scheduler, times(2)).scheduleJob(
+ argThat(jobDetail ->
+ jobDetail.getKey().getName().startsWith("DomainGroup_1_10_") &&
+ jobDetail.getJobClass().equals(DomainGroupJob.class)
+ ),
+ argThat(trigger ->
+ trigger.getKey().getName().startsWith("DomainGroup_1_10_")
+ )
+ );
+
+ // Verify domain group was updated correctly
+ assertThat("Updated codenames are not the same", result.getCodename().equals(codename + "2"));
+ assertThat("Names are not the same after update", result.getName().equals(name));
+ }
+
+ @Test
+ void shouldDeleteDomainGroup() throws SchedulerException {
+ // Setup webhook event
+ WebhookEvent webhookEvent = new WebhookEvent(1L, "webhook", "https://example.com/webhook", WebhookEventType.DOMAIN_GROUP_CHANGE, null, null);
+ when(webhookEventRepository.findIdByEventType(WebhookEventType.DOMAIN_GROUP_CHANGE))
+ .thenReturn(Stream.of(1L));
+ when(webhookEventRepository.findById(1L))
+ .thenReturn(Optional.of(webhookEvent));
+
+ DomainGroup domainGroup = new DomainGroup("testgroup", "testgrp");
+ domainGroup.setId(10L);
+ when(domainGroupRepository.findById(10L)).thenReturn(Optional.of(domainGroup));
+ when(scheduler.getListenerManager()).thenReturn(listenerManager);
+ doNothing().when(listenerManager).addJobListener(any(JobListener.class), any(Matcher.class));
+ this.domainGroupService.deleteDomainGroup(10L);
+ verify(domainGroupRepository, times(1)).deleteById(10L);
+
+ verify(scheduler, times(1)).scheduleJob(
+ argThat(jobDetail ->
+ jobDetail.getKey().getName().startsWith("DomainGroup_1_10_") &&
+ jobDetail.getJobClass().equals(DomainGroupJob.class)
+ ),
+ argThat(trigger ->
+ trigger.getKey().getName().startsWith("DomainGroup_1_10_")
+ )
+ );
+ }
+}
diff --git a/src/test/java/net/geant/nmaas/portal/service/impl/DomainServiceTest.java b/src/test/java/net/geant/nmaas/portal/service/impl/DomainServiceTest.java
index ccbc33590..5fb8cb4c8 100644
--- a/src/test/java/net/geant/nmaas/portal/service/impl/DomainServiceTest.java
+++ b/src/test/java/net/geant/nmaas/portal/service/impl/DomainServiceTest.java
@@ -94,8 +94,6 @@ class DomainServiceTest {
Scheduler scheduler = mock(Scheduler.class);
ListenerManager listenerManager = mock(ListenerManager.class);
ScheduleManager scheduleManager;
- EncryptionService encryptionService = mock(EncryptionService.class);
- WebhookEventService webhookEventService;
DomainService domainService;
@@ -103,15 +101,14 @@ class DomainServiceTest {
void setup() {
validator = new DefaultCodenameValidator("[a-z-]{2,12}");
namespaceValidator = new DefaultCodenameValidator("[a-z-]{0,64}");
- domainGroupService = new DomainGroupServiceImpl(domainGroupRepository, applicationStatePerDomainService, webhookEventRepository, scheduleManager, modelMapper);
scheduleManager = new ScheduleManager( scheduler);
+ domainGroupService = new DomainGroupServiceImpl(domainGroupRepository, applicationStatePerDomainService, webhookEventRepository, scheduleManager, modelMapper);
domainService = new DomainServiceImpl(validator,
namespaceValidator, domainRepository,
domainDcnDetailsRepository, domainTechDetailsRepository, userService,
userRoleRepo, dcnRepositoryManager,
modelMapper, applicationStatePerDomainService, domainGroupService, eventPublisher, domainAnnotationsRepository, webhookEventRepository, scheduleManager);
((DomainServiceImpl) domainService).globalDomain = "GLOBAL";
- webhookEventService = new WebhookEventService(webhookEventRepository, encryptionService, modelMapper);
}
@Test
--
GitLab