-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into main
- Loading branch information
Showing
8 changed files
with
436 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...rch/src/test/java/com/elasticsearch/elasticsearch/controller/CarEntityControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.elasticsearch.elasticsearch.controller; | ||
|
||
import com.elasticsearch.elasticsearch.entity.CarEntity; | ||
import com.elasticsearch.elasticsearch.service.CarService; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class CarEntityControllerTest { | ||
|
||
@InjectMocks | ||
private CarEntityController controller; | ||
|
||
@Mock | ||
private CarService service; | ||
|
||
@Test | ||
void testGetAllCarEntity() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(service.getAllCarEntity()).thenReturn(mockCarEntities); | ||
ResponseEntity<List<CarEntity>> responseEntity = controller.getAllCarEntity(); | ||
|
||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(mockCarEntities, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
void testGetCarDetailsById() { | ||
CarEntity mockCarEntity = new CarEntity(); | ||
when(service.getCarEntityWithCarId(any(Integer.class))).thenReturn(mockCarEntity); | ||
ResponseEntity<CarEntity> responseEntity = controller.getCarDetailsById("1"); | ||
|
||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(mockCarEntity, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
void testGetCarDetailsByMileage() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(service.getCarEntityWithCarMileage(any(Double.class))).thenReturn(mockCarEntities); | ||
|
||
ResponseEntity<List<CarEntity>> responseEntity = controller.getCarDetailsByMileage("50.5"); | ||
|
||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(mockCarEntities, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
void testGetCarDetailsByBrand() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(service.getCarEntityWithBrandName(any(String.class))).thenReturn(mockCarEntities); | ||
|
||
ResponseEntity<List<CarEntity>> responseEntity = controller.getCarDetailsByBrand("Toyota"); | ||
|
||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(mockCarEntities, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
void testGetCarDetailsByPrice() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(service.getCarEntityWithCarPrice(any(Double.class))).thenReturn(mockCarEntities); | ||
|
||
ResponseEntity<List<CarEntity>> responseEntity = controller.getCarDetailsByPrice("30000.0"); | ||
|
||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(mockCarEntities, responseEntity.getBody()); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
elastic-search/src/test/java/com/elasticsearch/elasticsearch/entity/CarEntityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.elasticsearch.elasticsearch.entity; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class CarEntityTest { | ||
|
||
@Test | ||
void testGetterAndSetter() { | ||
// Create a CarEntity object | ||
CarEntity carEntity = new CarEntity(); | ||
|
||
// Set values using setters | ||
carEntity.setCarId(1); | ||
carEntity.setModel("TestModel"); | ||
carEntity.setBrand("TestBrand"); | ||
carEntity.setYear(2022L); | ||
carEntity.setColor("TestColor"); | ||
carEntity.setMileage(50.5); | ||
carEntity.setPrice(30000.0); | ||
carEntity.setQuantity(5); | ||
carEntity.setTax(5.0); | ||
|
||
// Check values using getters | ||
assertEquals(1, carEntity.getCarId()); | ||
assertEquals("TestModel", carEntity.getModel()); | ||
assertEquals("TestBrand", carEntity.getBrand()); | ||
assertEquals(2022L, carEntity.getYear()); | ||
assertEquals("TestColor", carEntity.getColor()); | ||
assertEquals(50.5, carEntity.getMileage()); | ||
assertEquals(30000.0, carEntity.getPrice()); | ||
assertEquals(5, carEntity.getQuantity()); | ||
assertEquals(5.0, carEntity.getTax()); | ||
} | ||
|
||
@Test | ||
void testEqualsAndHashCode() { | ||
// Create two CarEntity objects with the same values | ||
CarEntity carEntity1 = new CarEntity(); | ||
carEntity1.setCarId(1); | ||
carEntity1.setModel("TestModel"); | ||
carEntity1.setBrand("TestBrand"); | ||
|
||
CarEntity carEntity2 = new CarEntity(); | ||
carEntity2.setCarId(1); | ||
carEntity2.setModel("TestModel"); | ||
carEntity2.setBrand("TestBrand"); | ||
|
||
// Check if equals method works | ||
assertTrue(carEntity1.equals(carEntity2) && carEntity2.equals(carEntity1)); | ||
|
||
// Check if hash codes are the same for equal objects | ||
assertEquals(carEntity1.hashCode(), carEntity2.hashCode()); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...search/src/test/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.elasticsearch.elasticsearch.eventlistener; | ||
|
||
import com.elasticsearch.elasticsearch.entity.CarEntity; | ||
import com.elasticsearch.elasticsearch.eventlistener.impl.AzureConsumer; | ||
import com.elasticsearch.elasticsearch.service.CarService; | ||
import com.elasticsearch.elasticsearch.util.CarMapper; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
class AzureConsumerTest { | ||
|
||
@InjectMocks | ||
private AzureConsumer azureConsumer; | ||
|
||
@Mock | ||
private CarService carService; | ||
|
||
@Mock | ||
private CarMapper carMapper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
|
||
@Test | ||
public void testConsumeEvents() { | ||
String kafkaMessage = "{\"id\":\"1\",\"make\":\"Toyota\",\"model\":\"Camry\",\"year\":2022}"; | ||
|
||
ConsumerRecord<String, String> consumerRecord = new ConsumerRecord<>( | ||
"test-topic", | ||
0, | ||
0, | ||
"key", | ||
kafkaMessage | ||
); | ||
|
||
azureConsumer.consumeEvents(consumerRecord); | ||
CarEntity expectedCarEntity = CarMapper.mapStringToEntity(kafkaMessage); | ||
Mockito.verify(carService, Mockito.times(1)).saveCarEntity(expectedCarEntity); | ||
} | ||
@Test | ||
public void test_logs_received_message() { | ||
AzureConsumer azureConsumer = new AzureConsumer(); | ||
String event = "Test message"; | ||
azureConsumer.consumeEvent(event); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...c-search/src/test/java/com/elasticsearch/elasticsearch/eventlistener/GCPConsumerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.elasticsearch.elasticsearch.eventlistener; | ||
|
||
import com.elasticsearch.elasticsearch.eventlistener.impl.GcpConsumer; | ||
import com.elasticsearch.elasticsearch.repository.CarEntityRepository; | ||
import com.elasticsearch.elasticsearch.service.CarService; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate; | ||
import org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class GcpConsumerTest { | ||
|
||
@Mock | ||
private PubSubTemplate pubSubTemplate; | ||
|
||
@Mock | ||
private CarEntityRepository carEntityRepository; | ||
|
||
@Mock | ||
private CarService carService; | ||
|
||
@Mock | ||
private ObjectMapper objectMapper; | ||
|
||
@InjectMocks | ||
private GcpConsumer gcpConsumer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
|
||
@Test | ||
void consumeEventTest() { | ||
BasicAcknowledgeablePubsubMessage mockMessage = Mockito.mock(BasicAcknowledgeablePubsubMessage.class); | ||
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(com.google.protobuf.ByteString.copyFromUtf8("test")).build(); | ||
when(mockMessage.getPubsubMessage()).thenReturn(pubsubMessage); | ||
|
||
assertDoesNotThrow(() -> gcpConsumer.consumeEvent(mockMessage)); | ||
|
||
verify(mockMessage).ack(); | ||
} | ||
|
||
@Test | ||
void processMessageShouldHandleJsonProcessingException() throws JsonProcessingException { | ||
BasicAcknowledgeablePubsubMessage mockMessage = Mockito.mock(BasicAcknowledgeablePubsubMessage.class); | ||
when(mockMessage.getPubsubMessage()).thenReturn(createPubsubMessage("invalid JSON")); | ||
when(objectMapper.readValue(any(String.class), any(Class.class))).thenThrow(JsonProcessingException.class); | ||
|
||
assertDoesNotThrow(() -> gcpConsumer.processMessage(mockMessage)); | ||
|
||
verify(mockMessage).ack(); | ||
} | ||
|
||
private PubsubMessage createPubsubMessage(String data) { | ||
return PubsubMessage.newBuilder().setData(com.google.protobuf.ByteString.copyFromUtf8(data)).build(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
elastic-search/src/test/java/com/elasticsearch/elasticsearch/service/CarServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.elasticsearch.elasticsearch.service; | ||
|
||
import com.elasticsearch.elasticsearch.entity.CarEntity; | ||
import com.elasticsearch.elasticsearch.repository.CarEntityRepository; | ||
import com.elasticsearch.elasticsearch.service.impl.CarServiceImpl; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
class CarServiceImplTest { | ||
|
||
@InjectMocks | ||
private CarServiceImpl carService; | ||
|
||
@Mock | ||
private CarEntityRepository repository; | ||
|
||
@Test | ||
void testGetCarEntityWithCarId() { | ||
CarEntity mockCarEntity = new CarEntity(); | ||
when(repository.findByCarId(any(Integer.class))).thenReturn(mockCarEntity); | ||
|
||
CarEntity result = carService.getCarEntityWithCarId(1); | ||
|
||
assertEquals(mockCarEntity, result); | ||
} | ||
|
||
@Test | ||
void testGetCarEntityWithBrandName() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(repository.findByBrand(any(String.class))).thenReturn(mockCarEntities); | ||
|
||
List<CarEntity> result = carService.getCarEntityWithBrandName("Toyota"); | ||
|
||
assertEquals(mockCarEntities, result); | ||
} | ||
|
||
@Test | ||
void testGetCarEntityWithCarPrice() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(repository.findByPriceGreaterThanEqual(any(Double.class))).thenReturn(mockCarEntities); | ||
|
||
List<CarEntity> result = carService.getCarEntityWithCarPrice(30000.0); | ||
|
||
assertEquals(mockCarEntities, result); | ||
} | ||
|
||
@Test | ||
void testGetCarEntityWithCarMileage() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(repository.findByMileageGreaterThanEqual(any(Double.class))).thenReturn(mockCarEntities); | ||
|
||
List<CarEntity> result = carService.getCarEntityWithCarMileage(50.5); | ||
|
||
assertEquals(mockCarEntities, result); | ||
} | ||
|
||
@Test | ||
void testGetAllCarEntity() { | ||
List<CarEntity> mockCarEntities = Arrays.asList(new CarEntity(), new CarEntity()); | ||
when(repository.findAll()).thenReturn(mockCarEntities); | ||
|
||
List<CarEntity> result = carService.getAllCarEntity(); | ||
|
||
assertEquals(mockCarEntities, result); | ||
} | ||
|
||
@Test | ||
void testSaveCarEntity() { | ||
CarEntity mockCarEntity = new CarEntity(); | ||
when(repository.save(any(CarEntity.class))).thenReturn(mockCarEntity); | ||
|
||
CarEntity result = carService.saveCarEntity(new CarEntity()); | ||
|
||
assertEquals(mockCarEntity, result); | ||
} | ||
} |
Oops, something went wrong.