42
42
import sonia .scm .repository .RepositoryTestData ;
43
43
import sonia .scm .repository .api .HookChangesetBuilder ;
44
44
import sonia .scm .repository .api .HookContext ;
45
+ import sonia .scm .repository .api .HookFeature ;
46
+ import sonia .scm .repository .api .HookMessageProvider ;
45
47
46
- import java .util .Collections ;
47
48
import java .util .Optional ;
48
49
50
+ import static java .util .Arrays .asList ;
51
+ import static java .util .Collections .emptyList ;
49
52
import static org .assertj .core .api .Assertions .assertThat ;
53
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
50
54
import static org .mockito .ArgumentMatchers .anyString ;
51
55
import static org .mockito .ArgumentMatchers .eq ;
56
+ import static org .mockito .Mockito .doThrow ;
57
+ import static org .mockito .Mockito .mock ;
52
58
import static org .mockito .Mockito .never ;
53
59
import static org .mockito .Mockito .verify ;
54
60
import static org .mockito .Mockito .when ;
@@ -77,7 +83,7 @@ class CommitMessageCheckerHookTest {
77
83
@ Test
78
84
void shouldNotValidateIfNoValidationsFound () {
79
85
when (configurationProvider .evaluateConfiguration (REPOSITORY ))
80
- .thenReturn (Optional .of (new Configuration (true , Collections . emptyList ())));
86
+ .thenReturn (Optional .of (new Configuration (true , emptyList ())));
81
87
82
88
PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent (new RepositoryHookEvent (hookContext , REPOSITORY , RepositoryHookType .PRE_RECEIVE ));
83
89
hook .onEvent (event );
@@ -87,7 +93,7 @@ void shouldNotValidateIfNoValidationsFound() {
87
93
88
94
@ Test
89
95
void shouldNotValidateIfConfigurationIsDisabled () {
90
- when (configurationProvider .evaluateConfiguration (REPOSITORY )).thenReturn (Optional .of (new Configuration (false , Collections . emptyList ())));
96
+ when (configurationProvider .evaluateConfiguration (REPOSITORY )).thenReturn (Optional .of (new Configuration (false , emptyList ())));
91
97
92
98
PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent (new RepositoryHookEvent (hookContext , REPOSITORY , RepositoryHookType .PRE_RECEIVE ));
93
99
hook .onEvent (event );
@@ -97,14 +103,7 @@ void shouldNotValidateIfConfigurationIsDisabled() {
97
103
98
104
@ Test
99
105
void shouldValidate () {
100
- Object configuration = new Object ();
101
- when (configurationProvider .evaluateConfiguration (REPOSITORY ))
102
- .thenReturn (Optional .of (new Configuration (true , ImmutableList .of (new Validation ("TestValidator" , configuration )))));
103
- when (hookContext .getChangesetProvider ()).thenReturn (hookChangesetBuilder );
104
- Changeset changeset = new Changeset ("1" , 1L , null , "awesome commit" );
105
- changeset .setBranches (ImmutableList .of ("master" ));
106
- when (hookChangesetBuilder .getChangesetList ()).thenReturn (ImmutableList .of (changeset ));
107
- when (availableValidators .validatorFor ("TestValidator" )).thenReturn (validator );
106
+ Object configuration = prepareValidator ("master" );
108
107
109
108
PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent (new RepositoryHookEvent (hookContext , REPOSITORY , RepositoryHookType .PRE_RECEIVE ));
110
109
hook .onEvent (event );
@@ -119,13 +118,7 @@ void shouldValidate() {
119
118
120
119
@ Test
121
120
void shouldValidateChangesetWithoutBranches () {
122
- Object configuration = new Object ();
123
- when (configurationProvider .evaluateConfiguration (REPOSITORY ))
124
- .thenReturn (Optional .of (new Configuration (true , ImmutableList .of (new Validation ("TestValidator" , configuration )))));
125
- when (hookContext .getChangesetProvider ()).thenReturn (hookChangesetBuilder );
126
- Changeset changeset = new Changeset ("1" , 1L , null , "awesome commit" );
127
- when (hookChangesetBuilder .getChangesetList ()).thenReturn (ImmutableList .of (changeset ));
128
- when (availableValidators .validatorFor ("TestValidator" )).thenReturn (validator );
121
+ Object configuration = prepareValidator ();
129
122
130
123
PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent (new RepositoryHookEvent (hookContext , REPOSITORY , RepositoryHookType .PRE_RECEIVE ));
131
124
hook .onEvent (event );
@@ -137,4 +130,31 @@ void shouldValidateChangesetWithoutBranches() {
137
130
assertThat (context .getRepository ()).isEqualTo (REPOSITORY );
138
131
assertThat (context .getConfiguration ()).isEqualTo (configuration );
139
132
}
133
+
134
+ @ Test
135
+ void shouldSendContextMessage () {
136
+ when (hookContext .isFeatureSupported (HookFeature .MESSAGE_PROVIDER )).thenReturn (true );
137
+ HookMessageProvider messageProvider = mock (HookMessageProvider .class );
138
+ when (hookContext .getMessageProvider ()).thenReturn (messageProvider );
139
+
140
+ prepareValidator ("master" );
141
+ doThrow (RuntimeException .class ).when (validator ).validate (contextCaptor .capture (), eq ("awesome commit" ));
142
+
143
+ PreReceiveRepositoryHookEvent event = new PreReceiveRepositoryHookEvent (new RepositoryHookEvent (hookContext , REPOSITORY , RepositoryHookType .PRE_RECEIVE ));
144
+ assertThrows (RuntimeException .class , () -> hook .onEvent (event ));
145
+
146
+ verify (messageProvider ).sendError ("Commit message validation:" );
147
+ }
148
+
149
+ private Object prepareValidator (String ... branches ) {
150
+ Object configuration = new Object ();
151
+ when (configurationProvider .evaluateConfiguration (REPOSITORY ))
152
+ .thenReturn (Optional .of (new Configuration (true , ImmutableList .of (new Validation ("TestValidator" , configuration )))));
153
+ when (hookContext .getChangesetProvider ()).thenReturn (hookChangesetBuilder );
154
+ Changeset changeset = new Changeset ("1" , 1L , null , "awesome commit" );
155
+ when (hookChangesetBuilder .getChangesetList ()).thenReturn (ImmutableList .of (changeset ));
156
+ when (availableValidators .validatorFor ("TestValidator" )).thenReturn (validator );
157
+ changeset .setBranches (asList (branches ));
158
+ return configuration ;
159
+ }
140
160
}
0 commit comments