diff options
Diffstat (limited to 'src')
5 files changed, 314 insertions, 182 deletions
diff --git a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/MirrorMakerAgent.java b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/MirrorMakerAgent.java index becfbc5..86a653d 100644 --- a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/MirrorMakerAgent.java +++ b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/MirrorMakerAgent.java @@ -34,9 +34,9 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Properties; -//import org.json.JSONObject; -//import org.apache.log4j.Logger; -//import org.jasypt.util.text.BasicTextEncryptor; +import org.json.JSONObject; +import org.apache.log4j.Logger; +import org.jasypt.util.text.BasicTextEncryptor; import org.onap.dmaap.mr.dmaapMMAgent.dao.CreateMirrorMaker; import org.onap.dmaap.mr.dmaapMMAgent.dao.DeleteMirrorMaker; @@ -48,10 +48,11 @@ import org.onap.dmaap.mr.dmaapMMAgent.utils.MirrorMakerProcessHandler; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.internal.LinkedTreeMap; + import com.sun.org.apache.xerces.internal.impl.dtd.models.CMAny; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; -public class MirrorMakerAgent {/* +public class MirrorMakerAgent { static final Logger logger = Logger.getLogger(MirrorMakerAgent.class); Properties mirrorMakerProperties = new Properties(); ListMirrorMaker mirrorMakers = null; @@ -62,6 +63,8 @@ public class MirrorMakerAgent {/* String mechid = ""; String password = ""; String grepLog = ""; + public boolean exitLoop = false; + TopicUtil topicUtil = new TopicUtil(); private static String secret = "utdfpWlgyDQ2ZB8SLVRtmN834I1JcT9J"; public static void main(String[] args) { @@ -84,7 +87,7 @@ public class MirrorMakerAgent {/* try { agent.checkAgentProcess(); } catch (Exception e) { - + e.printStackTrace(); } agent.readAgentTopic(); @@ -129,14 +132,14 @@ public class MirrorMakerAgent {/* } } } - String response = publishTopic("{\"test\":\"test\"}"); + String response = topicUtil.publishTopic(topicURL, topicname, mechid, password, "{\"test\":\"test\"}"); if (response.startsWith("ERROR:")) { logger.error("Problem publishing to topic, please verify the config " + this.topicname + " MR URL is:" + this.topicURL + " Error is: " + response); return false; } logger.info("Published to Topic :" + this.topicname + " Successfully"); - response = subscribeTopic("1"); + response = topicUtil.subscribeTopic(topicURL, topicname, "1", response, response); if (response != null && response.startsWith("ERROR:")) { logger.error("Problem subscribing to topic, please verify the config " + this.topicname + " MR URL is:" + this.topicURL + " Error is: " + response); @@ -160,14 +163,14 @@ public class MirrorMakerAgent {/* Properties prop = new Properties(); prop.load(input); if (propName.equals("consumer")) { - prop.setProperty("group.id", mm.name); - - prop.setProperty("bootstrap.servers", mm.consumer); - prop.setProperty("client.id", mm.name + "MM_consumer"); - } else { + prop.setProperty("group.id", mm.name); + + prop.setProperty("bootstrap.servers", mm.consumer); + prop.setProperty("client.id", mm.name + "MM_consumer"); + } else { prop.setProperty("bootstrap.servers", mm.producer); - prop.setProperty("client.id", mm.name + "MM_producer"); - + prop.setProperty("client.id", mm.name + "MM_producer"); + } out = new FileOutputStream(mmagenthome + "/etc/" + mm.name + propName + ".properties"); prop.store(out, ""); @@ -199,15 +202,17 @@ public class MirrorMakerAgent {/* int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size(); for (int i = 0; i < mirrorMakersCount; i++) { MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i); - if (MirrorMakerProcessHandler.checkMirrorMakerProcess(mm.name,mm.enablelogCheck,this.grepLog) == false) { + if (MirrorMakerProcessHandler.checkMirrorMakerProcess(mm.name, mm.enablelogCheck, + this.grepLog) == false) { checkPropertiesFile(mm, "consumer", false); checkPropertiesFile(mm, "producer", false); if (mm.whitelist != null && !mm.whitelist.equals("")) { - logger.info("MirrorMaker " + mm.name + " is not running, restarting. Check Logs for more Details"); + logger.info( + "MirrorMaker " + mm.name + " is not running, restarting. Check Logs for more Details"); MirrorMakerProcessHandler.startMirrorMaker(this.mmagenthome, this.kafkahome, mm.name, mmagenthome + "/etc/" + mm.name + "consumer.properties", - mmagenthome + "/etc/" + mm.name + "producer.properties",mm.numStreams, mm.whitelist); + mmagenthome + "/etc/" + mm.name + "producer.properties", mm.numStreams, mm.whitelist); mm.setStatus("RESTARTING"); } else { @@ -230,142 +235,28 @@ public class MirrorMakerAgent {/* // System.out.println(g.toJson(mirrorMakers)); } - private String subscribeTopic(String timeout) { - String response = ""; - try { - String requestURL = this.topicURL + "/events/" + this.topicname + "/mirrormakeragent/1?timeout=" + timeout - + "&limit=1"; - String authString = this.mechid + ":" + this.password; - String authStringEnc = Base64.encode(authString.getBytes()); - URL url = new URL(requestURL); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - connection.setDoOutput(true); - connection.setRequestProperty("Authorization", "Basic " + authStringEnc); - connection.setRequestProperty("Content-Type", "application/json"); - InputStream content = (InputStream) connection.getInputStream(); - BufferedReader in = new BufferedReader(new InputStreamReader(content)); - String line; - - while ((line = in.readLine()) != null) { - response = response + line; - } - Gson g = new Gson(); - //Get message as JSON Array - JsonArray topicMessage = g.fromJson(response, JsonArray.class); - if (topicMessage.size() != 0) { - return topicMessage.get(0).toString(); - } - - // get message as JSON String Array - String[] topicMessage = g.fromJson(response, String[].class); - if (topicMessage.length != 0) { - return topicMessage[0]; - } - } catch (Exception e) { - return "ERROR:" + e.getMessage() + " Server Response is:" + response; - } - return null; - } - - private String publishTopic(String message) { - try { - String requestURL = this.topicURL + "/events/" + this.topicname; - String authString = this.mechid + ":" + this.password; - String authStringEnc = Base64.encode(authString.getBytes()); - URL url = new URL(requestURL); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setDoOutput(true); - connection.setRequestProperty("Authorization", "Basic " + authStringEnc); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestProperty("Content-Length", Integer.toString(message.length())); - DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); - wr.write(message.getBytes()); - - InputStream content = (InputStream) connection.getInputStream(); - BufferedReader in = new BufferedReader(new InputStreamReader(content)); - String line; - String response = ""; - while ((line = in.readLine()) != null) { - response = response + line; - } - return response; - - } catch (Exception e) { - return "ERROR:" + e.getLocalizedMessage(); - } - } - - private void readAgentTopic() { + public void readAgentTopic() { try { int connectionattempt = 0; while (true) { logger.info("--------------------------------"); logger.info("Waiting for Messages for 60 secs"); - String topicMessage = subscribeTopic("60000"); + String topicMessage = topicUtil.subscribeTopic(topicURL, topicname, "60000", mechid, password); Gson g = new Gson(); LinkedTreeMap<?, ?> object = null; if (topicMessage != null) { try { - //Check and parse if String object returned by consumer API - //else use the jsonObject - if( topicMessage.startsWith("\"")) - { - topicMessage = g.fromJson(topicMessage.toString(), String.class); - } + // Check and parse if String object returned by consumer + // API + // else use the jsonObject + if (topicMessage.startsWith("\"")) { + topicMessage = g.fromJson(topicMessage.toString(), String.class); + } object = g.fromJson(topicMessage, LinkedTreeMap.class); // Cast the 1st item (since limit=1 and see the type of // object - if (object.get("createMirrorMaker") != null) { - logger.info("Received createMirrorMaker request from topic"); - CreateMirrorMaker m = g.fromJson(topicMessage, CreateMirrorMaker.class); - createMirrorMaker(m.getCreateMirrorMaker()); - checkAgentProcess(); - mirrorMakers.setMessageID(m.getMessageID()); - publishTopic(g.toJson(mirrorMakers)); - mirrorMakers.setMessageID(""); - } else if (object.get("updateMirrorMaker") != null) { - logger.info("Received updateMirrorMaker request from topic"); - UpdateMirrorMaker m = g.fromJson(topicMessage, UpdateMirrorMaker.class); - JSONObject json = new JSONObject(topicMessage); - JSONObject json2 = (JSONObject) json.get("updateMirrorMaker"); - if(!json2.has("numStreams")){ - m.getUpdateMirrorMaker().setNumStreams(0); - } - updateMirrorMaker(m.getUpdateMirrorMaker()); - checkAgentProcess(); - mirrorMakers.setMessageID(m.getMessageID()); - publishTopic(g.toJson(mirrorMakers)); - mirrorMakers.setMessageID(""); - } else if (object.get("deleteMirrorMaker") != null) { - logger.info("Received deleteMirrorMaker request from topic"); - DeleteMirrorMaker m = g.fromJson(topicMessage, DeleteMirrorMaker.class); - deleteMirrorMaker(m.getDeleteMirrorMaker()); - checkAgentProcess(); - mirrorMakers.setMessageID(m.getMessageID()); - publishTopic(g.toJson(mirrorMakers)); - mirrorMakers.setMessageID(""); - } else if (object.get("listAllMirrorMaker") != null) { - logger.info("Received listALLMirrorMaker request from topic"); - checkAgentProcess(); - mirrorMakers.setMessageID((String) object.get("messageID")); - publishTopic(g.toJson(mirrorMakers)); - mirrorMakers.setMessageID(""); - } else if (object.get("updateWhiteList") != null) { - logger.info("Received updateWhiteList request from topic"); - UpdateWhiteList m = g.fromJson(topicMessage, UpdateWhiteList.class); - updateWhiteList(m.getUpdateWhiteList()); - checkAgentProcess(); - mirrorMakers.setMessageID(m.getMessageID()); - publishTopic(g.toJson(mirrorMakers)); - mirrorMakers.setMessageID(""); - } else if (object.get("listMirrorMaker") != null) { - logger.info("Received listMirrorMaker from topic, skipping messages"); - } else { - logger.info("Received unknown request from topic"); - } + readAgent(object, topicMessage); } catch (Exception ex) { connectionattempt++; if (connectionattempt > 5) { @@ -381,6 +272,9 @@ public class MirrorMakerAgent {/* connectionattempt = 0; checkAgentProcess(); } + if (exitLoop) { + break; + } } } catch (Exception e) { @@ -389,7 +283,7 @@ public class MirrorMakerAgent {/* } - private void createMirrorMaker(MirrorMaker newMirrorMaker) { + public void createMirrorMaker(MirrorMaker newMirrorMaker) { boolean exists = false; if (mirrorMakers != null) { int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size(); @@ -412,8 +306,8 @@ public class MirrorMakerAgent {/* list.add(newMirrorMaker); mirrorMakers.setListMirrorMaker(list); } - checkPropertiesFile(newMirrorMaker, "consumer", true); - checkPropertiesFile(newMirrorMaker, "producer", true); + checkPropertiesFile(newMirrorMaker, "consumer", true); + checkPropertiesFile(newMirrorMaker, "producer", true); Gson g = new Gson(); mirrorMakerProperties.setProperty("mirrormakers", g.toJson(this.mirrorMakers)); @@ -442,23 +336,20 @@ public class MirrorMakerAgent {/* MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i); if (mm.name.equals(newMirrorMaker.name)) { exists = true; - if(null!=newMirrorMaker.getConsumer()) - { + if (null != newMirrorMaker.getConsumer()) { mm.setConsumer(newMirrorMaker.getConsumer()); } - if(null!=newMirrorMaker.getProducer()) - { + if (null != newMirrorMaker.getProducer()) { mm.setProducer(newMirrorMaker.getProducer()); } - if(newMirrorMaker.getNumStreams()>=1) - { + if (newMirrorMaker.getNumStreams() >= 1) { mm.setNumStreams(newMirrorMaker.getNumStreams()); - } - + } + mm.setEnablelogCheck(newMirrorMaker.enablelogCheck); - + mirrorMakers.getListMirrorMaker().set(i, mm); - newMirrorMaker=mm; + newMirrorMaker = mm; logger.info("Updating MirrorMaker:" + newMirrorMaker.name); } } @@ -608,12 +499,12 @@ public class MirrorMakerAgent {/* this.topicURL = mirrorMakerProperties.getProperty("topicURL"); this.topicname = mirrorMakerProperties.getProperty("topicname"); this.mechid = mirrorMakerProperties.getProperty("mechid"); - this.grepLog= mirrorMakerProperties.getProperty("grepLog"); + this.grepLog = mirrorMakerProperties.getProperty("grepLog"); BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); textEncryptor.setPassword(secret); this.password = textEncryptor.decrypt(mirrorMakerProperties.getProperty("password")); - } catch (IOException ex) { + } catch (Exception ex) { // ex.printStackTrace(); } finally { if (input != null) { @@ -626,4 +517,59 @@ public class MirrorMakerAgent {/* } } -*/} + + public void readAgent(LinkedTreeMap<?, ?> object, String topicMessage) throws Exception{ + + Gson g = new Gson(); + + if (object.get("createMirrorMaker") != null) { + logger.info("Received createMirrorMaker request from topic"); + CreateMirrorMaker m = g.fromJson(topicMessage, CreateMirrorMaker.class); + createMirrorMaker(m.getCreateMirrorMaker()); + checkAgentProcess(); + mirrorMakers.setMessageID(m.getMessageID()); + topicUtil.publishTopic(topicURL, topicname, mechid, password, g.toJson(mirrorMakers)); + mirrorMakers.setMessageID(""); + } else if (object.get("updateMirrorMaker") != null) { + logger.info("Received updateMirrorMaker request from topic"); + UpdateMirrorMaker m = g.fromJson(topicMessage, UpdateMirrorMaker.class); + JSONObject json = new JSONObject(topicMessage); + JSONObject json2 = (JSONObject) json.get("updateMirrorMaker"); + if (!json2.has("numStreams")) { + m.getUpdateMirrorMaker().setNumStreams(0); + } + updateMirrorMaker(m.getUpdateMirrorMaker()); + checkAgentProcess(); + mirrorMakers.setMessageID(m.getMessageID()); + topicUtil.publishTopic(topicURL, topicname, mechid, password, g.toJson(mirrorMakers)); + mirrorMakers.setMessageID(""); + } else if (object.get("deleteMirrorMaker") != null) { + logger.info("Received deleteMirrorMaker request from topic"); + DeleteMirrorMaker m = g.fromJson(topicMessage, DeleteMirrorMaker.class); + deleteMirrorMaker(m.getDeleteMirrorMaker()); + checkAgentProcess(); + mirrorMakers.setMessageID(m.getMessageID()); + topicUtil.publishTopic(topicURL, topicname, mechid, password, g.toJson(mirrorMakers)); + mirrorMakers.setMessageID(""); + } else if (object.get("listAllMirrorMaker") != null) { + logger.info("Received listALLMirrorMaker request from topic"); + checkAgentProcess(); + mirrorMakers.setMessageID((String) object.get("messageID")); + topicUtil.publishTopic(topicURL, topicname, mechid, password, g.toJson(mirrorMakers)); + mirrorMakers.setMessageID(""); + } else if (object.get("updateWhiteList") != null) { + logger.info("Received updateWhiteList request from topic"); + UpdateWhiteList m = g.fromJson(topicMessage, UpdateWhiteList.class); + updateWhiteList(m.getUpdateWhiteList()); + checkAgentProcess(); + mirrorMakers.setMessageID(m.getMessageID()); + topicUtil.publishTopic(topicURL, topicname, mechid, password, g.toJson(mirrorMakers)); + mirrorMakers.setMessageID(""); + } else if (object.get("listMirrorMaker") != null) { + logger.info("Received listMirrorMaker from topic, skipping messages"); + } else { + logger.info("Received unknown request from topic"); + } + + } +} diff --git a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java new file mode 100644 index 0000000..e2dc0ef --- /dev/null +++ b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ + +package org.onap.dmaap.mr.dmaapMMAgent; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.apache.log4j.Logger; + +import com.google.gson.Gson; +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; + +public class TopicUtil { + + static final Logger logger = Logger.getLogger(TopicUtil.class); + + public String publishTopic(String topicURL, String topicname, String mechid, String password, String message) { + try { + String requestURL = topicURL + "/events/" + topicname; + String authString = mechid + ":" + password; + String authStringEnc = Base64.encode(authString.getBytes()); + URL url = new URL(requestURL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setRequestProperty("Authorization", "Basic " + authStringEnc); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Content-Length", Integer.toString(message.length())); + DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); + wr.write(message.getBytes()); + + InputStream content = (InputStream) connection.getInputStream(); + BufferedReader in = new BufferedReader(new InputStreamReader(content)); + String line; + String response = ""; + while ((line = in.readLine()) != null) { + response = response + line; + } + return response; + + } catch (Exception e) { + logger.error(" Exception Occered " + e); + return "ERROR:" + e.getLocalizedMessage(); + } + } + + public String subscribeTopic(String topicURL, String topicname, String timeout, String mechid, String password) { + String response = ""; + try { + String requestURL = topicURL + "/events/" + topicname + "/mirrormakeragent/1?timeout=" + timeout + + "&limit=1"; + String authString = mechid + ":" + password; + String authStringEnc = Base64.encode(authString.getBytes()); + URL url = new URL(requestURL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setDoOutput(true); + connection.setRequestProperty("Authorization", "Basic " + authStringEnc); + connection.setRequestProperty("Content-Type", "application/json"); + InputStream content = (InputStream) connection.getInputStream(); + BufferedReader in = new BufferedReader(new InputStreamReader(content)); + String line; + + while ((line = in.readLine()) != null) { + response = response + line; + } + Gson g = new Gson(); + // get message as JSON String Array + String[] topicMessage = g.fromJson(response, String[].class); + if (topicMessage.length != 0) { + return topicMessage[0]; + } + } catch (Exception e) { + logger.error(" Exception Occered " + e); + return "ERROR:" + e.getMessage() + " Server Response is:" + response; + } + return null; + } + +} diff --git a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/dao/CreateMirrorMaker.java b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/dao/CreateMirrorMaker.java index 4d1426a..a1809f8 100644 --- a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/dao/CreateMirrorMaker.java +++ b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/dao/CreateMirrorMaker.java @@ -23,14 +23,14 @@ package org.onap.dmaap.mr.dmaapMMAgent.dao; public class CreateMirrorMaker { String messageID; - MirrorMaker MirrorMaker; + MirrorMaker createMirrorMaker; public MirrorMaker getCreateMirrorMaker() { - return MirrorMaker; + return createMirrorMaker; } public void setCreateMirrorMaker(MirrorMaker createMirrorMaker) { - this.MirrorMaker = createMirrorMaker; + this.createMirrorMaker = createMirrorMaker; } public String getMessageID() { diff --git a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/utils/MirrorMakerProcessHandler.java b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/utils/MirrorMakerProcessHandler.java index 82086a3..21a3974 100644 --- a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/utils/MirrorMakerProcessHandler.java +++ b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/utils/MirrorMakerProcessHandler.java @@ -27,7 +27,7 @@ import java.io.InputStreamReader; import org.apache.log4j.Logger; -public class MirrorMakerProcessHandler {/* +public class MirrorMakerProcessHandler { static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class); static String mmagenthome = System.getProperty("MMAGENTHOME"); @@ -197,4 +197,4 @@ public class MirrorMakerProcessHandler {/* e.printStackTrace(); } } -*/} +} diff --git a/src/test/java/org/onap/dmaap/mr/dmaapMMAgent/TestMirrorMakerAgent.java b/src/test/java/org/onap/dmaap/mr/dmaapMMAgent/TestMirrorMakerAgent.java index def9f39..f7b14d4 100644 --- a/src/test/java/org/onap/dmaap/mr/dmaapMMAgent/TestMirrorMakerAgent.java +++ b/src/test/java/org/onap/dmaap/mr/dmaapMMAgent/TestMirrorMakerAgent.java @@ -23,43 +23,112 @@ package org.onap.dmaap.mr.dmaapMMAgent; import static org.junit.Assert.*; +import static org.mockito.Mockito.spy; -import java.io.DataOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.net.HttpURLConnection; import java.util.ArrayList; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.Mock; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.modules.junit4.PowerMockRunner; -import org.onap.dmaap.mr.dmaapMMAgent.dao.ListMirrorMaker; -import org.onap.dmaap.mr.dmaapMMAgent.dao.MirrorMaker; +import org.onap.dmaap.mr.dmaapMMAgent.dao.ListMirrorMaker; +import org.onap.dmaap.mr.dmaapMMAgent.dao.MirrorMaker; +import com.google.gson.Gson; +import com.google.gson.internal.LinkedTreeMap; + +@RunWith(PowerMockRunner.class) +public class TestMirrorMakerAgent { -//@RunWith(PowerMockRunner.class) -public class TestMirrorMakerAgent {/* MirrorMakerAgent mirrorMakerAgent = new MirrorMakerAgent(); ListMirrorMaker listMirrorMaker = new ListMirrorMaker(); MirrorMaker mirrorMaker = new MirrorMaker(); MirrorMaker mirrorMaker2 = new MirrorMaker(); ArrayList<MirrorMaker> listsMirrorMaker = new ArrayList<MirrorMaker>(); + Gson g = new Gson(); + @InjectMocks + private MirrorMakerAgent agent; + @Mock + private TopicUtil topicUtil; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } @Test public void testcheckStartup() { String currentDirectory = System.getProperty("user.dir"); String MMAGENTHOME = currentDirectory + "/src/test/resources/"; - String parameters[] = {"-encrypt", "test"}; + String parameters[] = { "-encrypt", "test" }; String args[] = null; - + System.setProperty("MMAGENTHOME", MMAGENTHOME); - mirrorMakerAgent.main(args); - + + } + + @Test + public void testReadAgentTopics() { + + agent.exitLoop = true; + agent.readAgentTopic(); + } + + @Test + public void testReadCreateMirrorMaker() throws Exception { + + String topicMessage = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class); + agent.exitLoop = true; + agent.readAgent(object, topicMessage); + + } + + @Test + public void testReadUpdateMirrorMaker() throws Exception { + + String topicMessage = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class); + testReadCreateMirrorMaker(); + agent.readAgent(object, topicMessage); + } - + + @Test + public void testReadDeleteMirrorMaker() throws Exception { + + String topicMessage = "{ messageID:\"test\", deleteMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; + LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class); + testReadCreateMirrorMaker(); + agent.readAgent(object, topicMessage); + + } + + @Test + public void testReadListMirrorMaker() throws Exception { + + String topicMessage = "{ messageID:\"test\", listAllMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; + LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class); + testReadCreateMirrorMaker(); + agent.readAgent(object, topicMessage); + + } + + @Test + public void testReadWhitelistMirrorMaker() throws Exception { + + String topicMessage = "{ messageID:\"test\", updateWhiteList: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; + LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class); + testReadCreateMirrorMaker(); + agent.readAgent(object, topicMessage); + + } + @Test public void testCreateMirrorMaker() { mirrorMaker.setConsumer("consumer"); @@ -67,21 +136,34 @@ public class TestMirrorMakerAgent {/* mirrorMaker.setProducer("producer"); mirrorMaker.setStatus("200"); mirrorMaker.setWhitelist("whitelist"); - + mirrorMaker2.setConsumer("consumer"); mirrorMaker2.setName("MirrorMaker2"); mirrorMaker2.setProducer("producer"); mirrorMaker2.setStatus("200"); mirrorMaker2.setWhitelist("whitelist"); - + listsMirrorMaker.add(mirrorMaker2); listMirrorMaker.setListMirrorMaker(listsMirrorMaker); - + mirrorMakerAgent.mirrorMakers = listMirrorMaker; - + mirrorMakerAgent.createMirrorMaker(mirrorMaker); - + assertEquals(2, mirrorMakerAgent.mirrorMakers.getListMirrorMaker().size()); } -*/} + @Test + public void testPublish() { + TopicUtil util = new TopicUtil(); + util.publishTopic("topicURL", "topicname", "mechid", "password", "message"); + + } + + @Test + public void testSubscribe() { + TopicUtil util = new TopicUtil(); + util.subscribeTopic("topicURL", "topicname", "1", "mechid", "password"); + + } +} |