1+ /*******************************************************************************
2+ *
3+ * Copyright (c) 2025 AITIA
4+ *
5+ * This program and the accompanying materials are made available under the
6+ * terms of the Eclipse Public License 2.0 which is available at
7+ *
8+ * http://www.eclipse.org/legal/epl-2.0.
9+ *
10+ * SPDX-License-Identifier: EPL-2.0
11+ *
12+ * Contributors:
13+ * AITIA - implementation
14+ * Arrowhead Consortia - conceptualization
15+ *
16+ *******************************************************************************/
17+ package eu .arrowhead .authorization .service ;
18+
19+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22+ import static org .mockito .Mockito .verify ;
23+ import static org .mockito .Mockito .when ;
24+
25+ import java .util .List ;
26+
27+ import org .junit .jupiter .api .Test ;
28+ import org .junit .jupiter .api .extension .ExtendWith ;
29+ import org .mockito .InjectMocks ;
30+ import org .mockito .Mock ;
31+ import org .mockito .junit .jupiter .MockitoExtension ;
32+ import org .springframework .data .util .Pair ;
33+
34+ import eu .arrowhead .authorization .jpa .entity .AuthPolicy ;
35+ import eu .arrowhead .authorization .jpa .entity .AuthProviderPolicyHeader ;
36+ import eu .arrowhead .authorization .jpa .service .AuthorizationPolicyDbService ;
37+ import eu .arrowhead .authorization .service .dto .DTOConverter ;
38+ import eu .arrowhead .authorization .service .dto .NormalizedGrantRequest ;
39+ import eu .arrowhead .authorization .service .engine .AuthorizationPolicyEngine ;
40+ import eu .arrowhead .authorization .service .validation .AuthorizationValidation ;
41+ import eu .arrowhead .common .exception .ArrowheadException ;
42+ import eu .arrowhead .common .exception .InternalServerError ;
43+ import eu .arrowhead .common .exception .InvalidParameterException ;
44+ import eu .arrowhead .dto .AuthorizationGrantRequestDTO ;
45+ import eu .arrowhead .dto .AuthorizationPolicyDTO ;
46+ import eu .arrowhead .dto .AuthorizationPolicyRequestDTO ;
47+ import eu .arrowhead .dto .AuthorizationPolicyResponseDTO ;
48+ import eu .arrowhead .dto .enums .AuthorizationLevel ;
49+ import eu .arrowhead .dto .enums .AuthorizationPolicyType ;
50+ import eu .arrowhead .dto .enums .AuthorizationTargetType ;
51+
52+ @ ExtendWith (MockitoExtension .class )
53+ public class AuthorizationServiceTest {
54+
55+ //=================================================================================================
56+ // members
57+
58+ @ InjectMocks
59+ private AuthorizationService service ;
60+
61+ @ Mock
62+ private AuthorizationValidation validator ;
63+
64+ @ Mock
65+ private AuthorizationPolicyDbService dbService ;
66+
67+ @ Mock
68+ private AuthorizationPolicyEngine policyEngine ;
69+
70+ @ Mock
71+ private DTOConverter dtoConverter ;
72+
73+ //=================================================================================================
74+ // methods
75+
76+ //-------------------------------------------------------------------------------------------------
77+ @ Test
78+ public void testGrantOperationNullOrigin () {
79+ final Throwable ex = assertThrows (
80+ IllegalArgumentException .class ,
81+ () -> service .grantOperation (null , null , null ));
82+
83+ assertEquals ("origin is empty" , ex .getMessage ());
84+ }
85+
86+ //-------------------------------------------------------------------------------------------------
87+ @ Test
88+ public void testGrantOperationEmptyOrigin () {
89+ final Throwable ex = assertThrows (
90+ IllegalArgumentException .class ,
91+ () -> service .grantOperation (null , null , "" ));
92+
93+ assertEquals ("origin is empty" , ex .getMessage ());
94+ }
95+
96+ //-------------------------------------------------------------------------------------------------
97+ @ Test
98+ public void testGrantOperationInvalidParameterException () {
99+ final AuthorizationGrantRequestDTO dto = new AuthorizationGrantRequestDTO (
100+ "LOCAL" ,
101+ AuthorizationTargetType .SERVICE_DEF .name (),
102+ "serviceDef" ,
103+ "description" ,
104+ new AuthorizationPolicyRequestDTO (AuthorizationPolicyType .ALL .name (), null , null ),
105+ null );
106+
107+ final NormalizedGrantRequest normalized = new NormalizedGrantRequest (AuthorizationLevel .PROVIDER );
108+
109+ when (validator .validateAndNormalizeSystemName ("TestProvider" , "origin" )).thenReturn ("TestProvider" );
110+ when (validator .validateAndNormalizeGrantRequest ("TestProvider" , dto , "origin" )).thenReturn (normalized );
111+ when (dbService .createProviderLevelPolicy (normalized )).thenThrow (new InvalidParameterException ("test" ));
112+
113+ final ArrowheadException ex = assertThrows (
114+ InvalidParameterException .class ,
115+ () -> service .grantOperation ("TestProvider" , dto , "origin" ));
116+
117+ assertEquals ("test" , ex .getMessage ());
118+ assertEquals ("origin" , ex .getOrigin ());
119+
120+ verify (validator ).validateAndNormalizeSystemName ("TestProvider" , "origin" );
121+ verify (validator ).validateAndNormalizeGrantRequest ("TestProvider" , dto , "origin" );
122+ verify (dbService ).createProviderLevelPolicy (normalized );
123+ }
124+
125+ //-------------------------------------------------------------------------------------------------
126+ @ Test
127+ public void testGrantOperationInternalServerError () {
128+ final AuthorizationGrantRequestDTO dto = new AuthorizationGrantRequestDTO (
129+ "LOCAL" ,
130+ AuthorizationTargetType .SERVICE_DEF .name (),
131+ "serviceDef" ,
132+ "description" ,
133+ new AuthorizationPolicyRequestDTO (AuthorizationPolicyType .ALL .name (), null , null ),
134+ null );
135+
136+ final NormalizedGrantRequest normalized = new NormalizedGrantRequest (AuthorizationLevel .PROVIDER );
137+
138+ when (validator .validateAndNormalizeSystemName ("TestProvider" , "origin" )).thenReturn ("TestProvider" );
139+ when (validator .validateAndNormalizeGrantRequest ("TestProvider" , dto , "origin" )).thenReturn (normalized );
140+ when (dbService .createProviderLevelPolicy (normalized )).thenThrow (new InternalServerError ("test" ));
141+
142+ final ArrowheadException ex = assertThrows (
143+ InternalServerError .class ,
144+ () -> service .grantOperation ("TestProvider" , dto , "origin" ));
145+
146+ assertEquals ("test" , ex .getMessage ());
147+ assertEquals ("origin" , ex .getOrigin ());
148+
149+ verify (validator ).validateAndNormalizeSystemName ("TestProvider" , "origin" );
150+ verify (validator ).validateAndNormalizeGrantRequest ("TestProvider" , dto , "origin" );
151+ verify (dbService ).createProviderLevelPolicy (normalized );
152+ }
153+
154+ //-------------------------------------------------------------------------------------------------
155+ @ Test
156+ public void testGrantOperationOk () {
157+ final AuthorizationGrantRequestDTO dto = new AuthorizationGrantRequestDTO (
158+ "LOCAL" ,
159+ AuthorizationTargetType .SERVICE_DEF .name (),
160+ "serviceDef" ,
161+ "description" ,
162+ new AuthorizationPolicyRequestDTO (AuthorizationPolicyType .ALL .name (), null , null ),
163+ null );
164+
165+ final NormalizedGrantRequest normalized = new NormalizedGrantRequest (AuthorizationLevel .PROVIDER );
166+
167+ final Pair <Pair <AuthProviderPolicyHeader , List <AuthPolicy >>, Boolean > dbResult = Pair .of (
168+ Pair .of (new AuthProviderPolicyHeader (), List .of (new AuthPolicy ())),
169+ true );
170+
171+ final AuthorizationPolicyResponseDTO result = new AuthorizationPolicyResponseDTO (
172+ "PR|LOCAL|TestProvider|SERVICE_DEF|serviceDef" ,
173+ AuthorizationLevel .PROVIDER ,
174+ "LOCAL" ,
175+ "TestProvider" ,
176+ AuthorizationTargetType .SERVICE_DEF ,
177+ "serviceDef" ,
178+ "description" ,
179+ new AuthorizationPolicyDTO (AuthorizationPolicyType .ALL , null , null ),
180+ null ,
181+ "2025-10-16T16:00:01Z" ,
182+ "2025-10-16T16:00:01Z" );
183+
184+ when (validator .validateAndNormalizeSystemName ("TestProvider" , "origin" )).thenReturn ("TestProvider" );
185+ when (validator .validateAndNormalizeGrantRequest ("TestProvider" , dto , "origin" )).thenReturn (normalized );
186+ when (dbService .createProviderLevelPolicy (normalized )).thenReturn (dbResult );
187+ when (dtoConverter .convertPolicyToResponse (AuthorizationLevel .PROVIDER , dbResult .getFirst ())).thenReturn (result );
188+
189+ final Pair <AuthorizationPolicyResponseDTO , Boolean > response = service .grantOperation ("TestProvider" , dto , "origin" );
190+
191+ assertEquals (result , response .getFirst ());
192+ assertTrue (response .getSecond ());
193+
194+ verify (validator ).validateAndNormalizeSystemName ("TestProvider" , "origin" );
195+ verify (validator ).validateAndNormalizeGrantRequest ("TestProvider" , dto , "origin" );
196+ verify (dbService ).createProviderLevelPolicy (normalized );
197+ verify (dtoConverter ).convertPolicyToResponse (AuthorizationLevel .PROVIDER , dbResult .getFirst ());
198+ }
199+ }
0 commit comments