Open Kilda Java Documentation
TestUtils.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm;
17 
19 
20 import com.google.common.io.Files;
21 import kafka.server.KafkaConfig;
22 import kafka.server.KafkaServerStartable;
23 import org.apache.curator.test.TestingServer;
24 import org.apache.storm.Config;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.Properties;
29 
35 public class TestUtils {
36  public static Properties serverProperties(ZookeeperConfig config) {
37  Properties props = new Properties();
38  props.setProperty("zookeeper.connect", config.getHosts());
39  props.setProperty("broker.id", "1");
40  props.setProperty("delete.topic.enable", "true");
41  return props;
42  }
43 
44  public static Config stormConfig() {
45  Config config = new Config();
46  config.setDebug(false);
47  config.setNumWorkers(1);
48  return config;
49  }
50 
51  public static class KafkaTestFixture {
52  public TestingServer zk;
53  public KafkaServerStartable kafka;
54  public File tempDir = Files.createTempDir();
55  private ZookeeperConfig zooKeeperConfig;
56 
57  public KafkaTestFixture(ZookeeperConfig zooKeeperConfig) {
58  this.zooKeeperConfig = zooKeeperConfig;
59  }
60 
61  public void start() throws Exception {
62  Properties props = serverProperties(zooKeeperConfig);
63  this.start(props);
64  }
65 
66  public void start(Properties props) throws Exception {
67  Integer port = getZkPort(props);
68 
69  zk = new TestingServer(port, tempDir); // this starts the zk server
70  System.out.println("Started ZooKeeper: ");
71  System.out.println("--> Temp Directory: " + zk.getTempDirectory());
72 
73  props.put("log.dirs", tempDir.getAbsolutePath());
74  KafkaConfig kafkaConfig = new KafkaConfig(props);
75  kafka = new KafkaServerStartable(kafkaConfig);
76  kafka.startup();
77  System.out.println("Started KAFKA: ");
78  }
79 
80  public void stop() throws IOException {
81  kafka.shutdown();
82  zk.stop();
83  zk.close();
84  tempDir.delete();
85  }
86 
87  private int getZkPort(Properties properties) {
88  String url = (String) properties.get("zookeeper.connect");
89  String port = url.split(":")[1];
90  return Integer.valueOf(port);
91  }
92  }
93 }
static Config stormConfig()
Definition: TestUtils.java:44
static Properties serverProperties(ZookeeperConfig config)
Definition: TestUtils.java:36