Skip to content

Commit

Permalink
Add default identity id
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrouillet committed Jun 28, 2021
1 parent e0bba33 commit 165742f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/main/java/fr/icdc/ebad/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import java.util.Arrays;

import static fr.icdc.ebad.config.Constants.*;

/**
* Determine if a service is opened or closed
*/
Expand All @@ -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() {
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/fr/icdc/ebad/service/EnvironnementService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand All @@ -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<EnvironnementConnectorPlugin> 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<EnvironnementConnectorPlugin> environnementConnectorPluginList, ApplicationRepository applicationRepository, SpringPluginManager springPluginManager, SchedulingRepository schedulingRepository, JobScheduler jobScheduler, GlobalSettingService globalSettingService, IdentityRepository identityRepository) {
this.shellService = shellService;
this.environnementRepository = environnementRepository;
this.batchRepository = batchRepository;
Expand All @@ -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;
}


Expand Down Expand Up @@ -179,6 +184,8 @@ public Set<Environnement> importEnvironments(Long applicationId) throws EbadServ
Application application = applicationRepository.findById(applicationId).orElseThrow(() -> new EbadServiceException("No application with id " + applicationId));
Set<Environnement> environnements = new HashSet<>();
List<NormeDiscoverDto> 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) {
Expand All @@ -198,7 +205,7 @@ public Set<Environnement> 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);
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/db/changelog/changeset/2.7.0-1-changeset.yaml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 25 additions & 6 deletions src/test/java/fr/icdc/ebad/service/EnvironnementServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -323,6 +328,10 @@ public List<PluginDependency> 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
Expand Down Expand Up @@ -359,7 +368,7 @@ public List<PluginDependency> getDependencies() {
.homePath(environnementDiscoverDto1.getHome())
.name(environnementDiscoverDto1.getName())
.prefix(environnementDiscoverDto1.getPrefix())
// .login(environnementDiscoverDto1.getLogin())
.identity(identity)
.application(application)
.pluginId("import-plugin")
.build();
Expand All @@ -370,7 +379,7 @@ public List<PluginDependency> getDependencies() {
.homePath(environnementDiscoverDto2.getHome())
.name(environnementDiscoverDto2.getName())
.prefix(environnementDiscoverDto2.getPrefix())
// .login(environnementDiscoverDto2.getLogin())
.identity(identity)
.application(application)
.pluginId("import-plugin")
.build();
Expand Down Expand Up @@ -466,6 +475,11 @@ public List<PluginDependency> 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());
Expand Down Expand Up @@ -582,6 +596,11 @@ public List<PluginDependency> 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
Expand All @@ -599,7 +618,7 @@ public List<PluginDependency> 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)
Expand All @@ -608,7 +627,7 @@ public List<PluginDependency> 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()
Expand All @@ -617,7 +636,7 @@ public List<PluginDependency> getDependencies() {
.homePath(environnementDiscoverDto1.getHome())
.name(environnementDiscoverDto1.getName())
.prefix(environnementDiscoverDto1.getPrefix())
// .login(environnementDiscoverDto1.getLogin())
.identity(identity)
.application(application)
.pluginId("import-plugin")
.build();
Expand All @@ -628,7 +647,7 @@ public List<PluginDependency> getDependencies() {
.homePath(environnementDiscoverDto2.getHome())
.name(environnementDiscoverDto2.getName())
.prefix(environnementDiscoverDto2.getPrefix())
// .login(environnementDiscoverDto2.getLogin())
.identity(identity)
.application(application)
.pluginId("import-plugin")
.build();
Expand Down

0 comments on commit 165742f

Please sign in to comment.