-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathOpenApiAiAdaptiveCardExtensionTests.cs
More file actions
162 lines (155 loc) · 5.16 KB
/
Copy pathOpenApiAiAdaptiveCardExtensionTests.cs
File metadata and controls
162 lines (155 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Kiota.Builder.Configuration;
using Kiota.Builder.OpenApiExtensions;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Writers;
using Moq;
using Xunit;
namespace Kiota.Builder.Tests.OpenApiExtensions;
public sealed class OpenApiAiAdaptiveCardExtensionTest : IDisposable
{
private readonly HttpClient _httpClient = new();
public void Dispose()
{
_httpClient.Dispose();
}
[Fact]
public void Parses()
{
var oaiValueRepresentation =
"""
{
"data_path": "$.items",
"file": "path_to_file",
"title": "title",
"url": "https://example.com"
}
""";
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(oaiValueRepresentation));
var oaiValue = JsonNode.Parse(stream);
var value = OpenApiAiAdaptiveCardExtension.Parse(oaiValue);
Assert.NotNull(value);
Assert.Equal("$.items", value.DataPath);
Assert.Equal("path_to_file", value.File);
Assert.Equal("title", value.Title);
Assert.Equal("https://example.com", value.Url);
}
private readonly string TempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
[Fact]
public async Task ParsesInDocumentAsync()
{
var documentContent = @"openapi: 3.0.0
info:
title: Graph Users
version: 0.0.0
servers:
- url: https://graph.microsoft.com/v1.0
description: The Microsoft Graph API
tags: []
paths:
/users:
get:
operationId: getUsers
parameters: []
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
x-ai-adaptive-card:
data_path: $.users
file: path_to_file
title: title
url: https://example.com
/users/{id}:
get:
operationId: getUser
parameters:
- name: id
in: path
required: true
description: The user id
schema:
type: string
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
$ref: '#/components/schemas/User'
x-ai-adaptive-card:
data_path: $.user
file: path_to_file
title: title
url: https://example.com
components:
schemas:
User:
type: object
required:
- id
- displayName
properties:
id:
type: string
displayName:
type: string";
Directory.CreateDirectory(TempDirectory);
var documentPath = Path.Combine(TempDirectory, "document.yaml");
await File.WriteAllTextAsync(documentPath, documentContent);
var mockLogger = new Mock<ILogger<OpenApiAiAdaptiveCardExtension>>();
var documentDownloadService = new OpenApiDocumentDownloadService(_httpClient, mockLogger.Object);
var generationConfig = new GenerationConfiguration { OutputPath = TempDirectory, PluginTypes = [PluginType.APIPlugin] };
var (openApiDocumentStream, _) = await documentDownloadService.LoadStreamAsync(documentPath, generationConfig);
var document = await documentDownloadService.GetDocumentFromStreamAsync(openApiDocumentStream, generationConfig);
Assert.NotNull(document);
Assert.NotNull(document.Paths);
Assert.NotNull(document.Paths["/users"].Operations.FirstOrDefault().Value.Extensions);
Assert.True(document.Paths["/users"].Operations.FirstOrDefault().Value.Extensions.TryGetValue(OpenApiAiAdaptiveCardExtension.Name, out var adaptiveCardExtension));
Assert.NotNull(adaptiveCardExtension);
}
[Fact]
public void Serializes()
{
var value = new OpenApiAiAdaptiveCardExtension
{
DataPath = "$.items",
File = "path_to_file",
Title = "title",
Url = "https://example.com"
};
using var sWriter = new StringWriter();
OpenApiJsonWriter writer = new(sWriter, new OpenApiJsonWriterSettings { Terse = true });
value.Write(writer, OpenApiSpecVersion.OpenApi3_0);
var result = sWriter.ToString();
Assert.Equal("{\"data_path\":\"$.items\",\"file\":\"path_to_file\",\"title\":\"title\",\"url\":\"https://example.com\"}", result);
}
[Fact]
public void WritesNothingForEmptyAdaptiveCard()
{
var value = new OpenApiAiAdaptiveCardExtension
{
DataPath = null,
File = null,
Title = null,
Url = null
};
using var sWriter = new StringWriter();
OpenApiJsonWriter writer = new(sWriter, new OpenApiJsonWriterSettings { Terse = true });
value.Write(writer, OpenApiSpecVersion.OpenApi3_0);
var result = sWriter.ToString();
// When required properties are null/empty, nothing should be written
Assert.Equal(string.Empty, result);
}
}