فهرست منبع

added api interaction to topic test

Bogdan Volodarskiy 4 سال پیش
والد
کامیت
704427b340

+ 6 - 0
kafka-ui-e2e-checks/pom.xml

@@ -14,6 +14,7 @@
         <junit.version>5.7.0</junit.version>
         <aspectj.version>1.9.6</aspectj.version>
         <allure.version>2.13.7</allure.version>
+        <json-smart.version>1.1.1</json-smart.version>
         <testcontainers.version>1.15.2</testcontainers.version>
         <selenide.version>5.16.2</selenide.version>
         <assertj.version>3.17.1</assertj.version>
@@ -32,6 +33,11 @@
     </properties>
 
     <dependencies>
+        <dependency>
+            <groupId>net.minidev</groupId>
+            <artifactId>json-smart</artifactId>
+            <version>${json-smart.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.kafka</groupId>
             <artifactId>kafka_2.13</artifactId>

+ 2 - 0
kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/base/BaseTest.java

@@ -2,6 +2,7 @@ package com.provectus.kafka.ui.base;
 
 import com.codeborne.selenide.Configuration;
 import com.codeborne.selenide.logevents.SelenideLogger;
+import com.provectus.kafka.ui.helpers.Helpers;
 import com.provectus.kafka.ui.pages.Pages;
 import com.provectus.kafka.ui.screenshots.Screenshooter;
 import com.provectus.kafka.ui.steps.Steps;
@@ -25,6 +26,7 @@ public class BaseTest {
 
   protected Steps steps = Steps.INSTANCE;
   protected Pages pages = Pages.INSTANCE;
+  protected Helpers helpers = Helpers.INSTANCE;
 
   private Screenshooter screenshooter = new Screenshooter();
 

+ 64 - 0
kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/helpers/ApiHelper.java

@@ -0,0 +1,64 @@
+package com.provectus.kafka.ui.helpers;
+
+import com.provectus.kafka.ui.base.TestConfiguration;
+import com.provectus.kafka.ui.steps.kafka.KafkaSteps;
+import lombok.SneakyThrows;
+import net.minidev.json.JSONObject;
+
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ApiHelper {
+    int partitions = 1;
+    short replicationFactor = 1;
+    String newTopic = "new-topic";
+    @SneakyThrows
+    private void sendRequest(String method, String additionalURL, JSONObject jsonObject){
+        URL url = new URL(TestConfiguration.BASE_URL+additionalURL);
+        HttpURLConnection con = (HttpURLConnection) url.openConnection();
+        con.setDoOutput(true);
+        con.setDoInput(true);
+        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+        con.setRequestProperty("Accept", "application/json");
+        switch (method) {
+            case "POST":
+                con.setRequestMethod("POST");
+                OutputStream os = con.getOutputStream();
+                os.write(jsonObject.toString().getBytes("UTF-8"));
+                os.close();
+                break;
+            case "DELETE":
+                con.setRequestMethod("DELETE");
+                con.getResponseMessage();
+                break;
+        }
+        assertTrue(con.getResponseCode()==200);
+        InputStream in = new BufferedInputStream(con.getInputStream());
+        String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
+        in.close();
+        con.disconnect();
+    }
+
+
+    @SneakyThrows
+    public void createTopic(String clusterName, String topicName) {
+        JSONObject topic = new JSONObject();
+        topic.put("name",topicName);
+        topic.put("partitions",partitions);
+        topic.put("replicationFactor",replicationFactor);
+        sendRequest("POST","api/clusters/"+clusterName+"/topics",topic);
+
+    }
+
+    @SneakyThrows
+    public void deleteTopic(String clusterName, String topicName) {
+        sendRequest("DELETE","api/clusters/"+clusterName+"/topics/"+topicName,null);
+    }
+
+}

+ 11 - 0
kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/helpers/Helpers.java

@@ -0,0 +1,11 @@
+package com.provectus.kafka.ui.helpers;
+
+
+
+public class Helpers {
+    public static final Helpers INSTANCE = new Helpers();
+
+    private Helpers(){}
+
+    public ApiHelper apiHelper = new ApiHelper();
+}

+ 4 - 3
kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/topics/TopicTests.java

@@ -3,6 +3,7 @@ package com.provectus.kafka.ui.topics;
 import com.provectus.kafka.ui.base.BaseTest;
 import com.provectus.kafka.ui.pages.MainPage;
 import com.provectus.kafka.ui.steps.kafka.KafkaSteps;
+import com.provectus.kafka.ui.helpers.ApiHelper;
 import lombok.SneakyThrows;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.DisplayName;
@@ -15,15 +16,15 @@ public class TopicTests extends BaseTest {
 
     @AfterEach
     @SneakyThrows
-    void  afterEach(){
-        steps.kafka.deleteTopic(KafkaSteps.Cluster.SECOND_LOCAL,NEW_TOPIC);
+        void  afterEach(){
+            helpers.apiHelper.deleteTopic("secondLocal","new-topic");
     }
 
     @SneakyThrows
     @DisplayName("should create a topic")
     @Test
     void createTopic(){
-        steps.kafka.createTopic(KafkaSteps.Cluster.SECOND_LOCAL,NEW_TOPIC);
+        helpers.apiHelper.createTopic("secondLocal","new-topic");
         pages.open()
                 .mainPage.shouldBeOnPage()
         .goToSideMenu(KafkaSteps.Cluster.SECOND_LOCAL.getName(), MainPage.SideMenuOptions.TOPICS)