1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright company="Aspose Pty Ltd">
3+ // Copyright (c) 2003-2022 Aspose Pty Ltd
4+ // </copyright>
5+ // <summary>
6+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7+ // of this software and associated documentation files (the "Software"), to deal
8+ // in the Software without restriction, including without limitation the rights
9+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ // copies of the Software, and to permit persons to whom the Software is
11+ // furnished to do so, subject to the following conditions:
12+ //
13+ // The above copyright notice and this permission notice shall be included in all
14+ // copies or substantial portions of the Software.
15+ //
16+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ // SOFTWARE.
23+ // </summary>
24+ // --------------------------------------------------------------------------------------------------------------------
25+
26+ using GroupDocs . Conversion . Cloud . Sdk . Api ;
27+ using GroupDocs . Conversion . Cloud . Sdk . Client ;
28+ using GroupDocs . Conversion . Cloud . Sdk . Model . Requests ;
29+
30+ namespace GroupDocs . Conversion . Cloud . Sdk . Test . Api
31+ {
32+ using GroupDocs . Conversion . Cloud . Sdk . Test . Api . Internal ;
33+ using Newtonsoft . Json ;
34+ using NUnit . Framework ;
35+ using System . IO ;
36+ using System . Reflection ;
37+ using System . Text ;
38+ using System ;
39+ using GroupDocs . Conversion . Cloud . Sdk . Model ;
40+
41+ public class ThirdPartyStorageTests
42+ {
43+ private readonly string _appSid = Config . AppSid ;
44+ private readonly string _appKey = Config . AppKey ;
45+ private readonly string _apiBaseUrl = Config . ApiBaseUrl ;
46+
47+ protected ConvertApi ConvertApi ;
48+ protected InfoApi InfoApi ;
49+ protected FileApi FileApi ;
50+ protected FolderApi FolderApi ;
51+ protected StorageApi StorageApi ;
52+
53+ [ OneTimeSetUp ]
54+ public void BeforeAllTests ( )
55+ {
56+ var config = new Configuration ( _appSid , _appKey )
57+ {
58+ ApiBaseUrl = _apiBaseUrl
59+ } ;
60+
61+ ConvertApi = new ConvertApi ( config ) ;
62+ InfoApi = new InfoApi ( config ) ;
63+ FileApi = new FileApi ( config ) ;
64+ FolderApi = new FolderApi ( config ) ;
65+ StorageApi = new StorageApi ( config ) ;
66+ }
67+
68+ /// <summary>
69+ /// Test Conversion using Third Party Storage
70+ /// </summary>
71+ [ Test ]
72+ public void TestConversionAtThirdPartyStorage ( )
73+ {
74+ var testFile = TestFiles . FourPagesDocx ;
75+ var storageName = "week" ;
76+ var targetFormat = "pdf" ;
77+
78+ var deleteRequest = new DeleteFileRequest { path = testFile . FullName , storageName = storageName } ;
79+ var existsRequest = new ObjectExistsRequest { path = testFile . FullName , storageName = storageName } ;
80+ var uploadRequest = new UploadFileRequest ( testFile . FullName , GetTestFileStream ( testFile ) , storageName ) ;
81+
82+ FileApi . DeleteFile ( deleteRequest ) ;
83+ var response = StorageApi . ObjectExists ( existsRequest ) ;
84+ Assert . IsFalse ( response . Exists ) ;
85+ FileApi . UploadFile ( uploadRequest ) ;
86+ response = StorageApi . ObjectExists ( existsRequest ) ;
87+ Assert . IsTrue ( response . Exists ) ;
88+
89+ var settings = new ConvertSettings
90+ {
91+ FilePath = testFile . FullName ,
92+ StorageName = storageName ,
93+ Format = targetFormat ,
94+ OutputPath = "converted.pdf"
95+ } ;
96+
97+ var result = ConvertApi . ConvertDocument ( new ConvertDocumentRequest ( settings ) ) ;
98+
99+ Assert . NotNull ( result ) ;
100+ Assert . Greater ( result . Count , 0 ) ;
101+ var convertedExtension = Path . GetExtension ( result [ 0 ] . Name ) ;
102+ Assert . NotNull ( convertedExtension ) ;
103+ Assert . AreEqual ( targetFormat , convertedExtension . Substring ( 1 ) ) ;
104+
105+ var request = new DownloadFileRequest { path = result [ 0 ] . Path , storageName = storageName } ;
106+
107+ var dResponse = FileApi . DownloadFile ( request ) ;
108+ Assert . Greater ( dResponse . Length , 0 ) ;
109+ }
110+
111+
112+
113+ ////////////////////////////////////////////////////////////////////////////////////////////////////////
114+ private byte [ ] GetTestFileBytes ( TestFile file )
115+ {
116+ var filePath = Path . Combine ( GetTestDataPath ( ) , file . Folder , file . FileName ) ;
117+ if ( ! File . Exists ( filePath ) )
118+ {
119+ throw new FileNotFoundException ( "File not found: " + filePath ) ;
120+ }
121+
122+ return File . ReadAllBytes ( filePath ) ;
123+ }
124+
125+ protected Stream GetTestFileStream ( TestFile file )
126+ {
127+ var bytes = GetTestFileBytes ( file ) ;
128+
129+ return new MemoryStream ( bytes ) ;
130+ }
131+
132+ // ReSharper disable once UnusedMember.Global
133+ protected Stream SerializeObject ( object obj )
134+ {
135+ var options = new JsonSerializerSettings
136+ {
137+ NullValueHandling = NullValueHandling . Ignore
138+ } ;
139+ var json = JsonConvert . SerializeObject ( obj , options ) ;
140+
141+ var bytes = Encoding . UTF8 . GetBytes ( json ) ;
142+
143+ return new MemoryStream ( bytes ) ;
144+ }
145+
146+ private static string GetTestDataPath ( )
147+ {
148+ var uri = new Uri ( Assembly . GetExecutingAssembly ( ) . CodeBase ) ;
149+ var workingDir = Path . GetDirectoryName ( uri . LocalPath ) ?? Directory . GetCurrentDirectory ( ) ;
150+
151+ var baseDir = Path . Combine ( workingDir , "Resources" , "TestData" ) ;
152+
153+ return Path . GetFullPath ( baseDir ) ;
154+ }
155+ }
156+ }
0 commit comments