|
| 1 | +package com.ericsson.ei.flowtests; |
| 2 | + |
| 3 | +import com.ericsson.ei.handlers.ObjectHandler; |
| 4 | +import com.ericsson.ei.mongodbhandler.MongoDBHandler; |
| 5 | +import com.ericsson.ei.rmqhandler.RmqHandler; |
| 6 | +import com.ericsson.ei.waitlist.WaitListStorageHandler; |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import com.mongodb.MongoClient; |
| 11 | +import com.mongodb.client.MongoCollection; |
| 12 | +import com.rabbitmq.client.Channel; |
| 13 | +import com.rabbitmq.client.Connection; |
| 14 | +import com.rabbitmq.client.ConnectionFactory; |
| 15 | +import de.flapdoodle.embed.mongo.distribution.Version; |
| 16 | +import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; |
| 17 | +import org.apache.commons.io.FileUtils; |
| 18 | +import org.apache.qpid.server.Broker; |
| 19 | +import org.apache.qpid.server.BrokerOptions; |
| 20 | +import org.junit.AfterClass; |
| 21 | +import org.junit.BeforeClass; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.amqp.core.BindingBuilder; |
| 27 | +import org.springframework.amqp.core.Queue; |
| 28 | +import org.springframework.amqp.core.TopicExchange; |
| 29 | +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; |
| 30 | +import org.springframework.amqp.rabbit.core.RabbitAdmin; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.beans.factory.annotation.Value; |
| 33 | +import org.springframework.boot.test.context.SpringBootTest; |
| 34 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 35 | + |
| 36 | +import javax.annotation.PostConstruct; |
| 37 | +import java.io.File; |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 46 | +@SpringBootTest |
| 47 | +public class TrafficGeneratedTest { |
| 48 | + |
| 49 | + private static final int EVENT_PACKAGES = 10; |
| 50 | + private static Logger log = LoggerFactory.getLogger(TrafficGeneratedTest.class); |
| 51 | + |
| 52 | + public static File qpidConfig = null; |
| 53 | + private static MongodForTestsFactory testsFactory; |
| 54 | + static TrafficGeneratedTest.AMQPBrokerManager amqpBrocker; |
| 55 | + static Queue queue = null; |
| 56 | + static RabbitAdmin admin; |
| 57 | + |
| 58 | + static MongoClient mongoClient = null; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private MongoDBHandler mongoDBHandler; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private WaitListStorageHandler waitlist; |
| 65 | + |
| 66 | + @Autowired |
| 67 | + RmqHandler rmqHandler; |
| 68 | + |
| 69 | + @Autowired |
| 70 | + ObjectHandler objectHandler; |
| 71 | + |
| 72 | + public static class AMQPBrokerManager { |
| 73 | + private String path; |
| 74 | + private static final String PORT = "8672"; |
| 75 | + private final Broker broker = new Broker(); |
| 76 | + |
| 77 | + public AMQPBrokerManager(String path) { |
| 78 | + super(); |
| 79 | + this.path = path; |
| 80 | + } |
| 81 | + |
| 82 | + public void startBroker() throws Exception { |
| 83 | + final BrokerOptions brokerOptions = new BrokerOptions(); |
| 84 | + brokerOptions.setConfigProperty("qpid.amqp_port", PORT); |
| 85 | + brokerOptions.setConfigProperty("qpid.pass_file", "src/test/resources/configs/password.properties"); |
| 86 | + brokerOptions.setInitialConfigurationLocation(path); |
| 87 | + |
| 88 | + broker.startup(brokerOptions); |
| 89 | + } |
| 90 | + |
| 91 | + public void stopBroker() { |
| 92 | + broker.shutdown(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static ConnectionFactory cf; |
| 97 | + static Connection conn; |
| 98 | + private static String jsonFileContent; |
| 99 | + private static JsonNode parsedJason; |
| 100 | + private static String jsonFilePath = "src/test/resources/test_events_MP.json"; |
| 101 | + static private final String inputFilePath = "src/test/resources/aggregated_document_MP.json"; |
| 102 | + |
| 103 | + @Value("${database.name}") private String database; |
| 104 | + @Value("${event_object_map.collection.name}") private String event_map; |
| 105 | + |
| 106 | + @BeforeClass |
| 107 | + public static void setup() throws Exception { |
| 108 | + System.setProperty("flow.test", "true"); |
| 109 | + System.setProperty("eiffel.intelligence.processedEventsCount", "0"); |
| 110 | + System.setProperty("eiffel.intelligence.waitListEventsCount", "0"); |
| 111 | + setUpMessageBus(); |
| 112 | + setUpEmbeddedMongo(); |
| 113 | + } |
| 114 | + |
| 115 | + @PostConstruct |
| 116 | + public void initMocks() { |
| 117 | + mongoDBHandler.setMongoClient(mongoClient); |
| 118 | + waitlist.setMongoDbHandler(mongoDBHandler); |
| 119 | + } |
| 120 | + |
| 121 | + public static void setUpMessageBus() throws Exception { |
| 122 | + System.setProperty("rabbitmq.port", "8672"); |
| 123 | + System.setProperty("rabbitmq.user", "guest"); |
| 124 | + System.setProperty("rabbitmq.password", "guest"); |
| 125 | + |
| 126 | + |
| 127 | + String config = "src/test/resources/configs/qpidConfig.json"; |
| 128 | + jsonFileContent = FileUtils.readFileToString(new File(jsonFilePath)); |
| 129 | + ObjectMapper objectmapper = new ObjectMapper(); |
| 130 | + parsedJason = objectmapper.readTree(jsonFileContent); |
| 131 | + qpidConfig = new File(config); |
| 132 | + amqpBrocker = new TrafficGeneratedTest.AMQPBrokerManager(qpidConfig.getAbsolutePath()); |
| 133 | + amqpBrocker.startBroker(); |
| 134 | + cf = new ConnectionFactory(); |
| 135 | + cf.setUsername("guest"); |
| 136 | + cf.setPassword("guest"); |
| 137 | + cf.setPort(8672); |
| 138 | + conn = cf.newConnection(); |
| 139 | + } |
| 140 | + |
| 141 | + public static void setUpEmbeddedMongo() throws Exception { |
| 142 | + int port = 12349; |
| 143 | + System.setProperty("mongodb.port", ""+port); |
| 144 | + |
| 145 | + testsFactory = MongodForTestsFactory.with(Version.V3_4_1); |
| 146 | + mongoClient = testsFactory.newMongo(); |
| 147 | + } |
| 148 | + |
| 149 | + @AfterClass |
| 150 | + public static void tearDown() throws Exception { |
| 151 | + if (amqpBrocker != null) |
| 152 | + amqpBrocker.stopBroker(); |
| 153 | + |
| 154 | + try { |
| 155 | + conn.close(); |
| 156 | + } catch (Exception e) { |
| 157 | + //We try to close the connection but if |
| 158 | + //the connection is closed we just receive the |
| 159 | + //exception and go on |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void trafficGeneratedTest() { |
| 165 | + try { |
| 166 | + List<String> eventNames = getEventNamesToSend(); |
| 167 | + List<String> events = getPreparedEventsToSend(eventNames); |
| 168 | + int eventsCount = eventNames.size() * EVENT_PACKAGES; |
| 169 | + |
| 170 | + String queueName = rmqHandler.getQueueName(); |
| 171 | + String exchange = "ei-poc-4"; |
| 172 | + createExchange(exchange, queueName); |
| 173 | + Channel channel = conn.createChannel(); |
| 174 | + |
| 175 | + long timeBefore = System.currentTimeMillis(); |
| 176 | + |
| 177 | + for (String event : events) { |
| 178 | + try { |
| 179 | + channel.basicPublish(exchange, queueName, null, event.getBytes()); |
| 180 | + TimeUnit.MILLISECONDS.sleep(10); |
| 181 | + } catch (InterruptedException e) {} |
| 182 | + } |
| 183 | + |
| 184 | + // wait for all events to be processed |
| 185 | + long processedEvents = 0; |
| 186 | + MongoCollection eventMap = mongoClient.getDatabase(database).getCollection(event_map); |
| 187 | + while (processedEvents < eventsCount) { |
| 188 | + processedEvents = eventMap.count(); |
| 189 | + } |
| 190 | + |
| 191 | + TimeUnit.MILLISECONDS.sleep(1000); |
| 192 | + |
| 193 | + long timeAfter = System.currentTimeMillis(); |
| 194 | + long diffTime = timeAfter - timeBefore; |
| 195 | + |
| 196 | + String expectedDocument = FileUtils.readFileToString(new File(inputFilePath)); |
| 197 | + ObjectMapper objectmapper = new ObjectMapper(); |
| 198 | + JsonNode expectedJson = objectmapper.readTree(expectedDocument); |
| 199 | + for (int i = 0; i < EVENT_PACKAGES; i++) { |
| 200 | + String document = objectHandler.findObjectById("6acc3c87-75e0-4b6d-88f5-b1a5d4".concat(String.format("%06d", i))); |
| 201 | + JsonNode actualJson = objectmapper.readTree(document); |
| 202 | + assertEquals(expectedJson.toString().length(), actualJson.toString().length()); |
| 203 | + } |
| 204 | + |
| 205 | + String time = "" + diffTime / 60000 + "m " + (diffTime / 1000) % 60 + "s " + diffTime % 1000; |
| 206 | + System.out.println("Time of execution: " + time); |
| 207 | + } catch (Exception e) { |
| 208 | + log.debug(e.getMessage(),e); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * This method loops through every package of events and changes their ids and targets to unique value. |
| 214 | + * Ids of events that are located in the same package are related. |
| 215 | + * Events are sent to RabbitMQ queue. Deterministic traffic is used. |
| 216 | + * @param eventNames list of events to be sent. |
| 217 | + * @return list of ready to send events. |
| 218 | + */ |
| 219 | + private List<String> getPreparedEventsToSend(List<String> eventNames) throws IOException { |
| 220 | + List<String> events = new ArrayList<>(); |
| 221 | + String newID; |
| 222 | + for (int i = 0; i < EVENT_PACKAGES; i++) { |
| 223 | + for(String eventName : eventNames) { |
| 224 | + JsonNode eventJson = parsedJason.get(eventName); |
| 225 | + newID = eventJson.at("/meta/id").textValue().substring(0, 30).concat(String.format("%06d", i));; |
| 226 | + ((ObjectNode) eventJson.path("meta")).put("id", newID); |
| 227 | + for (JsonNode link : eventJson.path("links")) { |
| 228 | + if (link.has("target")) { |
| 229 | + newID = link.path("target").textValue().substring(0, 30).concat(String.format("%06d", i)); |
| 230 | + ((ObjectNode) link).put("target", newID); |
| 231 | + } |
| 232 | + } |
| 233 | + events.add(eventJson.toString()); |
| 234 | + } |
| 235 | + } |
| 236 | + return events; |
| 237 | + } |
| 238 | + |
| 239 | + private List<String> getEventNamesToSend() { |
| 240 | + List<String> eventNames = new ArrayList<>(); |
| 241 | + |
| 242 | + eventNames.add("event_EiffelArtifactCreatedEvent"); |
| 243 | + eventNames.add("event_EiffelArtifactPublishedEvent"); |
| 244 | + eventNames.add("event_EiffelConfidenceLevelModifiedEvent"); |
| 245 | + eventNames.add("event_EiffelTestCaseStartedEvent"); |
| 246 | + eventNames.add("event_EiffelTestCaseFinishedEvent"); |
| 247 | + |
| 248 | + return eventNames; |
| 249 | + } |
| 250 | + |
| 251 | + private void createExchange(final String exchangeName, final String queueName) { |
| 252 | + final CachingConnectionFactory ccf = new CachingConnectionFactory(cf); |
| 253 | + admin = new RabbitAdmin(ccf); |
| 254 | + queue = new Queue(queueName, false); |
| 255 | + admin.declareQueue(queue); |
| 256 | + final TopicExchange exchange = new TopicExchange(exchangeName); |
| 257 | + admin.declareExchange(exchange); |
| 258 | + admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("#")); |
| 259 | + ccf.destroy(); |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments