Files
2026-02-03 11:46:03 +01:00

129 lines
4.2 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/controller/base_controller.dart';
import 'package:fotodocumentation/controller/customer_controller.dart';
import 'package:fotodocumentation/dto/customer_dto.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/http_client_utils.dart';
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
import '../testing/test_http_client_utils.dart';
import '../testing/test_utils.mocks.dart';
void main() {
DiContainer.instance.initState();
var jwtTokenStorage = MockJwtTokenStorage();
when(jwtTokenStorage.getAccessToken()).thenAnswer((_) => null);
DiContainer.instance.put(JwtTokenStorage, jwtTokenStorage);
CustomerController controller = CustomerControllerImpl();
group('PictureControllerTest', () {
test('returns a list of customers', () async {
final client = MockClient();
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
when(client.get(Uri.parse('http://localhost:8080/api/customer?query=&startsWith='), headers: {"Accept-Language": "en-US"})).thenAnswer((_) async => http.Response(_customersJson, 200));
var dtos = await controller.getAll("", "");
expect(dtos, isA<List<CustomerListDto>>());
expect(dtos.length, 3);
});
test('throws an exception if the http call completes with an error', () async {
final client = MockClient();
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
when(client.get(Uri.parse('http://localhost:8080/api/customer?query=&startsWith='), headers: {"Accept-Language": "en-US"})).thenAnswer((_) async => http.Response('Not Found', 404));
expect(() async => await controller.getAll("", ""), throwsA(isA<ServerError>()));
});
});
test('returns a customer', () async {
final client = MockClient();
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
when(client.get(Uri.parse('http://localhost:8080/api/customer/4'), headers: {"Accept-Language": "en-US"}))
.thenAnswer((_) async => http.Response(_customerJson, 200));
var dto = await controller.get(id: 4);
expect(dto, isA<CustomerDto>());
});
test('export a customer', () async {
final client = MockClient();
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
when(client.get(Uri.parse('http://localhost:8080/api/customer/export/4'), headers: {"Accept-Language": "en-US"}))
.thenAnswer((_) async => http.Response(_customerJson, 200));
var dto = await controller.export(customerId: 4);
expect(dto, isA<List<int>>());
});
test('export a customer picture', () async {
final client = MockClient();
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
when(client.get(Uri.parse('http://localhost:8080/api/customer/export/4?picture=1'), headers: {"Accept-Language": "en-US"}))
.thenAnswer((_) async => http.Response(_customerJson, 200));
var dto = await controller.export(customerId: 4, pictureId: 1);
expect(dto, isA<List<int>>());
});
}
String _customersJson = '''[
{
"id": 1,
"name": "Müller Apotheke",
"customerNumber": "1234",
"lastUpdateDate": 1729764570000
},
{
"id": 2,
"name": "Meier Apotheke",
"customerNumber": "2345",
"lastUpdateDate": 1729764570000
},
{
"id": 3,
"name": "Schmidt Apotheke",
"customerNumber": "3456",
"lastUpdateDate": 1729764570000
}
]''';
String _customerJson = '''{
"id": 1,
"name": "Müller Apotheke",
"customerNumber": "1234",
"pictures": [
{
"id": 1,
"comment": "good looking picture 1",
"category": null,
"pictureDate": 1729764570000,
"evaluation": 1,
"username": "verboomp",
"imageUrl": "",
"normalSizeUrl": "",
"thumbnailSizeUrl": ""
},
{
"id": 2,
"comment": "good looking picture 2",
"category": null,
"pictureDate": 1729764570000,
"evaluation": 1,
"username": "verboomp",
"imageUrl": "",
"normalSizeUrl": "",
"thumbnailSizeUrl": ""
}
]
}''';