|
| 1 | +package com.ericsson.ei.encryption; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.mockserver.integration.ClientAndServer.startClientAndServer; |
| 6 | +import static org.mockserver.model.HttpRequest.request; |
| 7 | +import static org.mockserver.model.HttpResponse.response; |
| 8 | +import static org.slf4j.LoggerFactory.getLogger; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +import org.json.JSONObject; |
| 16 | +import org.junit.Ignore; |
| 17 | +import org.mockserver.integration.ClientAndServer; |
| 18 | +import org.mockserver.verify.VerificationTimes; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.springframework.boot.web.server.LocalServerPort; |
| 21 | +import org.springframework.http.HttpStatus; |
| 22 | +import org.springframework.http.ResponseEntity; |
| 23 | +import org.springframework.test.context.TestPropertySource; |
| 24 | + |
| 25 | +import com.ericsson.ei.utils.FunctionalTestBase; |
| 26 | +import com.ericsson.ei.utils.HttpRequest; |
| 27 | +import com.ericsson.eiffelcommons.subscriptionobject.RestPostSubscriptionObject; |
| 28 | + |
| 29 | +import cucumber.api.java.Before; |
| 30 | +import cucumber.api.java.en.Then; |
| 31 | +import cucumber.api.java.en.When; |
| 32 | + |
| 33 | +@Ignore |
| 34 | +@TestPropertySource(properties = { "spring.data.mongodb.database: EncryptionSteps", |
| 35 | + "missedNotificationDataBaseName: EncryptionSteps-missedNotifications", |
| 36 | + "rabbitmq.exchange.name: EncryptionSteps-exchange", |
| 37 | + "rabbitmq.consumerName: EncryptionSteps-consumer", |
| 38 | + "jasypt.encryptor.password: secret" }) |
| 39 | +public class EncryptionSteps extends FunctionalTestBase { |
| 40 | + |
| 41 | + private static final Logger LOGGER = getLogger(EncryptionSteps.class); |
| 42 | + |
| 43 | + private static final String EIFFEL_EVENTS_JSON_PATH = "src/functionaltests/resources/eiffel_events_for_test.json"; |
| 44 | + private static final String SUBSCRIPTION_GET_ENDPOINT = "/subscriptions/<name>"; |
| 45 | + private static final String SUBSCRIPTION_ROOT_ENDPOINT = "/subscriptions"; |
| 46 | + private static final String MOCK_ENDPOINT = "/notify-me"; |
| 47 | + private static final String AUTH_HEADER_NAME = "Authorization"; |
| 48 | + private static final String AUTH_HEADER_VALUE = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="; |
| 49 | + private static final int NOTIFICATION_SLEEP = 5; |
| 50 | + |
| 51 | + private static final String SUBSCRIPTION_NAME = "MySubscription"; |
| 52 | + private static final String MEDIA_TYPE = "application/x-www-form-urlencoded"; |
| 53 | + private static final String NOTIFICATION_META = "http://localhost:{port}/notify-me"; |
| 54 | + private static final String CONDITION_KEY = "jmespath"; |
| 55 | + private static final String CONDITION_VALUE = "split(identity, '/') | [1] =='com.mycompany.myproduct'"; |
| 56 | + private static final String USERNAME = "username"; |
| 57 | + private static final String PASSWORD = "password"; |
| 58 | + private static final String AUTH_TYPE = "BASIC_AUTH"; |
| 59 | + |
| 60 | + @LocalServerPort |
| 61 | + private int applicationPort; |
| 62 | + |
| 63 | + private ResponseEntity response; |
| 64 | + private String mockServerPort; |
| 65 | + private ClientAndServer clientAndServer; |
| 66 | + |
| 67 | + @Before |
| 68 | + public void init() throws IOException { |
| 69 | + clientAndServer = startClientAndServer(); |
| 70 | + mockServerPort = String.valueOf(clientAndServer.getLocalPort()); |
| 71 | + setUpMock(); |
| 72 | + } |
| 73 | + |
| 74 | + @When("^a subscription is created$") |
| 75 | + public void subscriptionIsCreated() throws Exception { |
| 76 | + LOGGER.debug("Creating a subscription."); |
| 77 | + RestPostSubscriptionObject subscription = new RestPostSubscriptionObject(SUBSCRIPTION_NAME); |
| 78 | + subscription.setNotificationMeta(NOTIFICATION_META.replace("{port}", mockServerPort)); |
| 79 | + subscription.setRestPostBodyMediaType(MEDIA_TYPE); |
| 80 | + subscription.addNotificationMessageKeyValue("json", "dummy"); |
| 81 | + JSONObject condition = new JSONObject().put(CONDITION_KEY, CONDITION_VALUE); |
| 82 | + subscription.addConditionToRequirement(0, condition); |
| 83 | + subscription.setUsername(USERNAME); |
| 84 | + subscription.setPassword(PASSWORD); |
| 85 | + subscription.setAuthenticationType(AUTH_TYPE); |
| 86 | + |
| 87 | + List<String> subscriptionsToSend = new ArrayList<>(); |
| 88 | + subscriptionsToSend.add(subscription.toString()); |
| 89 | + postSubscriptions(subscriptionsToSend.toString()); |
| 90 | + validateSubscriptionsSuccessfullyAdded(); |
| 91 | + } |
| 92 | + |
| 93 | + @When("^eiffel events are sent$") |
| 94 | + public void eiffelEventsAreSent() throws IOException, InterruptedException { |
| 95 | + LOGGER.debug("Sending Eiffel events."); |
| 96 | + List<String> eventNamesToSend = getEventNamesToSend(); |
| 97 | + eventManager.sendEiffelEvents(EIFFEL_EVENTS_JSON_PATH, eventNamesToSend); |
| 98 | + List<String> eventsIdList = eventManager.getEventsIdList(EIFFEL_EVENTS_JSON_PATH, |
| 99 | + eventNamesToSend); |
| 100 | + List<String> missingEventIds = dbManager.verifyEventsInDB(eventsIdList, 0); |
| 101 | + String errorMessage = "The following events are missing in mongoDB: " |
| 102 | + + missingEventIds.toString(); |
| 103 | + assertEquals(errorMessage, 0, missingEventIds.size()); |
| 104 | + } |
| 105 | + |
| 106 | + @Then("^the password should be encrypted in the database$") |
| 107 | + public void passwordShouldBeEncrypted() { |
| 108 | + LOGGER.debug("Verifying encoded password in subscription."); |
| 109 | + String json = dbManager.getSubscription(SUBSCRIPTION_NAME); |
| 110 | + JSONObject subscription = new JSONObject(json); |
| 111 | + String password = subscription.getString("password"); |
| 112 | + assertTrue(EncryptionFormatter.isEncrypted(password)); |
| 113 | + } |
| 114 | + |
| 115 | + @Then("^the subscription should trigger$") |
| 116 | + public void subscriptionShouldTrigger() throws InterruptedException { |
| 117 | + LOGGER.info("Verifying that subscription was triggered"); |
| 118 | + TimeUnit.SECONDS.sleep(NOTIFICATION_SLEEP); |
| 119 | + clientAndServer.verify(request().withPath(MOCK_ENDPOINT).withBody(""), |
| 120 | + VerificationTimes.atLeast(1)); |
| 121 | + } |
| 122 | + |
| 123 | + @Then("^the notification should be sent with a decrypted password$") |
| 124 | + public void notificationShouldBeSentWithDecryptedPassword() { |
| 125 | + LOGGER.info("Verifying that notification contains decrypted password"); |
| 126 | + clientAndServer.verify( |
| 127 | + request().withPath(MOCK_ENDPOINT).withHeader(AUTH_HEADER_NAME, AUTH_HEADER_VALUE), |
| 128 | + VerificationTimes.atLeast(1)); |
| 129 | + } |
| 130 | + |
| 131 | + private void postSubscriptions(String jsonDataAsString) throws Exception { |
| 132 | + HttpRequest postRequest = new HttpRequest(HttpRequest.HttpMethod.POST); |
| 133 | + response = postRequest.setHost(getHostName()) |
| 134 | + .setPort(applicationPort) |
| 135 | + .addHeader("content-type", "application/json") |
| 136 | + .addHeader("Accept", "application/json") |
| 137 | + .setEndpoint(SUBSCRIPTION_ROOT_ENDPOINT) |
| 138 | + .setBody(jsonDataAsString) |
| 139 | + .performRequest(); |
| 140 | + assertEquals("Expected to add subscription to EI", HttpStatus.OK.value(), |
| 141 | + response.getStatusCodeValue()); |
| 142 | + } |
| 143 | + |
| 144 | + private void validateSubscriptionsSuccessfullyAdded() |
| 145 | + throws Exception { |
| 146 | + String endpoint = SUBSCRIPTION_GET_ENDPOINT.replaceAll("<name>", SUBSCRIPTION_NAME); |
| 147 | + HttpRequest getRequest = new HttpRequest(HttpRequest.HttpMethod.GET); |
| 148 | + response = getRequest.setHost(getHostName()) |
| 149 | + .setPort(applicationPort) |
| 150 | + .addHeader("content-type", "application/json") |
| 151 | + .addHeader("Accept", "application/json") |
| 152 | + .setEndpoint(endpoint) |
| 153 | + .performRequest(); |
| 154 | + assertEquals("Subscription successfully added in EI: ", HttpStatus.OK.value(), |
| 155 | + response.getStatusCodeValue()); |
| 156 | + } |
| 157 | + |
| 158 | + private List<String> getEventNamesToSend() { |
| 159 | + List<String> eventNames = new ArrayList<>(); |
| 160 | + eventNames.add("event_EiffelArtifactCreatedEvent_3"); |
| 161 | + return eventNames; |
| 162 | + } |
| 163 | + |
| 164 | + private void setUpMock() { |
| 165 | + clientAndServer.when(request().withMethod("POST").withPath(MOCK_ENDPOINT)) |
| 166 | + .respond(response().withStatusCode(200).withBody("")); |
| 167 | + } |
| 168 | +} |
0 commit comments