1818 */
1919
2020/*
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.
2222 */
2323package org .opengrok .web .api .v1 .controller ;
2424
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 ;
2529import org .glassfish .jersey .server .ResourceConfig ;
30+ import org .glassfish .jersey .test .DeploymentContext ;
2631import 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 ;
2735import org .junit .After ;
2836import org .junit .Before ;
2937import org .junit .Test ;
3543import javax .ws .rs .client .Entity ;
3644import javax .ws .rs .core .Application ;
3745import javax .ws .rs .core .GenericType ;
46+ import javax .ws .rs .core .MediaType ;
3847import javax .ws .rs .core .Response ;
48+ import javax .ws .rs .core .UriBuilder ;
3949
50+ import java .io .IOException ;
4051import java .lang .reflect .Field ;
52+ import java .net .URI ;
4153import java .time .Duration ;
4254import java .util .Arrays ;
4355import java .util .Collections ;
@@ -72,6 +84,67 @@ protected Application configure() {
7284 return new ResourceConfig (MessagesController .class );
7385 }
7486
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+
75148 @ Before
76149 public void setupMessageListener () throws Exception {
77150 setMessageContainer (env , new MessagesContainer ());
@@ -135,6 +208,15 @@ private void removeMessages(final String tag) {
135208 .delete ();
136209 }
137210
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+
138220 @ Test
139221 public void addAndRemoveTest () {
140222 addMessage ("test" , "test" );
@@ -147,6 +229,24 @@ public void addAndRemoveTest() {
147229 assertTrue (env .getMessages ("test" ).isEmpty ());
148230 }
149231
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+
150250 @ Test
151251 public void addAndRemoveDifferentTagsTest () {
152252 addMessage ("test" , "tag1" );
0 commit comments