18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
package org .opengrok .web .api .v1 .controller ;
24
24
25
+ import org .glassfish .grizzly .http .server .HttpServer ;
26
+ import org .glassfish .jersey .client .ClientConfig ;
27
+ import org .glassfish .jersey .client .ClientProperties ;
28
+ import org .glassfish .jersey .grizzly2 .httpserver .GrizzlyHttpServerFactory ;
25
29
import org .glassfish .jersey .server .ResourceConfig ;
30
+ import org .glassfish .jersey .test .DeploymentContext ;
26
31
import org .glassfish .jersey .test .JerseyTest ;
32
+ import org .glassfish .jersey .test .spi .TestContainer ;
33
+ import org .glassfish .jersey .test .spi .TestContainerException ;
34
+ import org .glassfish .jersey .test .spi .TestContainerFactory ;
27
35
import org .junit .After ;
28
36
import org .junit .Before ;
29
37
import org .junit .Test ;
35
43
import javax .ws .rs .client .Entity ;
36
44
import javax .ws .rs .core .Application ;
37
45
import javax .ws .rs .core .GenericType ;
46
+ import javax .ws .rs .core .MediaType ;
38
47
import javax .ws .rs .core .Response ;
48
+ import javax .ws .rs .core .UriBuilder ;
39
49
50
+ import java .io .IOException ;
40
51
import java .lang .reflect .Field ;
52
+ import java .net .URI ;
41
53
import java .time .Duration ;
42
54
import java .util .Arrays ;
43
55
import java .util .Collections ;
@@ -72,6 +84,67 @@ protected Application configure() {
72
84
return new ResourceConfig (MessagesController .class );
73
85
}
74
86
87
+ // Allow entity body for DELETE method on the client side.
88
+ @ Override
89
+ protected void configureClient (ClientConfig config ) {
90
+ config .property (ClientProperties .SUPPRESS_HTTP_COMPLIANCE_VALIDATION , true );
91
+ }
92
+
93
+ // Allow entity body for DELETE method on the server side.
94
+ public static class CustomGrizzlyTestContainerFactory implements TestContainerFactory {
95
+ public CustomGrizzlyTestContainerFactory () {
96
+ }
97
+
98
+ public TestContainer create (URI baseUri , DeploymentContext context ) {
99
+ return new GrizzlyTestContainer (baseUri , context );
100
+ }
101
+
102
+ private class GrizzlyTestContainer implements TestContainer {
103
+ private URI baseUri ;
104
+ private final HttpServer server ;
105
+
106
+ private GrizzlyTestContainer (URI baseUri , DeploymentContext context ) {
107
+ this .baseUri = UriBuilder .fromUri (baseUri ).path (context .getContextPath ()).build (new Object [0 ]);
108
+ this .server = GrizzlyHttpServerFactory .createHttpServer (this .baseUri , context .getResourceConfig (), false );
109
+ this .server .getServerConfiguration ().setAllowPayloadForUndefinedHttpMethods (true );
110
+ }
111
+
112
+ public ClientConfig getClientConfig () {
113
+ return null ;
114
+ }
115
+
116
+ public URI getBaseUri () {
117
+ return this .baseUri ;
118
+ }
119
+
120
+ public void start () {
121
+ if (this .server .isStarted ()) {
122
+ return ;
123
+ }
124
+
125
+ try {
126
+ this .server .start ();
127
+ if (this .baseUri .getPort () == 0 ) {
128
+ this .baseUri = UriBuilder .fromUri (this .baseUri ).port (this .server .getListener ("grizzly" ).getPort ()).build (new Object [0 ]);
129
+ }
130
+ } catch (IOException e ) {
131
+ throw new TestContainerException (e );
132
+ }
133
+ }
134
+
135
+ public void stop () {
136
+ if (this .server .isStarted ()) {
137
+ this .server .shutdownNow ();
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+ @ Override
144
+ protected TestContainerFactory getTestContainerFactory () throws TestContainerException {
145
+ return new CustomGrizzlyTestContainerFactory ();
146
+ }
147
+
75
148
@ Before
76
149
public void setupMessageListener () throws Exception {
77
150
setMessageContainer (env , new MessagesContainer ());
@@ -135,6 +208,15 @@ private void removeMessages(final String tag) {
135
208
.delete ();
136
209
}
137
210
211
+ private void removeMessages (final String tag , final String text ) {
212
+ Entity <String > requestEntity = Entity .entity (text , MediaType .TEXT_PLAIN );
213
+ target ("messages" )
214
+ .queryParam ("tag" , tag )
215
+ .request ()
216
+ .build ("DELETE" , requestEntity ).
217
+ invoke ();
218
+ }
219
+
138
220
@ Test
139
221
public void addAndRemoveTest () {
140
222
addMessage ("test" , "test" );
@@ -147,6 +229,24 @@ public void addAndRemoveTest() {
147
229
assertTrue (env .getMessages ("test" ).isEmpty ());
148
230
}
149
231
232
+ @ Test
233
+ public void addAndRemoveWithTextTest () {
234
+ final String tag = "foo" ;
235
+ final String text = "text" ;
236
+
237
+ addMessage (text , tag );
238
+ assertEquals (1 , env .getMessages (tag ).size ());
239
+
240
+ removeMessages (tag + "bar" , text );
241
+ assertEquals (1 , env .getMessages (tag ).size ());
242
+
243
+ removeMessages (tag , text + "bar" );
244
+ assertEquals (1 , env .getMessages (tag ).size ());
245
+
246
+ removeMessages (tag , text );
247
+ assertTrue (env .getMessages (tag ).isEmpty ());
248
+ }
249
+
150
250
@ Test
151
251
public void addAndRemoveDifferentTagsTest () {
152
252
addMessage ("test" , "tag1" );
0 commit comments