|
| 1 | +package com.ericsson.ei.artifactintegrationtests; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.net.URL; |
| 9 | +import java.time.ZonedDateTime; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import org.junit.Ignore; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.boot.test.context.SpringBootContextLoader; |
| 22 | +import org.springframework.boot.test.context.SpringBootTest; |
| 23 | +import org.springframework.boot.web.server.LocalServerPort; |
| 24 | +import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.test.context.ContextConfiguration; |
| 26 | +import org.springframework.test.context.TestExecutionListeners; |
| 27 | +import org.springframework.test.context.junit4.SpringRunner; |
| 28 | +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; |
| 29 | + |
| 30 | +import com.ericsson.ei.App; |
| 31 | +import com.ericsson.ei.mongodbhandler.MongoDBHandler; |
| 32 | +import com.ericsson.ei.utils.HttpRequest; |
| 33 | +import com.ericsson.ei.utils.HttpRequest.HttpMethod; |
| 34 | +import com.fasterxml.jackson.databind.JsonNode; |
| 35 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 36 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 37 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 38 | + |
| 39 | +import cucumber.api.java.en.Given; |
| 40 | +import cucumber.api.java.en.Then; |
| 41 | +import cucumber.api.java.en.When; |
| 42 | +import util.IntegrationTestBase; |
| 43 | + |
| 44 | +@Ignore |
| 45 | +@RunWith(SpringRunner.class) |
| 46 | +@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) |
| 47 | +@ContextConfiguration(classes = App.class, loader = SpringBootContextLoader.class) |
| 48 | +@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class }) |
| 49 | +public class ArtifactFlowStepsIT extends IntegrationTestBase { |
| 50 | + private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactFlowStepsIT.class); |
| 51 | + |
| 52 | + private static final String UPSTREAM_INPUT_FILE = "src/test/resources/upStreamInput.json"; |
| 53 | + private static final String RULES_FILE_PATH = "src/test/resources/ArtifactRules_new.json"; |
| 54 | + private static final String EVENTS_FILE_PATH = "src/test/resources/test_events.json"; |
| 55 | + private static final String AGGREGATED_OBJECT_FILE_PATH = "src/test/resources/AggregatedDocumentInternalCompositionLatest.json"; |
| 56 | + private static final String AGGREGATED_OBJECT_ID = "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43"; |
| 57 | + private static final String SUBSCRIPTIONS_PATH = "src/integrationtests/resources/subscriptionsTemplate.json"; |
| 58 | + private static final String HOST = "localhost"; |
| 59 | + |
| 60 | + |
| 61 | + private long startTime; |
| 62 | + private static ObjectMapper objectMapper = new ObjectMapper(); |
| 63 | + |
| 64 | + @LocalServerPort |
| 65 | + int port; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private RabbitTemplate rabbitMqTemplate; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + MongoDBHandler mongoDBHandler; |
| 72 | + |
| 73 | + @Given("^subscriptions are uploaded$") |
| 74 | + public void subscriptions_are_created() throws URISyntaxException, IOException { |
| 75 | + startTime = System.currentTimeMillis(); |
| 76 | + |
| 77 | + URL subscriptionsInput = new File(SUBSCRIPTIONS_PATH).toURI().toURL(); |
| 78 | + ArrayNode subscriptionsJson = (ArrayNode) objectMapper.readTree(subscriptionsInput); |
| 79 | + subscriptionsJson = setSubscriptionFields(subscriptionsJson); |
| 80 | + |
| 81 | + HttpRequest postRequest = new HttpRequest(HttpMethod.POST); |
| 82 | + ResponseEntity response = postRequest.setHost(HOST) |
| 83 | + .setPort(port) |
| 84 | + .setEndpoint("/subscriptions") |
| 85 | + .addHeader("Content-type", "application/json") |
| 86 | + .setBody(subscriptionsJson.toString()) |
| 87 | + .performRequest(); |
| 88 | + assertEquals(200, response.getStatusCodeValue()); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + private ArrayNode setSubscriptionFields(ArrayNode subscriptionJson) { |
| 93 | + ObjectNode requirement = ((ObjectNode) subscriptionJson.get(0).get("requirements").get(0).get("conditions").get(0)); |
| 94 | + requirement.put("jmespath", "id=='1100572b-c3j4-441e-abc9-b62f48080011'"); |
| 95 | + |
| 96 | + return subscriptionJson; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @When("^eiffel events are sent$") |
| 101 | + public void eiffel_events_are_sent() throws Throwable { |
| 102 | + |
| 103 | + final URL upStreamInput = new File(UPSTREAM_INPUT_FILE).toURI().toURL(); |
| 104 | + ArrayNode upstreamJson = (ArrayNode) objectMapper.readTree(upStreamInput); |
| 105 | + if (upstreamJson != null) { |
| 106 | + for (JsonNode event : upstreamJson) { |
| 107 | + String eventStr = event.toString(); |
| 108 | + rabbitMqTemplate.convertAndSend(eventStr); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + super.sendEventsAndConfirm(); |
| 113 | + } |
| 114 | + |
| 115 | + @Then("^mongodb should contain mail\\.$") |
| 116 | + public void mongodb_should_contain_mails() throws Throwable { |
| 117 | + JsonNode newestMailJson = getMailFromDatabase(); |
| 118 | + String createdDate = newestMailJson.get("created").get("$date").asText(); |
| 119 | + |
| 120 | + long createdDateInMillis = ZonedDateTime.parse(createdDate).toInstant().toEpochMilli(); |
| 121 | + assert(createdDateInMillis >= startTime): "Mail was not triggered. createdDateInMillis is less than startTime."; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected String getRulesFilePath() { |
| 126 | + return RULES_FILE_PATH; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + protected String getEventsFilePath() { |
| 131 | + return EVENTS_FILE_PATH; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + protected int extraEventsCount() { |
| 136 | + return 8; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected List<String> getEventNamesToSend() { |
| 141 | + List<String> eventNames = new ArrayList<>(); |
| 142 | + eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2"); |
| 143 | + eventNames.add("event_EiffelArtifactPublishedEvent_3"); |
| 144 | + eventNames.add("event_EiffelArtifactCreatedEvent_3"); |
| 145 | + eventNames.add("event_EiffelTestCaseTriggeredEvent_3"); |
| 146 | + eventNames.add("event_EiffelTestCaseStartedEvent_3"); |
| 147 | + eventNames.add("event_EiffelTestCaseFinishedEvent_3"); |
| 148 | + eventNames.add("event_EiffelArtifactPublishedEvent_3_1"); |
| 149 | + eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3"); |
| 150 | + eventNames.add("event_EiffelTestCaseTriggeredEvent_3_1"); |
| 151 | + eventNames.add("event_EiffelTestCaseStartedEvent_3_1"); |
| 152 | + eventNames.add("event_EiffelTestCaseFinishedEvent_3_1"); |
| 153 | + return eventNames; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected Map<String, JsonNode> getCheckData() throws IOException { |
| 158 | + JsonNode expectedJSON = getJSONFromFile(AGGREGATED_OBJECT_FILE_PATH); |
| 159 | + Map<String, JsonNode> checkData = new HashMap<>(); |
| 160 | + checkData.put(AGGREGATED_OBJECT_ID, expectedJSON); |
| 161 | + return checkData; |
| 162 | + } |
| 163 | + |
| 164 | + private JsonNode getMailFromDatabase() throws IOException { |
| 165 | + ArrayList<String> allMails = mongoDBHandler.getAllDocuments(MAILHOG_DATABASE_NAME, "messages"); |
| 166 | + String mailString = allMails.get(0); |
| 167 | + |
| 168 | + return objectMapper.readTree(mailString); |
| 169 | + } |
| 170 | +} |
0 commit comments