Skip to content

Commit e784c26

Browse files
Merge branch '8.1.x' into master by GunalKupta
2 parents f0f76e6 + 609b04f commit e784c26

File tree

4 files changed

+312
-223
lines changed

4 files changed

+312
-223
lines changed

core/src/test/java/io/confluent/kafka/schemaregistry/ClusterTestHarness.java

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
import org.apache.kafka.common.utils.Java;
3838
import org.apache.kafka.common.utils.Time;
3939

40-
import java.io.IOException;
41-
import java.net.InetAddress;
42-
import java.net.ServerSocket;
4340
import java.util.List;
4441
import java.util.Properties;
4542

@@ -77,7 +74,7 @@
7774
*/
7875
@Tag("IntegrationTest")
7976
@DisplayNameGeneration(AddKraftQuorum.class)
80-
public abstract class ClusterTestHarness implements SchemaRegistryTestHarness {
77+
public class ClusterTestHarness implements SchemaRegistryTestHarness {
8178

8279
private static final Logger log = LoggerFactory.getLogger(ClusterTestHarness.class);
8380

@@ -86,38 +83,12 @@ public abstract class ClusterTestHarness implements SchemaRegistryTestHarness {
8683
public static final String KAFKASTORE_TOPIC = SchemaRegistryConfig.DEFAULT_KAFKASTORE_TOPIC;
8784
protected static final Optional<Properties> EMPTY_SASL_PROPERTIES = Optional.empty();
8885

89-
/**
90-
* Choose a number of random available ports
91-
*/
92-
public static int[] choosePorts(int count) {
93-
try {
94-
ServerSocket[] sockets = new ServerSocket[count];
95-
int[] ports = new int[count];
96-
for (int i = 0; i < count; i++) {
97-
sockets[i] = new ServerSocket(0, 0, InetAddress.getByName("0.0.0.0"));
98-
ports[i] = sockets[i].getLocalPort();
99-
}
100-
for (int i = 0; i < count; i++) {
101-
sockets[i].close();
102-
}
103-
return ports;
104-
} catch (IOException e) {
105-
throw new RuntimeException(e);
106-
}
107-
}
108-
109-
/**
110-
* Choose an available port
111-
*/
112-
@Override
113-
public int choosePort() {
114-
return choosePorts(1)[0];
115-
}
116-
11786
private final int numBrokers;
11887
private final boolean setupRestApp;
11988
protected String compatibilityType;
12089

90+
protected Properties schemaRegistryProperties;
91+
12192
// Quorum controller
12293
private TestInfo testInfo;
12394
private QuorumTestHarness quorumTestHarness;
@@ -147,6 +118,7 @@ public ClusterTestHarness(int numBrokers, boolean setupRestApp, String compatibi
147118
this.numBrokers = numBrokers;
148119
this.setupRestApp = setupRestApp;
149120
this.compatibilityType = compatibilityType;
121+
this.schemaRegistryProperties = new Properties();
150122
}
151123

152124
@BeforeEach
@@ -195,11 +167,28 @@ public void setupRestApp(Properties schemaRegistryProps) throws Exception {
195167
restApp.start();
196168
}
197169

170+
/**
171+
* Subclasses can override this method to provide custom schema registry properties
172+
* or add properties via {@link #injectSchemaRegistryProperties(Properties)}.
173+
* @return schema registry properties
174+
* @throws Exception if an error occurs
175+
*/
198176
public Properties getSchemaRegistryProperties() throws Exception {
199-
return new Properties();
177+
return schemaRegistryProperties;
200178
}
201179

202-
@Override
180+
/**
181+
* Inject properties into schema registry configuration.
182+
* @param props properties to inject
183+
*/
184+
public void injectSchemaRegistryProperties(Properties props) {
185+
this.schemaRegistryProperties.putAll(props);
186+
}
187+
188+
/**
189+
* Inject properties into broker configuration.
190+
* @param props properties to inject
191+
*/
203192
public void injectProperties(Properties props) {
204193
// Make sure that broker only role is "broker"
205194
props.setProperty("process.roles", "broker");
@@ -250,7 +239,10 @@ public Time brokerTime(int brokerId) {
250239
return Time.SYSTEM;
251240
}
252241

253-
@Override
242+
/**
243+
* Gets the broker list for Kafka connections.
244+
* @return broker list string, or null if not applicable
245+
*/
254246
public String getBrokerList() {
255247
return brokerList;
256248
}

core/src/test/java/io/confluent/kafka/schemaregistry/SchemaRegistryTestHarness.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515

1616
package io.confluent.kafka.schemaregistry;
1717

18-
import java.util.Properties;
18+
import java.io.IOException;
19+
import java.net.InetAddress;
20+
import java.net.ServerSocket;
1921

2022
/**
2123
* Interface defining common operations needed by schema registry integration tests.
22-
* This allows tests to be written against non-Kafka-based implementations.
24+
* This allows tests to be written and run against an arbitrary test harness.
2325
*/
2426
public interface SchemaRegistryTestHarness {
25-
26-
/**
27-
* Gets the broker list for Kafka connections.
28-
* @return broker list string, or null if not applicable
29-
*/
30-
String getBrokerList();
31-
3227
/**
3328
* Gets the REST application instance for making schema registry API calls.
3429
* @return RestApp instance
@@ -40,23 +35,40 @@ public interface SchemaRegistryTestHarness {
4035
* @return schema registry port number
4136
*/
4237
Integer getSchemaRegistryPort();
43-
38+
4439
/**
4540
* Chooses an available port for schema registry or other services.
4641
* @return available port number
4742
*/
48-
int choosePort();
43+
default int choosePort() {
44+
return choosePorts(1)[0];
45+
}
46+
47+
48+
/**
49+
* Choose a number of random available ports
50+
*/
51+
static int[] choosePorts(int count) {
52+
try {
53+
ServerSocket[] sockets = new ServerSocket[count];
54+
int[] ports = new int[count];
55+
for (int i = 0; i < count; i++) {
56+
sockets[i] = new ServerSocket(0, 0, InetAddress.getByName("0.0.0.0"));
57+
ports[i] = sockets[i].getLocalPort();
58+
}
59+
for (int i = 0; i < count; i++) {
60+
sockets[i].close();
61+
}
62+
return ports;
63+
} catch (IOException e) {
64+
throw new RuntimeException(e);
65+
}
66+
}
4967

5068
/**
5169
* Gets the schema registry protocol (http or https).
5270
* @return protocol string
5371
*/
5472
String getSchemaRegistryProtocol();
55-
56-
/**
57-
* Inject properties into broker configuration.
58-
* @param props properties to inject
59-
*/
60-
void injectProperties(Properties props);
6173
}
6274

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2018 Confluent Inc.
3+
*
4+
* Licensed under the Confluent Community License (the "License"); you may not use
5+
* this file except in compliance with the License. You may obtain a copy of the
6+
* License at
7+
*
8+
* http://www.confluent.io/confluent-community-license
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
13+
* specific language governing permissions and limitations under the License.
14+
*/
15+
16+
package io.confluent.kafka.schemaregistry.rest;
17+
18+
import io.confluent.kafka.schemaregistry.ClusterTestHarness;
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.TestInfo;
22+
23+
import java.util.Properties;
24+
25+
/**
26+
* ClusterTestHarness implementation of REST API integration tests.
27+
*/
28+
public class RestApiClusterTest extends RestApiTest {
29+
30+
protected ClusterTestHarness harness;
31+
32+
public RestApiClusterTest() {
33+
this.harness = new ClusterTestHarness(1, true);
34+
this.harness.injectSchemaRegistryProperties(getSchemaRegistryProperties());
35+
}
36+
37+
@BeforeEach
38+
public void setUpTest(TestInfo testInfo) throws Exception {
39+
harness.setUpTest(testInfo);
40+
setRestApp(harness.getRestApp());
41+
}
42+
43+
@AfterEach
44+
public void tearDown() throws Exception {
45+
harness.tearDown();
46+
}
47+
48+
public Properties getSchemaRegistryProperties() {
49+
Properties schemaRegistryProps = new Properties();
50+
schemaRegistryProps.put("response.http.headers.config",
51+
"add X-XSS-Protection: 1; mode=block, \"add Cache-Control: no-cache, no-store, must-revalidate\"");
52+
schemaRegistryProps.put("schema.providers.avro.validate.defaults", "true");
53+
return schemaRegistryProps;
54+
}
55+
}

0 commit comments

Comments
 (0)