GoLang how to test without static mock data - unit-testing

Good day!
How to test code written in Go, which decrypts payload from some API, which I can't change?
Static mock data is not actual, since there is a signature and expiration date inside.
Also have idea to write some API to retrieve fresh payload data, but writing API to test seems to be bad idea.
Can you share ideas to test method of decrypting?

You can try to create a program that consume such api and save different files.
“Golden files”
Then use it in tests.
Expiration is not a problem if you save the moment that you perform the first request and use it instead “time.Now()”

Related

Calling a method in a java web service, with an object as parameter, from c# client

I have written a web service in java with axis2 and tomcat, and also a client in c# which I have successfully called methods on the web server with.
But I am struggling with a method, which has to be able to take in any Object as a parameter. A float, string, random file or picture.
I tried making a serializable object in c#, which I can create and put my String inside and then send it as a parameter to my web service funtion.
But I get: Exception thrown: 'System.InvalidOperationException' in System.Xml.dll
How should I solve this? I guess I have to serialize it first, but I dont get how I do that and then send the serialized object as a parameter to the function. And does this object have to be defined on the web service too?
You are going to run into problems if you want to send "any" C# object to Java. C# objects do not easily reduce to a Java object. They are different languages and each have some unique features in their object model. Instead, find an language neutral format (like Json) that can capture whatever type of data you need. Common data formats like Int, String, Boolean, etc are easily captured in JSon. More complex objects can be created as well with JSON.
So, I recommend the following. For each C# object you want to send across the wire, have it implement a toJson method, which takes it state and dumps it into JSON, transmit the JSON and deserilize as a Java Object. This should cover most cases.

how to mock file copy in a functional test

I have a controller which duty is copying a file passed along with the request (through a body POST) to a specific path under web/images. The path is specified by a property living into the specific Controller.
What I would like to do is testing it with a functional test, but I wouldn't like it to overwrite files in my project, so I would like to use vfs or change the path before my test case sends the request.
Is there a good-straight way to accomplish this?
A common approach is to load configuration that may change between environments as an environmental variable. (I have not ever used symfony before, so there may be tools to help with env vars)
The upload path could then be
$upload_path = getenv('WEB_IMAGE_UPLOAD_PATH') ?
getenv('WEB_IMAGE_UPLOAD_PATH') : 'web/images'
This will allow you to specify a temp (/tmp ?) directory when starting up your server in integration mode.
Ah cool, (disclaimer: i'm not a php person) it looks like php has IO streams that may be able to help in functional testing, and allow easy cleanup.
http://php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-unknown-unknown-descriptios
I believe you may be able to set your 'WEB_IMAGE_UPLOAD_PATH' to be one of those streams
I'll try to answer myself: I refactored my code in order to have a property that specifies the path I would like to copy/overwrite my file.
Then, inside a PHPUnit class I replace the object property's value with a vfsStream path. By doing like that I get the behavior I need, without touching my real files/paths. Everything will live inside the virtual file system and my object will use it.
Parameters are important for a clean and reusable code, but even more when you want to unit-test: I think Unit testing is helping me to force to parameterize everything in place of relapsing to hardcoding when you don't have so much time. In order to help me writing unit tests I created a class that accesses methods and properties, irrespective of their accessibility.
PHPUnitUtils
I'm quite sure there's already something more sophisticated, but this class fullfills my needs in this very moment. Hope it helps :-)

Retrieve data from Account Server

I'm trying to make a game launcher in C++ and I was wondering if I could get some guidance on how to carry out this task. Basically I've created an API which outputs the account data in JSON format.
i.e {"success":true,"errorCode":0,"reason":"You're now logged in!"}
http_fetch("http://www.example.com/api/login.php?username="+username+"&password="+password+"");
How am I able to retrieve the data?
Sorry if you don't understand. English isn't my first language :)
-brownzilla
Look for a library that allows you to parse Json. Some examples:
Picojson
Rapidjson
Both are quite easy to use and allow you to turn json into a model that can later be used to map to your own model. Alternatively you could write your own Json parser though that would be a bit of work (reinventing the wheel perhaps).

Best way to use a unit test to read and then delete xml files

I created a unit test in which I dynamically create and then parse an xml. When I'm finished with the file I delete it. I'm storing the file momentarily in a created resource folder within my project,but I want to know will this still pass if I deploy to a tomcat server. I'm using getRealPath () right now and it works. I in no way need these files later on which is why I'm deleting them.
I've read the getRealPath () isn't portable and shouldn't really be used but that's why I'm asking for my purpose would it be ok?
I can't post code because I'm at work but I'll try to explain somewhat:
I use ServletContextHolder.servletcontext.getRealPath () and add resources/testfiles to the end..this takes me to my project path (project/out/test/resources/testfiles)
I create an xml file using stringwriter,filewriter,markupbuilder..
I save this file, read it and delete it after the test..it works on windows but I need to know if it'll work on tomcat if it is deployed and the unit tests run automatically..will it be able to do all this..
Apologies for poor format my phone isn't the best way to write this
The ideal solution would be to not use files at all. But it really depends on what class your XML parser uses for its input. For example, if the parser accepts an InputStream, you can use a StringBufferInputStream to build your XML content. The parser would then be able to use that stream as if it were a file. OOP interfaces are awesome like that :)

Are there tools for generating the clients model from an odata metadata?

Back in the good old days of flex (anyone?) flash builder provided a tool for generating the clients model based on the server model. Is there something similar for generating, say an ember's app model, based on the odata metadata?
datajs documentation does mention the subject. Though the reference for OData.read used in the sample doesn't say explicitly that it interprets the metadata somehow, it seems implied. You'll have to verify that.
It does take an optional metadata object however, suggesting there exists a formal representation for metadata to the library -- I would imagine generated via OData.read. Documentation seems non-existent. I don't know what that looks like.
From there, you should be able to further transform the model to something suitable for Ember.
(datajs is a low-level javascript library that implements client-side OData operations.)
I also know that JayStack provides a JaySvcUtil, a CLI process assembly (.NET program) that extracts metadata. The destination format is JavaScript code, though the model it uses is specific to JayData. Still, you may be able to work from there.
As mentioned by Maya, Microsoft provides the OData Client Code Generator, which generates .NET proxies. Might be more difficult to transform that.
If none of these work for you (which is actually likely), you can always parse the $metadata resource yourself -- I believe it always uses an XML representation in all current versions of OData.
If you need to do it dynamically in the browser, use DOMParser or XMLHttpRequest. More information.
If you can do it statically, then by all means do so -- it's simply best for performance. In this case, you can use whatever language and runtime tools you want to fetch, parse, transform and re-serialize the model.
The format (CSDL) is specified for OData here (v4) and here (v3).
Finally, check out this list, something new may appear that better fits your needs.
There are two suggestion which may help you.
1, OData provide client code generator to generate client-side proxy class. Just need to pass metadata url, .net client code will be generate for you. You can follow the following blog:
http://blogs.msdn.com/b/odatateam/archive/2014/03/11/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx
2, If the model means "EdmModel", you can just de-serialize $metadata. OData reader can de-serialize the $metadata to IEdmModel, which can be used in client side. The following is sample code:
HttpWebRequestMessage message = new HttpWebRequestMessage(new Uri(ServiceBaseUri.AbsoluteUri + "$metadata", UriKind.Absolute));
message.SetHeader("Accept", MimeTypes.ApplicationXml);
using (var messageReader = new ODataMessageReader(message.GetResponse()))
{
Model = messageReader.ReadMetadataDocument();
}