5
5
import static org .mockito .BDDMockito .given ;
6
6
7
7
import java .util .Arrays ;
8
+ import java .util .Optional ;
8
9
9
10
import javax .inject .Inject ;
10
11
import javax .ws .rs .core .Response ;
13
14
import org .junit .jupiter .api .Test ;
14
15
import org .mockito .Mockito ;
15
16
17
+ import com .google .common .collect .Lists ;
16
18
import com .redhat .labs .lodestar .models .Engagement ;
17
19
import com .redhat .labs .lodestar .models .gitlab .Group ;
18
20
import com .redhat .labs .lodestar .models .gitlab .Namespace ;
@@ -35,28 +37,6 @@ class ProjectStructureServiceTest {
35
37
36
38
// New Customers and Projects
37
39
38
- // @Test
39
- // void testThis() {
40
- //
41
- // String customerName = "customer1";
42
- // String projectName = "project1";
43
- //
44
- // Group cGroup = Group.builder().name(customerName).path(GitLabPathUtils.generateValidPath(customerName))
45
- // .parentId(null).build();
46
- //
47
- // Group pGroup = Group.builder().name(projectName).path(GitLabPathUtils.generateValidPath(projectName))
48
- // .parentId(null).build();
49
- //
50
- //// when(groupService.createGitLabGroup(ArgumentMatchers.argThat((g) -> g.getName().equals(customerName)))).thenReturn(Optional.of(cGroup));
51
- //// when(groupService.createGitLabGroup(ArgumentMatchers.argThat((g) -> g.getName().equals(customerName)))).thenReturn(Optional.of(pGroup));
52
- // when(groupService.createGitLabGroup(Mockito.any(Group.class)))
53
- // .thenAnswer((i) -> Optional.of(i.getArgument(0, Group.class)));
54
- //
55
- // Optional<Group> group = psService.testThis(customerName);
56
- // System.out.print(group.orElse(null));
57
- //
58
- // }
59
-
60
40
@ Test
61
41
void testCreateProjectStructureForNewCustomerAndNewProject () {
62
42
@@ -91,6 +71,80 @@ void testCreateProjectStructureForNewCustomerAndNewProject() {
91
71
92
72
}
93
73
74
+ @ Test
75
+ void testCreateProjectStructureForCustomerNameChange () {
76
+
77
+ // given
78
+ String customerName = "customer1" ;
79
+ String projectName = "project1" ;
80
+
81
+ Group cGroup = Group .builder ().name (customerName ).path (GitLabPathUtils .generateValidPath (customerName )).id (2222 )
82
+ .parentId (2 ).build ();
83
+ Group pGroup = Group .builder ().name (projectName ).path (GitLabPathUtils .generateValidPath (projectName ))
84
+ .parentId (2222 ).id (3333 ).build ();
85
+ Project project = Project .builder ().id (4444 ).name ("iac" ).visibility ("private" )
86
+ .namespace (Namespace .builder ().id (3333 ).parentId (2222 ).build ()).build ();
87
+ Group newCustomerGroup = Group .builder ().name ("newCustomer1" )
88
+ .path (GitLabPathUtils .generateValidPath ("newCustomer1" )).id (2222 ).parentId (2 ).build ();
89
+
90
+ given (gitLabService .getProjectById (Mockito .anyString ())).willReturn (project );
91
+ given (gitLabService .getGroupByIdOrPath (Mockito .anyString ())).willReturn (pGroup , cGroup );
92
+ given (gitLabService .getSubGroups (Mockito .anyInt (), Mockito .eq (100 ), Mockito .eq (1 )))
93
+ .willReturn (Response .ok (Lists .newArrayList ()).header ("X-Total-Pages" , 1 ).build ());
94
+ given (gitLabService .updateGroup (Mockito .anyInt (), Mockito .any ())).willReturn (newCustomerGroup );
95
+
96
+ Engagement engagement = Engagement .builder ().customerName ("newCustomer1" ).projectName (projectName ).build ();
97
+
98
+ // when
99
+ Project actual = psService .createOrUpdateProjectStructure (engagement , "http://some-path/engagements" );
100
+
101
+ // then
102
+ assertNotNull (project );
103
+ assertEquals ("iac" , actual .getName ());
104
+ assertEquals ("private" , actual .getVisibility ());
105
+ assertEquals (3333 , actual .getNamespace ().getId ());
106
+ assertEquals (2222 , actual .getNamespace ().getParentId ());
107
+
108
+ }
109
+
110
+ @ Test
111
+ void testCreateProjectStructureForCustomerNameChangeCustomerHasMultipleProjects () {
112
+
113
+ // given
114
+ String customerName = "customer1" ;
115
+ String projectName = "project1" ;
116
+
117
+ Group cGroup = Group .builder ().name (customerName ).path (GitLabPathUtils .generateValidPath (customerName )).id (2222 )
118
+ .parentId (2 ).build ();
119
+ Group pGroup = Group .builder ().name (projectName ).path (GitLabPathUtils .generateValidPath (projectName ))
120
+ .parentId (2222 ).id (3333 ).build ();
121
+ Group anotherGroup = Group .builder ().name (projectName ).path (GitLabPathUtils .generateValidPath ("anotherGroup" ))
122
+ .parentId (2222 ).id (5555 ).build ();
123
+ Project project = Project .builder ().id (4444 ).name ("iac" ).visibility ("private" )
124
+ .namespace (Namespace .builder ().id (3333 ).parentId (2222 ).build ()).build ();
125
+ Group newCustomerGroup = Group .builder ().name ("newCustomer1" )
126
+ .path (GitLabPathUtils .generateValidPath ("newCustomer1" )).id (6666 ).parentId (2 ).build ();
127
+
128
+ given (gitLabService .getProjectById (Mockito .anyString ())).willReturn (project );
129
+ given (gitLabService .getGroupByIdOrPath (Mockito .anyString ())).willReturn (pGroup , cGroup );
130
+ given (gitLabService .getSubGroups (Mockito .anyInt (), Mockito .eq (100 ), Mockito .eq (1 )))
131
+ .willReturn (Response .ok (Lists .newArrayList (pGroup , anotherGroup )).header ("X-Total-Pages" , 1 ).build ());
132
+ given (gitLabService .updateGroup (Mockito .anyInt (), Mockito .any ())).willReturn (newCustomerGroup );
133
+ given (gitLabService .createGroup (Mockito .any (Group .class ))).willReturn (newCustomerGroup );
134
+ given (gitLabService .transferProject (Mockito .anyInt (), Mockito .any ())).willReturn (Optional .of (project ));
135
+
136
+ Engagement engagement = Engagement .builder ().customerName ("newCustomer1" ).projectName (projectName ).build ();
137
+
138
+ // when
139
+ Project actual = psService .createOrUpdateProjectStructure (engagement , "http://some-path/engagements" );
140
+
141
+ // then
142
+ assertNotNull (project );
143
+ assertEquals ("iac" , actual .getName ());
144
+ assertEquals ("private" , actual .getVisibility ());
145
+
146
+ }
147
+
94
148
}
95
149
96
150
//New Customers and Projects - 2/2
0 commit comments