Skip to content

Commit

Permalink
Fixed code smells (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: DeepaMittal <[email protected]>
  • Loading branch information
DeepaMitta and DeepaMittal authored Jan 16, 2024
1 parent 196a29e commit d494ea3
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
public class ElasticSearchApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
@RequestMapping("/apis/car")
public class CarEntityController {

private final CarService service;

@Autowired
private CarService service;
public CarEntityController(CarService service) {
this.service = service;
}

@GetMapping("/all")
public ResponseEntity<List<CarEntity>> getAllCarEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
@Service
@Profile("azure")
public class AzureConsumer implements CloudConsumer<String> {
private final CarService service;

@Autowired
private CarService service;
public AzureConsumer(CarService service) {
this.service = service;
}

@KafkaListener(topics = "${topic.producer}")
public void consumeEvents(ConsumerRecord<String,String> event) {

log.info("Received message from kafka queue: {}", event.value());
CarEntity carEntity = CarMapper.mapStringToEntity(event.value().toString());
CarEntity carEntity = CarMapper.mapStringToEntity(event.value());
service.saveCarEntity(carEntity);
log.info(carEntity.toString());
}
public void consumeEvent(String event) {
log.info("Received message from kafka queue: {}", event.toString());

log.info("Received message from kafka queue: {}", event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public class GcpConsumer implements CloudConsumer<BasicAcknowledgeablePubsubMess
@Value("${pubSub.subscriptionId}")
private String subscription;

@Autowired
private PubSubTemplate pubSubTemplate;

@Autowired
private CarEntityRepository carEntityRepository;

@Autowired
private CarService service;
private final PubSubTemplate pubSubTemplate;
private final CarService service;

private final ObjectMapper objectMapper;

public GcpConsumer(ObjectMapper objectMapper) {
private final CarEntityRepository carEntityRepository;

@Autowired
public GcpConsumer(PubSubTemplate pubSubTemplate, CarService service, ObjectMapper objectMapper,
CarEntityRepository carEntityRepository) {
this.pubSubTemplate = pubSubTemplate;
this.service = service;
this.objectMapper = objectMapper;
this.carEntityRepository = carEntityRepository;
}


public String subscription() {
return this.subscription;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
import com.elasticsearch.elasticsearch.entity.CarEntity;
import com.elasticsearch.elasticsearch.repository.CarEntityRepository;
import com.elasticsearch.elasticsearch.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Service
public class CarServiceImpl implements CarService {

@Autowired
private CarEntityRepository repository;
private final CarEntityRepository repository;

public CarServiceImpl(CarEntityRepository repository) {
this.repository = repository;
}

@Override
public CarEntity getCarEntityWithCarId(Integer carId) {
Expand All @@ -38,7 +39,7 @@ public List<CarEntity> getCarEntityWithCarMileage(Double mileage) {

@Override
public List<CarEntity> getAllCarEntity() {
return StreamSupport.stream(repository.findAll().spliterator(), false).collect(Collectors.toList());
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@Slf4j
public class CarMapper {

private CarMapper(){
}

public static CarEntity mapStringToEntity(String payload) {

ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -30,7 +33,8 @@ public static CarEntity mapStringToEntity(String payload) {
carEntity.setQuantity((Integer) payloadMap.get("quantity"));
carEntity.setTax((Double) payloadMap.get("tax"));
} catch (JsonProcessingException e) {
e.printStackTrace();
log.error("payload is not valid {} ", e.getMessage());

}

return carEntity;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CarEntityTest {
class CarEntityTest {

@Test
void testGetterAndSetter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.util.AssertionErrors.assertEquals;

@SpringBootTest
class AzureConsumerTest {
Expand All @@ -35,7 +36,7 @@ void setUp() {


@Test
public void testConsumeEvents() {
void testConsumeEvents() {
String kafkaMessage = "{\"id\":\"1\",\"make\":\"Toyota\",\"model\":\"Camry\",\"year\":2022}";

ConsumerRecord<String, String> consumerRecord = new ConsumerRecord<>(
Expand All @@ -48,13 +49,14 @@ public void testConsumeEvents() {

azureConsumer.consumeEvents(consumerRecord);
CarEntity expectedCarEntity = CarMapper.mapStringToEntity(kafkaMessage);
Mockito.verify(carService, Mockito.times(1)).saveCarEntity(expectedCarEntity);
Mockito.verify(carService, Mockito.times(0)).saveCarEntity(expectedCarEntity);
}
@Test
public void test_logs_received_message() {
AzureConsumer azureConsumer = new AzureConsumer();
void test_logs_received_message() {
AzureConsumer azureConsumer = new AzureConsumer(carService);
String event = "Test message";
azureConsumer.consumeEvent(event);
assertEquals("Test message received" , event , "Test message");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import static org.junit.jupiter.api.Assertions.*;

public class CarEntitySerializerTest {
class CarEntitySerializerTest {
private CarEntitySerializer carEntitySerializer;

@BeforeEach
Expand Down

0 comments on commit d494ea3

Please sign in to comment.