Skip to content

Commit

Permalink
Test and fix sendPrimitive
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Apr 4, 2024
1 parent 0ed4bea commit e0d4bbf
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
working-directory: packages/${{ matrix.package }}

- name: Run build_runner
if: contains(fromJson('[ "kiota_abstractions" ]'), matrix.package)
if: contains(fromJson('[ "kiota_abstractions", "kiota_http" ]'), matrix.package)
run: dart run build_runner build --delete-conflicting-outputs
working-directory: packages/${{ matrix.package }}

Expand Down
17 changes: 17 additions & 0 deletions .run/watch kiota_abstractions.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="watch kiota_abstractions" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="dart run build_runner watch" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/packages/kiota_abstractions/" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="build_runner watch" type="ShConfigurationType">
<configuration default="false" name="watch kiota_http" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="dart run build_runner watch" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/packages/kiota_http/" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
Expand Down
61 changes: 23 additions & 38 deletions packages/kiota_http/lib/src/http_client_request_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,45 +247,30 @@ class HttpClientRequestAdapter implements RequestAdapter {
return null;
}

if (ModelType is bool?) {
return rootNode.getBoolValue() as ModelType;
}

if (ModelType is int?) {
return rootNode.getIntValue() as ModelType;
}

if (ModelType is double?) {
return rootNode.getDoubleValue() as ModelType;
}

if (ModelType is String?) {
return rootNode.getStringValue() as ModelType;
}

if (ModelType is DateTime?) {
return rootNode.getDateTimeValue() as ModelType;
}

if (ModelType is DateOnly?) {
return rootNode.getDateOnlyValue() as ModelType;
}

if (ModelType is TimeOnly?) {
return rootNode.getTimeOnlyValue() as ModelType;
}

if (ModelType is Duration?) {
return rootNode.getDurationValue() as ModelType;
}

if (ModelType is UuidValue?) {
return rootNode.getGuidValue() as ModelType;
switch (ModelType) {
case const (bool):
return rootNode.getBoolValue() as ModelType;
case const (int):
return rootNode.getIntValue() as ModelType;
case const (double):
return rootNode.getDoubleValue() as ModelType;
case const (String):
return rootNode.getStringValue() as ModelType;
case const (DateTime):
return rootNode.getDateTimeValue() as ModelType;
case const (DateOnly):
return rootNode.getDateOnlyValue() as ModelType;
case const (TimeOnly):
return rootNode.getTimeOnlyValue() as ModelType;
case const (Duration):
return rootNode.getDurationValue() as ModelType;
case const (UuidValue):
return rootNode.getGuidValue() as ModelType;
default:
throw ArgumentError(
'The type $ModelType is not supported for primitive deserialization',
);
}

throw ArgumentError(
'The type $ModelType is not supported for primitive deserialization',
);
}

@override
Expand Down
2 changes: 2 additions & 0 deletions packages/kiota_http/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dependencies:
dev_dependencies:
strict: ^2.0.0
test: ^1.25.2
mockito: ^5.4.4
build_runner: ^2.4.8
7 changes: 0 additions & 7 deletions packages/kiota_http/test/dummy_test.dart

This file was deleted.

67 changes: 67 additions & 0 deletions packages/kiota_http/test/http_client_request_adapter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:typed_data';

import 'package:http/http.dart' as http;
import 'package:kiota_abstractions/kiota_abstractions.dart';
import 'package:kiota_http/kiota_http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'http_client_request_adapter_test.mocks.dart';

@GenerateMocks([
http.Client,
AuthenticationProvider,
ParseNodeFactory,
SerializationWriterFactory,
ParseNode,
])
void main() {
group('HttpClientRequestAdapter', () {
test('sendPrimitive String', () async {
final client = MockClient();
final authProvider = MockAuthenticationProvider();
final pNodeFactory = MockParseNodeFactory();
final sWriterFactory = MockSerializationWriterFactory();
final parseNode = MockParseNode();

const stringContent = 'Hello, World!';
final contentBytes = Uint8List.fromList(stringContent.codeUnits);

final adapter = HttpClientRequestAdapter(
client: client,
authProvider: authProvider,
pNodeFactory: pNodeFactory,
sWriterFactory: sWriterFactory,
);

final info = RequestInformation(
httpMethod: HttpMethod.get,
urlTemplate: 'https://example.test/primitive',
);

when(authProvider.authenticateRequest(info))
.thenAnswer((_) async => Future<void>.value());

when(client.send(any)).thenAnswer((_) async {
return http.StreamedResponse(
Stream.fromIterable([contentBytes]),
200,
headers: {
'content-type': 'text/plain',
},
);
});

when(pNodeFactory.getRootParseNode('text/plain', contentBytes))
.thenReturn(parseNode);

when(parseNode.getStringValue()).thenReturn(stringContent);

final stringResult = await adapter.sendPrimitive<String>(info);

expect(stringResult, isNotNull);
expect(stringResult, stringContent);
});
});
}
Loading

0 comments on commit e0d4bbf

Please sign in to comment.