SchemaRegistryServiceTests.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.provectus.kafka.ui;
  2. import com.provectus.kafka.ui.model.CompatibilityLevelResponse;
  3. import com.provectus.kafka.ui.model.SchemaSubject;
  4. import com.provectus.kafka.ui.rest.MetricsRestController;
  5. import io.confluent.kafka.schemaregistry.CompatibilityLevel;
  6. import lombok.extern.log4j.Log4j2;
  7. import org.junit.jupiter.api.Assertions;
  8. import org.junit.jupiter.api.Test;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.test.context.ContextConfiguration;
  12. import org.springframework.test.web.reactive.server.EntityExchangeResult;
  13. import org.springframework.test.web.reactive.server.WebTestClient;
  14. import org.springframework.web.reactive.function.BodyInserters;
  15. import java.util.List;
  16. import java.util.UUID;
  17. @ContextConfiguration(initializers = {AbstractBaseTest.Initializer.class})
  18. @Log4j2
  19. class SchemaRegistryServiceTests extends AbstractBaseTest {
  20. @Autowired
  21. MetricsRestController metricsRestController;
  22. @Test
  23. public void should404WhenGetAllSchemasForUnknownCluster() {
  24. WebTestClient.bindToController(metricsRestController)
  25. .build()
  26. .get()
  27. .uri("http://localhost:8080/api/clusters/unknown-cluster/schemas")
  28. .exchange()
  29. .expectStatus().isNotFound();
  30. }
  31. @Test
  32. void shouldReturn404WhenGetLatestSchemaByNonExistingSchemaName() {
  33. String unknownSchema = "unknown-schema";
  34. WebTestClient.bindToController(metricsRestController).build()
  35. .get()
  36. .uri("http://localhost:8080/api/clusters/local/schemas/{schemaName}/latest", unknownSchema)
  37. .exchange()
  38. .expectStatus().isNotFound();
  39. }
  40. @Test
  41. void shouldReturnBackwardAsGlobalCompatibilityLevelByDefault() {
  42. WebTestClient.bindToController(metricsRestController).build()
  43. .get()
  44. .uri("http://localhost:8080/api/clusters/local/schemas/compatibility")
  45. .exchange()
  46. .expectStatus().isOk()
  47. .expectBody(CompatibilityLevelResponse.class)
  48. .consumeWith(result -> {
  49. CompatibilityLevelResponse responseBody = result.getResponseBody();
  50. Assertions.assertNotNull(responseBody);
  51. Assertions.assertEquals(CompatibilityLevel.BACKWARD.name, responseBody.getCompatibilityLevel());
  52. });
  53. }
  54. @Test
  55. public void shouldReturnNotNullResponseWhenGetAllSchemas() {
  56. WebTestClient.bindToController(metricsRestController)
  57. .build()
  58. .get()
  59. .uri("http://localhost:8080/api/clusters/local/schemas")
  60. .exchange()
  61. .expectStatus().isOk()
  62. .expectBodyList(String.class)
  63. .consumeWith(result -> {
  64. List<String> responseBody = result.getResponseBody();
  65. Assertions.assertNotNull(responseBody);
  66. log.info("Response of test schemas: {}", responseBody);
  67. });
  68. }
  69. @Test
  70. public void shouldReturnSuccessAndSchemaIdWhenCreateNewSchema() {
  71. WebTestClient webTestClient = WebTestClient.bindToController(metricsRestController).build();
  72. String schemaName = UUID.randomUUID().toString();
  73. webTestClient
  74. .post()
  75. .uri("http://localhost:8080/api/clusters/local/schemas/{schemaName}", schemaName)
  76. .contentType(MediaType.APPLICATION_JSON)
  77. .body(BodyInserters.fromValue("{\"schema\":\"{\\\"type\\\": \\\"string\\\"}\"}"))
  78. .exchange()
  79. .expectStatus().isOk()
  80. .expectBody(SchemaSubject.class)
  81. .consumeWith(this::assertResponseBodyWhenCreateNewSchema);
  82. webTestClient
  83. .get()
  84. .uri("http://localhost:8080/api/clusters/local/schemas/{schemaName}/latest", schemaName)
  85. .exchange()
  86. .expectStatus().isOk()
  87. .expectBodyList(SchemaSubject.class)
  88. .consumeWith(listEntityExchangeResult -> assertSchemaWhenGetLatest(schemaName, listEntityExchangeResult));
  89. }
  90. private void assertSchemaWhenGetLatest(String schemaName, EntityExchangeResult<List<SchemaSubject>> listEntityExchangeResult) {
  91. List<SchemaSubject> responseBody = listEntityExchangeResult.getResponseBody();
  92. Assertions.assertNotNull(responseBody);
  93. Assertions.assertEquals(1, responseBody.size());
  94. SchemaSubject actualSchema = responseBody.get(0);
  95. Assertions.assertNotNull(actualSchema);
  96. Assertions.assertEquals(schemaName, actualSchema.getSubject());
  97. Assertions.assertEquals("\"string\"", actualSchema.getSchema());
  98. }
  99. private void assertResponseBodyWhenCreateNewSchema(EntityExchangeResult<SchemaSubject> exchangeResult) {
  100. SchemaSubject responseBody = exchangeResult.getResponseBody();
  101. Assertions.assertNotNull(responseBody);
  102. Assertions.assertEquals(1, responseBody.getId(), "The schema ID should be non-null in the response");
  103. String message = "It should be null";
  104. Assertions.assertNull(responseBody.getSchema(), message);
  105. Assertions.assertNull(responseBody.getSubject(), message);
  106. Assertions.assertNull(responseBody.getVersion(), message);
  107. }
  108. }