diff --git a/src/main/java/fr/icdc/ebad/config/Constants.java b/src/main/java/fr/icdc/ebad/config/Constants.java index 4cf65a31..dd7cf77e 100644 --- a/src/main/java/fr/icdc/ebad/config/Constants.java +++ b/src/main/java/fr/icdc/ebad/config/Constants.java @@ -12,6 +12,13 @@ public final class Constants { public static final String ROLE_ADMIN = "ROLE_ADMIN"; public static final String ROLE_USER = "ROLE_USER"; + + public static final String GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID = "DEFAULT_IDENTITY_ID"; + public static final String GLOBAL_SETTINGS_ENVIRONMENT_IMPORT_ENABLED = "ENVIRONMENT_IMPORT_ENABLED"; + public static final String GLOBAL_SETTINGS_APPLICATION_IMPORT_ENABLED = "APPLICATION_IMPORT_ENABLED"; + public static final String GLOBAL_SETTINGS_ENVIRONMENT_CREATE_ENABLED = "ENVIRONMENT_CREATE_ENABLED"; + public static final String GLOBAL_SETTINGS_APPLICATION_CREATE_ENABLED = "APPLICATION_CREATE_ENABLED"; + private Constants() { } diff --git a/src/main/java/fr/icdc/ebad/security/permission/PermissionServiceOpen.java b/src/main/java/fr/icdc/ebad/security/permission/PermissionServiceOpen.java index 8e062685..f84b5947 100644 --- a/src/main/java/fr/icdc/ebad/security/permission/PermissionServiceOpen.java +++ b/src/main/java/fr/icdc/ebad/security/permission/PermissionServiceOpen.java @@ -10,6 +10,8 @@ import java.util.Arrays; +import static fr.icdc.ebad.config.Constants.*; + /** * Determine if a service is opened or closed */ @@ -25,19 +27,19 @@ public PermissionServiceOpen(GlobalSettingService globalSettingService, Environm } public boolean canImportEnvironment() { - return getServiceOpen("ENVIRONMENT_IMPORT_ENABLED"); + return getServiceOpen(GLOBAL_SETTINGS_ENVIRONMENT_IMPORT_ENABLED); } public boolean canImportApplication() { - return getServiceOpen("APPLICATION_IMPORT_ENABLED"); + return getServiceOpen(GLOBAL_SETTINGS_APPLICATION_IMPORT_ENABLED); } public boolean canCreateEnvironment() { - return getServiceOpen("ENVIRONMENT_CREATE_ENABLED"); + return getServiceOpen(GLOBAL_SETTINGS_ENVIRONMENT_CREATE_ENABLED); } public boolean canCreateApplication() { - return getServiceOpen("APPLICATION_CREATE_ENABLED"); + return getServiceOpen(GLOBAL_SETTINGS_APPLICATION_CREATE_ENABLED); } public boolean canCreateOrUpdateUser() { diff --git a/src/main/java/fr/icdc/ebad/service/EnvironnementService.java b/src/main/java/fr/icdc/ebad/service/EnvironnementService.java index 5dbec2d3..4b8f159a 100644 --- a/src/main/java/fr/icdc/ebad/service/EnvironnementService.java +++ b/src/main/java/fr/icdc/ebad/service/EnvironnementService.java @@ -7,7 +7,6 @@ import fr.icdc.ebad.plugin.dto.NormeDiscoverDto; import fr.icdc.ebad.plugin.plugin.EnvironnementConnectorPlugin; import fr.icdc.ebad.repository.*; -import fr.icdc.ebad.security.permission.PermissionIdentity; import fr.icdc.ebad.service.util.EbadServiceException; import ma.glasnost.orika.MapperFacade; import org.jobrunr.scheduling.JobScheduler; @@ -29,6 +28,8 @@ import java.text.SimpleDateFormat; import java.util.*; +import static fr.icdc.ebad.config.Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID; + /** * Created by dtrouillet on 03/03/2016. */ @@ -53,8 +54,10 @@ public class EnvironnementService { private final SpringPluginManager springPluginManager; private final SchedulingRepository schedulingRepository; private final JobScheduler jobScheduler; + private final GlobalSettingService globalSettingService; + private final IdentityRepository identityRepository; - public EnvironnementService(ShellService shellService, EnvironnementRepository environnementRepository, BatchRepository batchRepository, LogBatchRepository logBatchRepository, ChaineRepository chaineRepository, DirectoryRepository directoryRepository, NormeRepository normeRepository, MapperFacade mapper, List environnementConnectorPluginList, ApplicationRepository applicationRepository, SpringPluginManager springPluginManager, SchedulingRepository schedulingRepository, JobScheduler jobScheduler) { + public EnvironnementService(ShellService shellService, EnvironnementRepository environnementRepository, BatchRepository batchRepository, LogBatchRepository logBatchRepository, ChaineRepository chaineRepository, DirectoryRepository directoryRepository, NormeRepository normeRepository, MapperFacade mapper, List environnementConnectorPluginList, ApplicationRepository applicationRepository, SpringPluginManager springPluginManager, SchedulingRepository schedulingRepository, JobScheduler jobScheduler, GlobalSettingService globalSettingService, IdentityRepository identityRepository) { this.shellService = shellService; this.environnementRepository = environnementRepository; this.batchRepository = batchRepository; @@ -68,6 +71,8 @@ public EnvironnementService(ShellService shellService, EnvironnementRepository e this.springPluginManager = springPluginManager; this.schedulingRepository = schedulingRepository; this.jobScheduler = jobScheduler; + this.globalSettingService = globalSettingService; + this.identityRepository = identityRepository; } @@ -179,6 +184,8 @@ public Set importEnvironments(Long applicationId) throws EbadServ Application application = applicationRepository.findById(applicationId).orElseThrow(() -> new EbadServiceException("No application with id " + applicationId)); Set environnements = new HashSet<>(); List normeDiscoverDtos = mapper.mapAsList(normeRepository.findAll(), NormeDiscoverDto.class); + GlobalSetting defaultIdentityId = globalSettingService.getValue(GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID); + Identity identity = identityRepository.getById(Long.valueOf(defaultIdentityId.getValue())); try { for (EnvironnementConnectorPlugin environnementConnectorPlugin : environnementConnectorPluginList) { @@ -198,7 +205,7 @@ public Set importEnvironments(Long applicationId) throws EbadServ environnement.setExternalId(environnementDiscoverDto.getId()); environnement.setPluginId(pluginId); environnement.setApplication(application); - //FIXME DTROUILLET ADD DEFAULT IDENTITY + environnement.setIdentity(identity); try { environnementRepository.save(environnement); diff --git a/src/main/resources/db/changelog/changeset/2.7.0-1-changeset.yaml b/src/main/resources/db/changelog/changeset/2.7.0-1-changeset.yaml new file mode 100644 index 00000000..38c6d15d --- /dev/null +++ b/src/main/resources/db/changelog/changeset/2.7.0-1-changeset.yaml @@ -0,0 +1,20 @@ +databaseChangeLog: + - changeSet: + id: global_setting_default_identity + author: dtrouillet + changes: + - insert: + tableName: t_global_setting + columns: + - column: + name: key + value: DEFAULT_IDENTITY_ID + - column: + name: value + value: "1" + - column: + name: description + value: Default id identity for environment import + - column: + name: label + value: Default Identity ID diff --git a/src/test/java/fr/icdc/ebad/service/EnvironnementServiceTest.java b/src/test/java/fr/icdc/ebad/service/EnvironnementServiceTest.java index 99139539..993ff1fa 100644 --- a/src/test/java/fr/icdc/ebad/service/EnvironnementServiceTest.java +++ b/src/test/java/fr/icdc/ebad/service/EnvironnementServiceTest.java @@ -1,6 +1,7 @@ package fr.icdc.ebad.service; import com.querydsl.core.types.Predicate; +import fr.icdc.ebad.config.Constants; import fr.icdc.ebad.domain.*; import fr.icdc.ebad.domain.util.RetourBatch; import fr.icdc.ebad.plugin.dto.EnvironnementDiscoverDto; @@ -63,6 +64,10 @@ public class EnvironnementServiceTest { @Mock private SchedulingRepository schedulingRepository; @Mock + private GlobalSettingService globalSettingService; + @Mock + private IdentityRepository identityRepository; + @Mock private JobScheduler jobScheduler; @Spy private MapperFacade mapperFacade; @@ -323,6 +328,10 @@ public List getDependencies() { when(environnementRepository.findAllByExternalIdAndPluginId(eq(environnementDiscoverDto1.getId()), eq("import-plugin"))).thenReturn(Optional.empty()); when(environnementRepository.findAllByExternalIdAndPluginId(eq(environnementDiscoverDto2.getId()), eq("import-plugin"))).thenReturn(Optional.empty()); + GlobalSetting globalSetting = GlobalSetting.builder().key(Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID).value("1").build(); + when(globalSettingService.getValue(Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID)).thenReturn(globalSetting); + Identity identity = Identity.builder().id(1L).build(); + when(identityRepository.getById(eq(1L))).thenReturn(identity); when(springPluginManager.whichPlugin(any())).thenReturn(pluginWrapper); //WHEN @@ -359,7 +368,7 @@ public List getDependencies() { .homePath(environnementDiscoverDto1.getHome()) .name(environnementDiscoverDto1.getName()) .prefix(environnementDiscoverDto1.getPrefix()) -// .login(environnementDiscoverDto1.getLogin()) + .identity(identity) .application(application) .pluginId("import-plugin") .build(); @@ -370,7 +379,7 @@ public List getDependencies() { .homePath(environnementDiscoverDto2.getHome()) .name(environnementDiscoverDto2.getName()) .prefix(environnementDiscoverDto2.getPrefix()) -// .login(environnementDiscoverDto2.getLogin()) + .identity(identity) .application(application) .pluginId("import-plugin") .build(); @@ -466,6 +475,11 @@ public List getDependencies() { }; PluginWrapper pluginWrapper = new PluginWrapper(springPluginManager, pluginDescriptor, null, null); + GlobalSetting globalSetting = GlobalSetting.builder().key(Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID).value("1").build(); + when(globalSettingService.getValue(Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID)).thenReturn(globalSetting); + Identity identity = Identity.builder().id(1L).build(); + when(identityRepository.getById(eq(1L))).thenReturn(identity); + when(applicationRepository.findById(eq(1L))).thenReturn(Optional.of(application)); when(normeRepository.findAll()).thenReturn(normeList); when(environnementConnectorPlugin.discoverFromApp(eq(application.getCode()), eq(application.getName()), anyList())).thenThrow(new PluginRuntimeException()); @@ -582,6 +596,11 @@ public List getDependencies() { when(environnementRepository.findAllByExternalIdAndPluginId(eq(environnementDiscoverDto1.getId()), eq("import-plugin"))).thenReturn(Optional.empty()); when(environnementRepository.findAllByExternalIdAndPluginId(eq(environnementDiscoverDto2.getId()), eq("import-plugin"))).thenReturn(Optional.empty()); + GlobalSetting globalSetting = GlobalSetting.builder().key(Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID).value("1").build(); + when(globalSettingService.getValue(Constants.GLOBAL_SETTINGS_DEFAULT_IDENTITY_ID)).thenReturn(globalSetting); + Identity identity = Identity.builder().id(1L).build(); + when(identityRepository.getById(eq(1L))).thenReturn(identity); + when(springPluginManager.whichPlugin(any())).thenReturn(pluginWrapper); //WHEN @@ -599,7 +618,7 @@ public List getDependencies() { && env.getName().equals(environnementDiscoverDto1.getName()) && env.getPrefix().equals(environnementDiscoverDto1.getPrefix()) && env.getExternalId().equals(environnementDiscoverDto1.getId()) -// && env.getLogin().equals(environnementDiscoverDto1.getLogin()) + && env.getIdentity().getId().equals(identity.getId()) && env.getPluginId().equals("import-plugin"))); verify(environnementRepository).save(argThat((env) -> env.getApplication().equals(application) @@ -608,7 +627,7 @@ public List getDependencies() { && env.getName().equals(environnementDiscoverDto2.getName()) && env.getPrefix().equals(environnementDiscoverDto2.getPrefix()) && env.getExternalId().equals(environnementDiscoverDto2.getId()) -// && env.getLogin().equals(environnementDiscoverDto2.getLogin()) + && env.getIdentity().getId().equals(identity.getId()) && env.getPluginId().equals("import-plugin"))); Environnement expectedEnv1 = Environnement.builder() @@ -617,7 +636,7 @@ public List getDependencies() { .homePath(environnementDiscoverDto1.getHome()) .name(environnementDiscoverDto1.getName()) .prefix(environnementDiscoverDto1.getPrefix()) -// .login(environnementDiscoverDto1.getLogin()) + .identity(identity) .application(application) .pluginId("import-plugin") .build(); @@ -628,7 +647,7 @@ public List getDependencies() { .homePath(environnementDiscoverDto2.getHome()) .name(environnementDiscoverDto2.getName()) .prefix(environnementDiscoverDto2.getPrefix()) -// .login(environnementDiscoverDto2.getLogin()) + .identity(identity) .application(application) .pluginId("import-plugin") .build();