1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2015 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using Google.Protobuf.TestProtos; 34 using NUnit.Framework; 35 36 namespace Google.Protobuf.WellKnownTypes 37 { 38 public class AnyTest 39 { 40 [Test] Pack()41 public void Pack() 42 { 43 var message = SampleMessages.CreateFullTestAllTypes(); 44 var any = Any.Pack(message); 45 Assert.AreEqual("type.googleapis.com/protobuf_unittest.TestAllTypes", any.TypeUrl); 46 Assert.AreEqual(message.CalculateSize(), any.Value.Length); 47 } 48 49 [Test] Pack_WithCustomPrefix()50 public void Pack_WithCustomPrefix() 51 { 52 var message = SampleMessages.CreateFullTestAllTypes(); 53 var any = Any.Pack(message, "foo.bar/baz"); 54 Assert.AreEqual("foo.bar/baz/protobuf_unittest.TestAllTypes", any.TypeUrl); 55 Assert.AreEqual(message.CalculateSize(), any.Value.Length); 56 } 57 58 [Test] Pack_WithCustomPrefixTrailingSlash()59 public void Pack_WithCustomPrefixTrailingSlash() 60 { 61 var message = SampleMessages.CreateFullTestAllTypes(); 62 var any = Any.Pack(message, "foo.bar/baz/"); 63 Assert.AreEqual("foo.bar/baz/protobuf_unittest.TestAllTypes", any.TypeUrl); 64 Assert.AreEqual(message.CalculateSize(), any.Value.Length); 65 } 66 67 [Test] Unpack_WrongType()68 public void Unpack_WrongType() 69 { 70 var message = SampleMessages.CreateFullTestAllTypes(); 71 var any = Any.Pack(message); 72 Assert.Throws<InvalidProtocolBufferException>(() => any.Unpack<TestOneof>()); 73 } 74 75 [Test] Unpack_Success()76 public void Unpack_Success() 77 { 78 var message = SampleMessages.CreateFullTestAllTypes(); 79 var any = Any.Pack(message); 80 var unpacked = any.Unpack<TestAllTypes>(); 81 Assert.AreEqual(message, unpacked); 82 } 83 84 [Test] Unpack_CustomPrefix_Success()85 public void Unpack_CustomPrefix_Success() 86 { 87 var message = SampleMessages.CreateFullTestAllTypes(); 88 var any = Any.Pack(message, "foo.bar/baz"); 89 var unpacked = any.Unpack<TestAllTypes>(); 90 Assert.AreEqual(message, unpacked); 91 } 92 93 [Test] ToString_WithValues()94 public void ToString_WithValues() 95 { 96 var message = SampleMessages.CreateFullTestAllTypes(); 97 var any = Any.Pack(message); 98 var text = any.ToString(); 99 Assert.That(text, Is.StringContaining("\"@value\": \"" + message.ToByteString().ToBase64() + "\"")); 100 } 101 102 [Test] ToString_Empty()103 public void ToString_Empty() 104 { 105 var any = new Any(); 106 Assert.AreEqual("{ \"@type\": \"\", \"@value\": \"\" }", any.ToString()); 107 } 108 109 [Test] ToString_MessageContainingAny()110 public void ToString_MessageContainingAny() 111 { 112 var message = new TestWellKnownTypes { AnyField = new Any() }; 113 Assert.AreEqual("{ \"anyField\": { \"@type\": \"\", \"@value\": \"\" } }", message.ToString()); 114 } 115 } 116 } 117