Skip to content

Commit 4b9d2d7

Browse files
committed
[BUG] bug-npe-is-thrown-when-creating-an-application-without-a-group
(#1224)
1 parent b41da50 commit 4b9d2d7

File tree

7 files changed

+95
-4
lines changed

7 files changed

+95
-4
lines changed

sugoi-api-core/src/main/java/fr/insee/sugoi/core/service/impl/ApplicationServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import fr.insee.sugoi.core.service.ApplicationService;
2525
import fr.insee.sugoi.core.store.StoreProvider;
2626
import fr.insee.sugoi.model.Application;
27+
import fr.insee.sugoi.model.Group;
2728
import fr.insee.sugoi.model.Realm;
2829
import fr.insee.sugoi.model.exceptions.ApplicationNotFoundException;
2930
import fr.insee.sugoi.model.exceptions.RealmNotFoundException;
3031
import fr.insee.sugoi.model.paging.PageResult;
3132
import fr.insee.sugoi.model.paging.PageableResult;
3233
import fr.insee.sugoi.model.paging.SearchType;
34+
import java.util.ArrayList;
3335
import java.util.Map;
3436
import org.springframework.beans.factory.annotation.Autowired;
3537
import org.springframework.stereotype.Service;
@@ -45,6 +47,9 @@ public class ApplicationServiceImpl implements ApplicationService {
4547
public ProviderResponse create(
4648
String realm, Application application, ProviderRequest providerRequest) {
4749
try {
50+
if (application.getGroups() == null) {
51+
application.setGroups(new ArrayList<Group>());
52+
}
4853
ProviderResponse response =
4954
storeProvider.getWriterStore(realm).createApplication(application, providerRequest);
5055
sugoiEventPublisher.publishCustomEvent(

sugoi-api-file-store-provider/src/main/java/fr/insee/sugoi/store/file/FileWriterStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import fr.insee.sugoi.model.User;
2828
import fr.insee.sugoi.model.exceptions.ApplicationNotFoundException;
2929
import fr.insee.sugoi.model.exceptions.GroupAlreadyExistException;
30+
import fr.insee.sugoi.model.exceptions.NoGroupException;
3031
import fr.insee.sugoi.model.exceptions.UserNotFoundException;
3132
import java.io.File;
3233
import java.io.FileWriter;
@@ -119,6 +120,9 @@ public ProviderResponse deleteGroup(
119120
@Override
120121
public ProviderResponse createGroup(
121122
String appName, Group appGroup, ProviderRequest providerRequest) {
123+
if (appGroup == null) {
124+
throw new NoGroupException("no group provided");
125+
}
122126
fileReaderStore.setResourceLoader(resourceLoader);
123127
Application application =
124128
fileReaderStore

sugoi-api-file-store-provider/src/test/java/fr/insee/sugoi/store/file/FileWriterStoreTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
import static org.hamcrest.MatcherAssert.assertThat;
1717
import static org.hamcrest.Matchers.*;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1819

1920
import com.fasterxml.jackson.databind.ObjectMapper;
2021
import fr.insee.sugoi.model.*;
22+
import fr.insee.sugoi.model.exceptions.NoGroupException;
2123
import java.io.FileWriter;
2224
import java.io.IOException;
2325
import java.util.ArrayList;
@@ -358,6 +360,17 @@ public void testCreateGroup() {
358360
is("Groupy_Applitest"));
359361
}
360362

363+
@Test
364+
public void testCreateGroupWithNoGroup() {
365+
Group group = null;
366+
assertThrows(
367+
NoGroupException.class,
368+
() -> {
369+
fileWriterStore.createGroup("Applitest", group, null);
370+
},
371+
"Should throw NoGroupException");
372+
}
373+
361374
@Test
362375
public void testDeleteGroup() {
363376
Group group = new Group("Asupprimer_WebServicesLdap", "WebServicesLdap");

sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapWriterStore.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import fr.insee.sugoi.model.exceptions.GroupAlreadyExistException;
5252
import fr.insee.sugoi.model.exceptions.GroupNotFoundException;
5353
import fr.insee.sugoi.model.exceptions.InvalidPasswordException;
54+
import fr.insee.sugoi.model.exceptions.NoGroupException;
5455
import fr.insee.sugoi.model.exceptions.OrganizationAlreadyExistException;
5556
import fr.insee.sugoi.model.exceptions.OrganizationNotFoundException;
5657
import fr.insee.sugoi.model.exceptions.StoragePolicyNotMetException;
@@ -61,6 +62,7 @@
6162
import fr.insee.sugoi.model.technics.StoreMapping;
6263
import java.security.cert.CertificateException;
6364
import java.security.cert.X509Certificate;
65+
import java.util.ArrayList;
6466
import java.util.List;
6567
import java.util.Map;
6668
import java.util.Optional;
@@ -213,10 +215,17 @@ public ProviderResponse createGroup(
213215
String appName, Group group, ProviderRequest providerRequest) {
214216

215217
try {
216-
if (ldapReaderStore.getApplication(appName).isEmpty()) {
218+
if (group == null) {
219+
throw new NoGroupException("no group provided");
220+
}
221+
Optional<Application> applicationOptional = ldapReaderStore.getApplication(appName);
222+
if (applicationOptional.isEmpty()) {
217223
throw new ApplicationNotFoundException(appName);
218224
}
219-
225+
Application application = applicationOptional.get();
226+
if (application.getGroups() == null) {
227+
application.setGroups(new ArrayList<Group>());
228+
}
220229
if (ldapReaderStore.getGroup(appName, group.getName()).isPresent()) {
221230
throw new GroupAlreadyExistException(
222231
String.format(

sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapWriterStoreTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,27 @@
1414
package fr.insee.sugoi.ldap;
1515

1616
import static org.hamcrest.MatcherAssert.assertThat;
17-
import static org.hamcrest.Matchers.*;
17+
import static org.hamcrest.Matchers.is;
18+
import static org.hamcrest.Matchers.not;
19+
import static org.hamcrest.Matchers.nullValue;
1820
import static org.junit.jupiter.api.Assertions.assertThrows;
1921

2022
import fr.insee.sugoi.core.configuration.GlobalKeysConfig;
2123
import fr.insee.sugoi.core.model.ProviderRequest;
2224
import fr.insee.sugoi.core.model.ProviderResponse;
2325
import fr.insee.sugoi.core.model.ProviderResponse.ProviderResponseStatus;
2426
import fr.insee.sugoi.ldap.utils.config.LdapConfigKeys;
25-
import fr.insee.sugoi.model.*;
27+
import fr.insee.sugoi.model.Application;
28+
import fr.insee.sugoi.model.Group;
29+
import fr.insee.sugoi.model.Habilitation;
30+
import fr.insee.sugoi.model.Organization;
31+
import fr.insee.sugoi.model.PostalAddress;
32+
import fr.insee.sugoi.model.Realm;
33+
import fr.insee.sugoi.model.User;
34+
import fr.insee.sugoi.model.UserStorage;
2635
import fr.insee.sugoi.model.exceptions.ApplicationNotFoundException;
2736
import fr.insee.sugoi.model.exceptions.InvalidPasswordException;
37+
import fr.insee.sugoi.model.exceptions.NoGroupException;
2838
import fr.insee.sugoi.model.exceptions.StoragePolicyNotMetException;
2939
import fr.insee.sugoi.model.fixtures.StoreMappingFixture;
3040
import fr.insee.sugoi.store.ldap.LdapReaderStore;
@@ -383,6 +393,17 @@ public void testCreateGroup() {
383393
is("Groupy_Applitest"));
384394
}
385395

396+
@Test
397+
public void testCreateGroupWithNoGroup() {
398+
Group group = null;
399+
assertThrows(
400+
NoGroupException.class,
401+
() -> {
402+
ldapWriterStore.createGroup("Applitest", group, null);
403+
},
404+
"Should throw NoGroupException");
405+
}
406+
386407
@Test
387408
void testCreateGroupApplicationNotFound() {
388409
Group group = new Group();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package fr.insee.sugoi.model.exceptions;
15+
16+
public class NoGroupException extends BadRequestException {
17+
18+
public NoGroupException(String message, Throwable cause) {
19+
super(message, cause);
20+
// TODO Auto-generated constructor stub
21+
}
22+
23+
public NoGroupException(String message) {
24+
super(message);
25+
// TODO Auto-generated constructor stub
26+
}
27+
}

sugoi-api-test/src/test/resources/scenario/application_scenario_sync.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ Feature: Applications scenario
3434
Then the client receives status code 201
3535
Then the client expect to receive an application
3636

37+
Scenario: Post application without group
38+
When the client perform POST request with body on url /realms/domaine1/applications body:
39+
"""
40+
{
41+
"name": "MyApp2",
42+
"groups": null
43+
}
44+
"""
45+
And show body received
46+
Then the client receives status code 201
47+
Then the client expect to receive an application
48+
3749
Scenario: Post application alreadyExist
3850
When the client perform POST request with body on url /realms/domaine1/applications body:
3951
"""

0 commit comments

Comments
 (0)