11package org .opendevstack .apiservice .projectplatform .facade .impl ;
22
3+ import org .junit .jupiter .api .AfterEach ;
34import org .junit .jupiter .api .BeforeEach ;
45import org .junit .jupiter .api .Test ;
5- import org .junit .jupiter .api .extension .ExtendWith ;
6- import org .mockito .Mock ;
7- import org .mockito .junit .jupiter .MockitoExtension ;
86import org .opendevstack .apiservice .externalservice .projectsinfoservice .exception .ProjectsInfoServiceException ;
7+ import org .opendevstack .apiservice .externalservice .projectsinfoservice .model .PlatformSection ;
98import org .opendevstack .apiservice .externalservice .projectsinfoservice .model .Platforms ;
109import org .opendevstack .apiservice .externalservice .projectsinfoservice .service .ProjectsInfoService ;
1110import org .opendevstack .apiservice .projectplatform .exception .ProjectPlatformsException ;
1211import org .opendevstack .apiservice .projectplatform .mapper .ProjectPlatformsMapper ;
13- import org .opendevstack .apiservice .projectplatform .model .Link ;
1412import org .opendevstack .apiservice .projectplatform .model .ProjectPlatforms ;
15- import org .opendevstack . apiservice . projectplatform . model . Section ;
13+ import org .springframework . security . core . context . SecurityContextHolder ;
1614
1715import java .util .List ;
1816
19- import static org .junit .jupiter .api .Assertions .*;
20- import static org .mockito .Mockito .*;
17+ import static org .assertj .core .api .Assertions .assertThat ;
18+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
19+ import static org .mockito .Mockito .mock ;
20+ import static org .mockito .Mockito .verify ;
21+ import static org .mockito .Mockito .when ;
2122
22- @ ExtendWith (MockitoExtension .class )
2323class ProjectsFacadeImplTest {
2424
25- @ Mock
2625 private ProjectsInfoService projectsInfoService ;
27-
28- @ Mock
2926 private ProjectPlatformsMapper mapper ;
3027
31- private ProjectsFacadeImpl facade ;
28+ private ProjectsFacadeImpl sut ;
3229
3330 @ BeforeEach
34- void setUp () {
35- facade = new ProjectsFacadeImpl (projectsInfoService , mapper );
36- }
37-
38- @ Test
39- void givenAnyProjectKey_whenGetProjectPlatforms_thenGetMockProjectPlatforms () throws ProjectsInfoServiceException , ProjectPlatformsException {
40- // Arrange
41- String projectKey = "DEVSTACK" ;
31+ void setup () {
32+ projectsInfoService = mock (ProjectsInfoService .class );
33+ mapper = mock (ProjectPlatformsMapper .class );
4234
43- // Create external service response
44- Platforms externalPlatforms = new Platforms ();
35+ sut = new ProjectsFacadeImpl (projectsInfoService , mapper );
4536
46- // Create expected API response
47- ProjectPlatforms expectedPlatforms = createExpectedProjectPlatforms ();
37+ // Reset security context before each test
38+ SecurityContextHolder .clearContext ();
39+ }
4840
49- // Mock behavior
50- when (projectsInfoService .getProjectPlatforms (projectKey )).thenReturn (externalPlatforms );
51- when (mapper .toApiModel (externalPlatforms )).thenReturn (expectedPlatforms );
52-
53- // Act
54- ProjectPlatforms result = facade .getProjectPlatforms (projectKey );
55-
56- // Assert
57- assertNotNull (result , "Result should not be null" );
58-
59- List <Section > sections = result .getSections ();
60- assertNotNull (sections , "Sections should not be null" );
61- assertEquals (3 , sections .size (), "There should be 3 sections" );
62-
63- // Validate first section
64- Section appPlatformSection = sections .get (0 );
65- assertEquals ("Project Shortcuts - Application Platform" , appPlatformSection .getSection ());
66- assertEquals (4 , appPlatformSection .getLinks ().size ());
67- assertTrue (appPlatformSection .getLinks ().stream ().anyMatch (link -> link .getLabel ().equals ("JIRA" )));
68- assertTrue (appPlatformSection .getLinks ().stream ().allMatch (link -> link .getUrl ().equals ("https://www.google.com" )));
69-
70- // Validate second section
71- Section dataPlatformSection = sections .get (1 );
72- assertEquals ("Project Shortcuts - Data Platform" , dataPlatformSection .getSection ());
73- assertEquals (2 , dataPlatformSection .getLinks ().size ());
74-
75- // Validate third section
76- Section servicesSection = sections .get (2 );
77- assertEquals ("Services" , servicesSection .getSection ());
78- assertEquals (3 , servicesSection .getLinks ().size ());
79-
80- // Verify interactions
81- verify (projectsInfoService , times (1 )).getProjectPlatforms (projectKey );
82- verify (mapper , times (1 )).toApiModel (externalPlatforms );
41+ @ AfterEach
42+ void cleanup () {
43+ SecurityContextHolder .clearContext ();
8344 }
8445
8546 @ Test
86- void givenProjectsInfoServiceThrowsException_whenGetProjectPlatforms_thenRuntimeExceptionIsThrown () throws ProjectsInfoServiceException {
87- // Arrange
88- String projectKey = "DEVSTACK" ;
89- when (projectsInfoService .getProjectPlatforms (projectKey ))
90- .thenThrow (new ProjectsInfoServiceException ("Service error" ));
47+ void getProjectPlatforms_whenGetProjectPlatforms_thenReturnMappedApiModel () throws Exception {
48+ //given
49+ String projectKey = "PROJ" ;
9150
92- // Act & Assert
93- ProjectPlatformsException exception = assertThrows (ProjectPlatformsException .class , () -> facade .getProjectPlatforms (projectKey ));
51+ List <PlatformSection > sections = List .of ();
52+ Platforms externalPlatforms = new Platforms (sections );
53+ when (projectsInfoService .getProjectPlatforms (projectKey )).thenReturn (externalPlatforms );
9454
95- assertEquals ("Failed to retrieve project platforms" , exception .getMessage ());
96- verify (projectsInfoService , times (1 )).getProjectPlatforms (projectKey );
97- verify (mapper , never ()).toApiModel (any ());
98- }
55+ ProjectPlatforms mapped = new ProjectPlatforms ();
56+ when (mapper .toApiModel (externalPlatforms )).thenReturn (mapped );
9957
100- private ProjectPlatforms createExpectedProjectPlatforms () {
101- ProjectPlatforms platforms = new ProjectPlatforms ( );
58+ //when
59+ ProjectPlatforms result = sut . getProjectPlatforms ( projectKey );
10260
103- // Set sections
104- Section appPlatformSection = new Section ("Project Shortcuts - Application Platform" , "tooltip" , List .of (
105- new Link ("JIRA" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false ),
106- new Link ("Bitbucket" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false ),
107- new Link ("Confluence" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false ),
108- new Link ("Jenkins" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false )
109- ));
61+ //then
62+ verify (projectsInfoService ).getProjectPlatforms (projectKey );
63+ verify (mapper ).toApiModel (externalPlatforms );
11064
111- Section dataPlatformSection = new Section ("Project Shortcuts - Data Platform" , "tooltip" , List .of (
112- new Link ("EKG" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false ),
113- new Link ("EDGC" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false )
114- ));
65+ assertThat (result ).isSameAs (mapped );
66+ }
11567
116- Section servicesSection = new Section ("Services" , "tooltip" , List .of (
117- new Link ("Service Onboarding" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false ),
118- new Link ("Documentation" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false ),
119- new Link ("Service Training" , "https://www.google.com" , "tooltip" , "type" , "abbreviation" , false )
120- ));
68+ @ Test
69+ void getProjectPlatforms_whenInfoServiceThrowsException_thenWrapInProjectPlatformsException () throws Exception {
70+ //given
71+ String projectKey = "PROJ" ;
12172
122- platforms .setSections (List .of (appPlatformSection , dataPlatformSection , servicesSection ));
73+ when (projectsInfoService .getProjectPlatforms (projectKey ))
74+ .thenThrow (new ProjectsInfoServiceException ("boom" ));
12375
124- return platforms ;
76+ //when/then
77+ assertThatThrownBy (() -> sut .getProjectPlatforms (projectKey ))
78+ .isInstanceOf (ProjectPlatformsException .class )
79+ .hasMessageContaining ("Failed to retrieve project platforms" );
12580 }
126- }
12781
82+ }
0 commit comments