Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kundan59 committed Jan 13, 2024
2 parents 8da324a + 6007793 commit 89f9e75
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void consumeEvent(BasicAcknowledgeablePubsubMessage basicAcknowledgeableP
basicAcknowledgeablePubsubMessage.ack();
}

private void processMessage(BasicAcknowledgeablePubsubMessage message) {
public void processMessage(BasicAcknowledgeablePubsubMessage message) {
CarEntity[] gcHubMessageArray;
String eventMsgString = "";
try {
Expand Down
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());
}

}
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());
}

}
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);
}

}
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();
}
}
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);
}
}
Loading

0 comments on commit 89f9e75

Please sign in to comment.