Skip to content

Commit

Permalink
Extend the coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Pavlovskiy committed Aug 19, 2023
1 parent bc767d9 commit 09fe26f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"cSpell.words": ["carres", "Instancio", "Keanu", "Keycloak"],
"java.test.config": {
"name": "main",
"workingDirectory": "${workspaceFolder}",
"workingDirectory": "${workspaceFolder}/apps/backend",
"env": {
"SPRING_PROFILES_ACTIVE": "test"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.nikitades.carres.domain.Notifier;
import com.nikitades.carres.shared.NewReservationNotification;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AmqpNotifier implements Notifier {

@Autowired
private RabbitTemplate rabbitTemplate;
private final RabbitTemplate rabbitTemplate;

public void notifyOfNewReservation(String email, String manufacturer, String model) {
rabbitTemplate.convertAndSend(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,29 @@ void whenAvailableCarsListIsRequested_listIsShown() throws Exception {
.andExpect(jsonPath("$.items").exists())
.andExpect(jsonPath("$.items.length()").value(3));
}

@Test
@Transactional
@WithMockJwt(MockJwt.Nikita)
void whenCarIsRequestedById_andCarIsPresentInDb_carIsShown() throws Exception {
//Given that some cars exist in the database
Car car = Instancio
.of(Car.class)
.set(field(Car::getCreatedBy), Users.Nikita.getId())
.set(field(Car::isAvailable), true)
.create();

carRepository.save(car);

//when an authorized user requests this car (e.g. at the time of the reservation)
var actual = mvc.perform(get("/api/v1/cars/%s".formatted(car.getId())));

//then the car is displayed in the regular fashion:
actual
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(car.getId().toString()))
.andExpect(jsonPath("$.manufacturer").value(car.getManufacturer()))
.andExpect(jsonPath("$.model").value(car.getModel()))
.andExpect(jsonPath("$.manufacturedAt").value(car.getManufacturedAt().toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.nikitades.carres.infrastructure.amqp;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.nikitades.carres.shared.NewReservationNotification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

@ExtendWith(MockitoExtension.class)
class AmqpNotifierTest {

private AmqpNotifier instance;

@Mock
private RabbitTemplate rabbitTemplate;

@BeforeEach
void init() {
instance = new AmqpNotifier(rabbitTemplate);
}

@Test
void whenNewReservationNotificationIsSent_correctQueueNameIsPassedAsRoutingKey() {
//Given that [email protected] reserved Centaur Warrunner
instance.notifyOfNewReservation("[email protected]", "Centaur", "Warrunner");

//When a notification is performed
verify(rabbitTemplate, times(1))
.convertAndSend(
"new_reservations",
//correct data is passed
new NewReservationNotification("[email protected]", "Centaur", "Warrunner")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.rabbitmq.template.default-receive-queue=
spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=guest
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

@SpringBootTest
class TgnotifyApplicationTests {

@MockBean
private WebhookCarresNotificationBot bot;

@Test
void contextLoads() {}
}

0 comments on commit 09fe26f

Please sign in to comment.