diff --git a/sugoi-api-core/src/main/java/fr/insee/sugoi/core/service/impl/ApplicationServiceImpl.java b/sugoi-api-core/src/main/java/fr/insee/sugoi/core/service/impl/ApplicationServiceImpl.java index 28005a4a1..9a9a68c21 100644 --- a/sugoi-api-core/src/main/java/fr/insee/sugoi/core/service/impl/ApplicationServiceImpl.java +++ b/sugoi-api-core/src/main/java/fr/insee/sugoi/core/service/impl/ApplicationServiceImpl.java @@ -24,12 +24,14 @@ import fr.insee.sugoi.core.service.ApplicationService; import fr.insee.sugoi.core.store.StoreProvider; import fr.insee.sugoi.model.Application; +import fr.insee.sugoi.model.Group; import fr.insee.sugoi.model.Realm; import fr.insee.sugoi.model.exceptions.ApplicationNotFoundException; import fr.insee.sugoi.model.exceptions.RealmNotFoundException; import fr.insee.sugoi.model.paging.PageResult; import fr.insee.sugoi.model.paging.PageableResult; import fr.insee.sugoi.model.paging.SearchType; +import java.util.ArrayList; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -45,6 +47,9 @@ public class ApplicationServiceImpl implements ApplicationService { public ProviderResponse create( String realm, Application application, ProviderRequest providerRequest) { try { + if (application.getGroups() == null) { + application.setGroups(new ArrayList()); + } ProviderResponse response = storeProvider.getWriterStore(realm).createApplication(application, providerRequest); sugoiEventPublisher.publishCustomEvent( diff --git a/sugoi-api-file-store-provider/src/main/java/fr/insee/sugoi/store/file/FileWriterStore.java b/sugoi-api-file-store-provider/src/main/java/fr/insee/sugoi/store/file/FileWriterStore.java index e767a74da..b17c33eae 100644 --- a/sugoi-api-file-store-provider/src/main/java/fr/insee/sugoi/store/file/FileWriterStore.java +++ b/sugoi-api-file-store-provider/src/main/java/fr/insee/sugoi/store/file/FileWriterStore.java @@ -27,6 +27,7 @@ import fr.insee.sugoi.model.User; import fr.insee.sugoi.model.exceptions.ApplicationNotFoundException; import fr.insee.sugoi.model.exceptions.GroupAlreadyExistException; +import fr.insee.sugoi.model.exceptions.NoGroupException; import fr.insee.sugoi.model.exceptions.UserNotFoundException; import java.io.File; import java.io.FileWriter; @@ -119,6 +120,9 @@ public ProviderResponse deleteGroup( @Override public ProviderResponse createGroup( String appName, Group appGroup, ProviderRequest providerRequest) { + if (appGroup == null) { + throw new NoGroupException("no group provided"); + } fileReaderStore.setResourceLoader(resourceLoader); Application application = fileReaderStore diff --git a/sugoi-api-file-store-provider/src/test/java/fr/insee/sugoi/store/file/FileWriterStoreTest.java b/sugoi-api-file-store-provider/src/test/java/fr/insee/sugoi/store/file/FileWriterStoreTest.java index 214003e42..47cdbea48 100644 --- a/sugoi-api-file-store-provider/src/test/java/fr/insee/sugoi/store/file/FileWriterStoreTest.java +++ b/sugoi-api-file-store-provider/src/test/java/fr/insee/sugoi/store/file/FileWriterStoreTest.java @@ -15,9 +15,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.fasterxml.jackson.databind.ObjectMapper; import fr.insee.sugoi.model.*; +import fr.insee.sugoi.model.exceptions.NoGroupException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; @@ -358,6 +360,17 @@ public void testCreateGroup() { is("Groupy_Applitest")); } + @Test + public void testCreateGroupWithNoGroup() { + Group group = null; + assertThrows( + NoGroupException.class, + () -> { + fileWriterStore.createGroup("Applitest", group, null); + }, + "Should throw NoGroupException"); + } + @Test public void testDeleteGroup() { Group group = new Group("Asupprimer_WebServicesLdap", "WebServicesLdap"); diff --git a/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapWriterStore.java b/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapWriterStore.java index 963d20367..0295040da 100644 --- a/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapWriterStore.java +++ b/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapWriterStore.java @@ -51,6 +51,7 @@ import fr.insee.sugoi.model.exceptions.GroupAlreadyExistException; import fr.insee.sugoi.model.exceptions.GroupNotFoundException; import fr.insee.sugoi.model.exceptions.InvalidPasswordException; +import fr.insee.sugoi.model.exceptions.NoGroupException; import fr.insee.sugoi.model.exceptions.OrganizationAlreadyExistException; import fr.insee.sugoi.model.exceptions.OrganizationNotFoundException; import fr.insee.sugoi.model.exceptions.StoragePolicyNotMetException; @@ -61,6 +62,7 @@ import fr.insee.sugoi.model.technics.StoreMapping; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -213,10 +215,17 @@ public ProviderResponse createGroup( String appName, Group group, ProviderRequest providerRequest) { try { - if (ldapReaderStore.getApplication(appName).isEmpty()) { + if (group == null) { + throw new NoGroupException("no group provided"); + } + Optional applicationOptional = ldapReaderStore.getApplication(appName); + if (applicationOptional.isEmpty()) { throw new ApplicationNotFoundException(appName); } - + Application application = applicationOptional.get(); + if (application.getGroups() == null) { + application.setGroups(new ArrayList()); + } if (ldapReaderStore.getGroup(appName, group.getName()).isPresent()) { throw new GroupAlreadyExistException( String.format( diff --git a/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapWriterStoreTest.java b/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapWriterStoreTest.java index 680dcdd66..db4713f68 100644 --- a/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapWriterStoreTest.java +++ b/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapWriterStoreTest.java @@ -14,7 +14,9 @@ package fr.insee.sugoi.ldap; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertThrows; import fr.insee.sugoi.core.configuration.GlobalKeysConfig; @@ -22,9 +24,17 @@ import fr.insee.sugoi.core.model.ProviderResponse; import fr.insee.sugoi.core.model.ProviderResponse.ProviderResponseStatus; import fr.insee.sugoi.ldap.utils.config.LdapConfigKeys; -import fr.insee.sugoi.model.*; +import fr.insee.sugoi.model.Application; +import fr.insee.sugoi.model.Group; +import fr.insee.sugoi.model.Habilitation; +import fr.insee.sugoi.model.Organization; +import fr.insee.sugoi.model.PostalAddress; +import fr.insee.sugoi.model.Realm; +import fr.insee.sugoi.model.User; +import fr.insee.sugoi.model.UserStorage; import fr.insee.sugoi.model.exceptions.ApplicationNotFoundException; import fr.insee.sugoi.model.exceptions.InvalidPasswordException; +import fr.insee.sugoi.model.exceptions.NoGroupException; import fr.insee.sugoi.model.exceptions.StoragePolicyNotMetException; import fr.insee.sugoi.model.fixtures.StoreMappingFixture; import fr.insee.sugoi.store.ldap.LdapReaderStore; @@ -383,6 +393,17 @@ public void testCreateGroup() { is("Groupy_Applitest")); } + @Test + public void testCreateGroupWithNoGroup() { + Group group = null; + assertThrows( + NoGroupException.class, + () -> { + ldapWriterStore.createGroup("Applitest", group, null); + }, + "Should throw NoGroupException"); + } + @Test void testCreateGroupApplicationNotFound() { Group group = new Group(); diff --git a/sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/NoGroupException.java b/sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/NoGroupException.java new file mode 100644 index 000000000..4cd8034d0 --- /dev/null +++ b/sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/NoGroupException.java @@ -0,0 +1,27 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package fr.insee.sugoi.model.exceptions; + +public class NoGroupException extends BadRequestException { + + public NoGroupException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + public NoGroupException(String message) { + super(message); + // TODO Auto-generated constructor stub + } +} diff --git a/sugoi-api-test/src/test/resources/scenario/application_scenario_sync.feature b/sugoi-api-test/src/test/resources/scenario/application_scenario_sync.feature index 4f0ba483a..0c9331077 100644 --- a/sugoi-api-test/src/test/resources/scenario/application_scenario_sync.feature +++ b/sugoi-api-test/src/test/resources/scenario/application_scenario_sync.feature @@ -34,6 +34,21 @@ Feature: Applications scenario Then the client receives status code 201 Then the client expect to receive an application + Scenario: Post application without group + When the client perform POST request with body on url /realms/domaine1/applications body: + """ + { + "name": "dada", + "groups": null, + "attributes": { + "owner": "Branche privative de l'application applitest" + } + } + """ + And show body received + Then the client receives status code 201 + Then the client expect to receive an application + Scenario: Post application alreadyExist When the client perform POST request with body on url /realms/domaine1/applications body: """