Skip to content

Commit

Permalink
added DeviceController Integration Test
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-nowak authored and ogedo committed Apr 19, 2023
1 parent 611e638 commit 7f9f560
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

import com.amigoscode.devicemanagement.BaseIT;
import com.amigoscode.devicemanagement.TestDeviceFactory;
import com.amigoscode.devicemanagement.TestMeasurementFactory;
import com.amigoscode.devicemanagement.TestUserFactory;
import com.amigoscode.devicemanagement.api.response.ErrorResponse;
import com.amigoscode.devicemanagement.domain.device.DeviceService;
import com.amigoscode.devicemanagement.domain.device.model.Device;
import com.amigoscode.devicemanagement.domain.measurement.MeasurementService;
import com.amigoscode.devicemanagement.domain.measurement.model.Measurement;
import com.amigoscode.devicemanagement.domain.measurement.model.PageMeasurement;
import com.amigoscode.devicemanagement.domain.user.UserService;
import com.amigoscode.devicemanagement.domain.user.model.User;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

Expand All @@ -27,6 +33,9 @@ class DeviceControllerIT extends BaseIT {
@Autowired
UserService userService;

@Autowired
MeasurementService measurementService;

@Autowired
DeviceService deviceService;

Expand Down Expand Up @@ -371,6 +380,49 @@ void admin_should_be_able_to_delete_device() {
Assertions.assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
}

@Test
void measurements_made_by_a_given_device_should_be_deleted_when_this_device_is_removed() {
//given
String adminAccessToken = getTokenForAdmin();
Device device = TestDeviceFactory.createDevice();
deviceService.save(device, "creatorId");
Measurement measurement1 = TestMeasurementFactory.createRandom();
Measurement measurement2 = TestMeasurementFactory.createRandom();
Measurement measurement3 = TestMeasurementFactory.createRandom();
Measurement measurement4 = TestMeasurementFactory.createRandom();
Measurement measurement5 = TestMeasurementFactory.createRandom();
measurement1.setDeviceId(device.getId());
measurement2.setDeviceId(device.getId());
measurement3.setDeviceId(device.getId());
measurement4.setDeviceId(device.getId() + "1234");
measurement5.setDeviceId(device.getId() + "1234");
measurementService.save(measurement1);
measurementService.save(measurement2);
measurementService.save(measurement3);
measurementService.save(measurement4);
measurementService.save(measurement5);

//when
var response = callHttpMethod(HttpMethod.DELETE,
"/api/v1/devices/" + device.getId(),
adminAccessToken,
deviceDtoMapper.toDto(device),
Void.class);

//then
Assertions.assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());

int page = 0;
int size = 3;
Pageable pageable = PageRequest.of(page, size);
final PageMeasurement deviceMeasurements = measurementService.findAllByDeviceId(pageable, device.getId());
Assertions.assertEquals(0, deviceMeasurements.getTotalElements());

final PageMeasurement allMeasurements = measurementService.findAll(pageable);
Assertions.assertEquals(2, allMeasurements.getTotalElements());

}

@Test
void device_owner_should_not_be_able_to_delete_device() {
//given
Expand Down

0 comments on commit 7f9f560

Please sign in to comment.