// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import 'package:aws_common/aws_common.dart'; import 'package:aws_common/src/config/config_file/terms.dart'; import 'package:json_annotation/json_annotation.dart'; part 'test_models.g.dart'; const serializable = JsonSerializable( explicitToJson: true, includeIfNull: false, ); enum TestPlatform { linux, windows, } @serializable class FileLocationTest with AWSSerializable> { const FileLocationTest({ required this.name, this.environment = const {}, this.languageSpecificHome, required this.platform, this.profile, required this.configLocation, required this.credentialsLocation, }); factory FileLocationTest.fromJson(Map json) => _$FileLocationTestFromJson(json); final String name; final Map environment; final String? languageSpecificHome; final TestPlatform platform; final String? profile; final String configLocation; final String credentialsLocation; @override Map toJson() => _$FileLocationTestToJson(this); } @serializable class ParserTest with AWSSerializable> { const ParserTest({ required this.name, required this.input, required this.output, }); factory ParserTest.fromJson(Map json) => _$ParserTestFromJson(json); final String name; final ParserTestInput input; final ParserTestOutput output; @override Map toJson() => _$ParserTestToJson(this); } @serializable class ParserTestInput with AWSSerializable> { const ParserTestInput({ this.configFile, this.credentialsFile, }); factory ParserTestInput.fromJson(Map json) => _$ParserTestInputFromJson(json); final String? configFile; final String? credentialsFile; @override Map toJson() => _$ParserTestInputToJson(this); } @serializable @NullableAWSProfileFileConverter() class ParserTestOutput with AWSSerializable> { const ParserTestOutput({ this.errorContaining, this.profiles, }); factory ParserTestOutput.fromJson(Map json) => _$ParserTestOutputFromJson(json); final String? errorContaining; final AWSProfileFile? profiles; @override Map toJson() => _$ParserTestOutputToJson(this); } @serializable @AWSProfileFileConverter() class ProfileTest with AWSSerializable>, AWSDebuggable { const ProfileTest({ required this.profiles, required this.regionTests, required this.credentialsTests, }); factory ProfileTest.fromJson(Map json) => _$ProfileTestFromJson(json); final AWSProfileFile profiles; final ProfileTestCase regionTests; final List credentialsTests; @override Map toJson() => _$ProfileTestToJson(this); @override String get runtimeTypeName => 'ProfileTest'; } @serializable class ProfileTestCase { const ProfileTestCase({ required this.name, required this.profile, required this.output, }); factory ProfileTestCase.fromJson(Map json) => _$ProfileTestCaseFromJson(json); final String name; final String profile; final ProfileTestOutput output; Map toJson() => _$ProfileTestCaseToJson(this); } enum AWSCredentialsType { assumeRole, session, basic, } @serializable class ProfileTestOutput { const ProfileTestOutput({ this.region, this.credentialType, }); factory ProfileTestOutput.fromJson(Map json) => _$ProfileTestOutputFromJson(json); final String? region; final AWSCredentialsType? credentialType; Map toJson() => _$ProfileTestOutputToJson(this); } class AWSProfileFileConverter implements JsonConverter> { const AWSProfileFileConverter(); @override AWSProfileFile fromJson(Map json) { return AWSProfileFile.build((b) { for (final profile in json.entries) { final profileName = profile.key; final profileMap = profile.value as Map; b.profiles[profileName] = AWSProfile.build((b) { b.name = profileName; for (final property in profileMap.entries) { final propertyName = property.key; b.properties[propertyName] = AWSProperty.build((b) { b.name = propertyName; final propertyValue = property.value as String; if (propertyValue.startsWith(lineBreakExp)) { for (final propLine in propertyValue.split(lineBreakExp).skip(1)) { final name = propLine.split('=')[0].trim(); final value = propLine.split('=')[1].trim(); b.value = ''; b.subProperties[name] = AWSProperty( name: name, value: value, ); } } else { b.value = propertyValue; } }); } }); } }); } @override Map toJson(AWSProfileFile object) => object.toJson()['profiles'] as Map; } class NullableAWSProfileFileConverter implements JsonConverter?> { const NullableAWSProfileFileConverter(); @override AWSProfileFile? fromJson(Map? json) { if (json == null) { return null; } return const AWSProfileFileConverter().fromJson(json); } @override Map? toJson(AWSProfileFile? object) => object == null ? null : const AWSProfileFileConverter().toJson(object); }