I am trying to generate a pact between 2 of our services using pact-JVM. But when I try to run the Java class, I get this exception.
1) I suspect something is wrong with the Pact body, is that correct? There is an extra 'message' parameter in the JSON body of the PactDslWithProvider,but in the runTest1() method, I am equating only the lists and when I inspect the results, they are same to me.
2) Is it correct to provide the actual provider URL in the runTest1() method? (the provider is already in place)
au.com.dius.pact.consumer.PactMismatchesException: The following requests were not received:
method: GET
path: /devices/v1
query: [externalId:[0942dc67-35de-44f7-a061-743f59436a98]]
headers: [:]
matchers: MatchingRules(rules=[:])
generators: Generators(categories={})
body: OptionalBody(state=MISSING, value=null)
Below is my Java class
public class PactForDevice {
Map<String, String> headers = MapUtils.putAll(new HashMap<String, String>(), new String[]{"Content-Type", "application/json;charset=UTF-8"});
#Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("device-service-m", this);
#Pact(consumer = "device-r", provider = "device-service-m")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
return builder
.given("Device M details")
.uponReceiving("retrieving Device details")
.path("/devices/v1")
.method("GET")
.query("externalId=0942dc67-35de-44f7-a061-743f59436a98")
.willRespondWith()
.headers(headers)
.status(200)
.body("{" +
"\"data\": [,\n " +
"{ \n" +
" \"dateRegistered\": \"2017-07-13T11:10:51.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"a02b14ee72192ab3\",\n" +
" \"description\": \"Samsung SM-G930F\",\n" +
" \"title\": \"a02b14ee72192ab3\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
"},\n" +
"{\n" +
" \"dateRegistered\": \"2017-07-13T10:45:51.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"a41c3af56ec35874\",\n" +
" \"description\": \"Samsung SM-T819\",\n" +
" \"title\": \"a41c3af56ec35874\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
" },\n" +
" {\n" +
" \"dateRegistered\": \"2017-07-13T10:45:31.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"bd2b027bbd0a2f17\",\n" +
" \"description\": \"Samsung SM-A320Y\",\n" +
" \"title\": \"bd2b027bbd0a2f17\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
" }\n" +
"],\n" +
" \"message\": \"3 devices found for the user 0942dc67-35de-44f7-a061-743f59436a98\"\n" +
"}")
.toPact();
}
#PactVerification("device-service-m")
#Test
#JsonIgnoreProperties(ignoreUnknown = true)
public void runTest1() throws IOException {
final GetDevicesResponse deviceResponse = new GetDevicesResponse();
final List<Device> deviceList = new ArrayList<>();
Device dev = new Device();
dev.withDateRegistered("2017-07-13T11:10:51.000+12:00");
dev.withAlias("");
dev.withId("a02b14ee72192ab3");
dev.withDescription("Samsung SM-G930F");
dev.withTitle("a02b14ee72192ab3");
dev.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev);
Device dev1 = new Device();
dev1.withDateRegistered("2017-07-13T10:45:51.000+12:00");
dev1.withAlias("");
dev1.withId("a41c3af56ec35874");
dev1.withDescription("Samsung SM-T819");
dev1.withTitle("a41c3af56ec35874");
dev1.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev1);
Device dev2 = new Device();
dev2.withDateRegistered("2017-07-13T10:45:31.000+12:00");
dev2.withAlias("");
dev2.withId("bd2b027bbd0a2f17");
dev2.withDescription("Samsung SM-A320Y");
dev2.withTitle("bd2b027bbd0a2f17");
dev2.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev2);
deviceResponse.setDevices(deviceList);
final RestTemplate restTemplate = new RestTemplate();
GetDevicesResponse devices = restTemplate.getForObject("http://localhost:8091/devices/v1?externalId=0942dc67-35de-44f7-a061-743f59436a98", GetDevicesResponse.class);
assertThat(devices, sameBeanAs(deviceResponse));
}
}
EDIT:
I just found that if I comment out #Rule part, the test is getting passed - but a pact file is not getting generated. Shouod I explicitly specify a "pact" folder for that?
There are a few problems with your test.
Problem #1
You have not specified a port for the Pact provider rule, so it is starting the mock server on a random port. Your test is accessing your provider on port 8091, so Pact is failing the test and reporting that it did not get the expected request, which it did not (the request went to something else listening on port 8091).
You can fix this by either providing the port 8091 to the rule (you'll need to shutdown whatever is running on 8091), or get your client to use the port of the mock server (from calling getMockServer().getPort()).
Problem #2
Your test is using the Spring Rest Template directly, which means it is not really testing anything other than the Spring HTTP client and the bean de-serialisation. You should be using whatever client code you have (i.e. the class that uses the rest template) and call that in the test.
I had a similar issue with pacts not being generated after the test would run. I never got them to work using the annotation approach, instead I solved it by extending ConsumerPactTestMk2. Pact will setup the mockserver and mock the response for you.
public class PactForDevice extends ConsumerPactTestMk2 {
Map<String, String> headers = MapUtils.putAll(new HashMap<String, String>(), new String[]{"Content-Type", "application/json;charset=UTF-8"});
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.given("Device M details")
.uponReceiving("retrieving Device details")
.path("/devices/v1")
.method("GET")
.query("externalId=0942dc67-35de-44f7-a061-743f59436a98")
.willRespondWith()
.headers(headers)
.status(200)
.body("{" +
"\"data\": [,\n " +
"{ \n" +
" \"dateRegistered\": \"2017-07-13T11:10:51.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"a02b14ee72192ab3\",\n" +
" \"description\": \"Samsung SM-G930F\",\n" +
" \"title\": \"a02b14ee72192ab3\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
"},\n" +
"{\n" +
" \"dateRegistered\": \"2017-07-13T10:45:51.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"a41c3af56ec35874\",\n" +
" \"description\": \"Samsung SM-T819\",\n" +
" \"title\": \"a41c3af56ec35874\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
" },\n" +
" {\n" +
" \"dateRegistered\": \"2017-07-13T10:45:31.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"bd2b027bbd0a2f17\",\n" +
" \"description\": \"Samsung SM-A320Y\",\n" +
" \"title\": \"bd2b027bbd0a2f17\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
" }\n" +
"],\n" +
" \"message\": \"3 devices found for the user 0942dc67-35de-44f7-a061-743f59436a98\"\n" +
"}")
.toPact();
}
#Override
protected String providerName() {
return "device-service-m";
}
#Override
protected String consumerName() {
return "device-r";
}
#Override
protected void runTest(MockServer mockServer) throws IOException {
final GetDevicesResponse deviceResponse = new GetDevicesResponse();
final List<Device> deviceList = new ArrayList<>();
Device dev = new Device();
dev.withDateRegistered("2017-07-13T11:10:51.000+12:00");
dev.withAlias("");
dev.withId("a02b14ee72192ab3");
dev.withDescription("Samsung SM-G930F");
dev.withTitle("a02b14ee72192ab3");
dev.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev);
Device dev1 = new Device();
dev1.withDateRegistered("2017-07-13T10:45:51.000+12:00");
dev1.withAlias("");
dev1.withId("a41c3af56ec35874");
dev1.withDescription("Samsung SM-T819");
dev1.withTitle("a41c3af56ec35874");
dev1.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev1);
Device dev2 = new Device();
dev2.withDateRegistered("2017-07-13T10:45:31.000+12:00");
dev2.withAlias("");
dev2.withId("bd2b027bbd0a2f17");
dev2.withDescription("Samsung SM-A320Y");
dev2.withTitle("bd2b027bbd0a2f17");
dev2.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev2);
deviceResponse.setDevices(deviceList);
String url = mockServer.getUrl();
String path = "devices/v1";
String query = "externalId=0942dc67-35de-44f7-a061-743f59436a98";
URIBuilder uriBuilder = null;
try {
uriBuilder = new URIBuilder(url)
.setPath(path)
.setQuery(query);
} catch (URISyntaxException e) {
e.printStackTrace();
}
GetDevicesResponse devices = new ObjectMapper().readValue(Request.Get(uriBuilder.toString())
.addHeader("content-type", "application/json")
.execute().returnContent().asString(), GetDevicesResponse.class);
assertThat(devices, sameBeanAs(deviceResponse));
}
}
With this approach I had to add google guava 19 to my pom. But it works good.
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
Related
The following is a method for sending an Email from a Razor page in ASP.NET Core. I need to use MailKit since System.Net.Mail is not available in ASP.NET Core.
Despite much research, I haven't been able to figure out a way to include the image to the Email. Note that it doesn't have to be an attachment - embedding the image will work.
public ActionResult Contribute([Bind("SubmitterScope, SubmitterLocation, SubmitterItem, SubmitterCategory, SubmitterEmail, SubmitterAcceptsTerms, SubmitterPicture")]
EmailFormModel model)
{
if (ModelState.IsValid)
{
try
{
var emailName= _appSettings.EmailName;
var emailAddress = _appSettings.EmailAddress;
var emailPassword = _appSettings.EmailPassword;
var message = new MimeMessage();
message.From.Add(new MailboxAddress(emailName, emailAddress));
message.To.Add(new MailboxAddress(emailName, emailAddress));
message.Subject = "Record Submission From: " + model.SubmitterEmail.ToString();
message.Body = new TextPart("plain")
{
Text = "Scope: " + model.SubmitterScope.ToString() + "\n" +
"Zip Code: " + model.SubmitterLocation.ToString() + "\n" +
"Item Description: " + model.SubmitterItem.ToString() + "\n" +
"Category: " + model.SubmitterCategory + "\n" +
"Submitted By: " + model.SubmitterEmail + "\n" +
// This is the file that should be attached.
//"Picture: " + model.SubmitterPicture + "\n" +
"Terms Accepted: " + model.SubmitterAcceptsTerms + "\n"
};
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
client.Authenticate(emailAddress, emailPassword);
client.Send(message);
client.Disconnect(true);
return RedirectToAction("Success");
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ": " + ex.StackTrace);
return RedirectToAction("Failure");
}
}
else
{
return View();
}
}
This is from the FAQ on Mailkit github repo, and seems to cover the full process.
https://github.com/jstedfast/MailKit/blob/master/FAQ.md#CreateAttachments
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey#friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice#wonderland.com"));
message.Subject = "How you doin?";
// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
Text = #"Hey Alice,
What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.
Will you be my +1?
-- Joey
"
};
// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
ContentObject = new ContentObject (File.OpenRead (path), ContentEncoding.Default),
ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName (path)
};
// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);
// now set the multipart/mixed as the message body
message.Body = multipart;
I am trying to write test cases for our AJAX calls to our API. Doing a simply web request and response. My question is with regard to the response. Is there a simpler way to pull out the response JSON values? Is the best way to do this sort of thing? I know we could us JQuery, but wanted to use Microsoft Testing framework.
[TestMethod]
public void TestMethod1()
{
string brand = "KEWL";
string BRAND = "";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://203.135.xx.138:4040/api/v1/subscriptions/signup.format");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = #"{" +
" 'api_key': '91230D10-247C-11E1-83FF-9B9C4824019B'," +
" 'phone': '12122639043', " +
" 'dob': '11231954', " +
" 'subscriptions': [ " +
" {" +
" 'Brand':'" + brand + "', " +
" 'campaign':'BTLNDN', " +
" 'groups':[" +
" {" +
" 'group': 'BTLALL'," +
" 'subscribed':true" +
" } " +
" ]," +
" 'lang': 'en' " +
" }" +
" ] " +
" }";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, dynamic> dc = serializer.Deserialize<Dictionary<string, dynamic>>(responseText);
var kev = dc;
foreach (var key1 in dc.Keys)
{
var value3 = dc["ReturnData"]["subscriptions"];
BRAND = value3[0]["brand"];
// var groups = value3[0]["groups"];
}
}
Assert.AreEqual(brand, BRAND);
}
The idea of unit testing ASP.NET MVC methods is that you can run the test without using any Http request or respond functionality.
Suppose you have the following method:
public class MyController : Controller
{
public ActionResult MyAjax()
{
return Json(new { Test = "Test" });
}
}
You can test it with this code:
[TestMethod]
public void MyTest()
{
MyControllercontroller = new MyController();
JsonResult json = controller.MyAjax() as JsonResult;
Assert.IsNotNull(json);
dynamic data = json.Data;
Assert.AreEqual("Test", data.Test);
}
To use the dynamic keyword you have to make sure that your test project can see the internals of your web project (this is because anonymous types are declared internal). You can do this by adding: [assembly: InternalsVisibleTo("YourTestProject")] to the AssemblyInfo.cs file of your web project.
I am working on a Facebook application and I can successfully get the user's info in JSON after authorizing. However, when I refresh the page, I'm redirected to Facebook for OAuth when I thought that the authorization had already taken place. I had understood that I only needed an access token, subsequently.
if (fbData != null)
{
if (fbData.user_id == null)
{
var redirURL = Request.Url.Scheme + "://" + fbAuthURL.Replace("[APPID]", appId).Replace("[REDIRECT]", (Request.Url.Scheme + "://" + canvasPage));
Response.Write("<script type=\"text/javascript\"> " + Environment.NewLine +
"top.location.href=\"" + redirURL + "\";" + Environment.NewLine +
"</script>");
Response.Write("Redirect URL for Authorization : " + redirURL);
}
else
{
if (isReady == true)
{
MyWebRequest userRequest = new MyWebRequest("https://graph.facebook.com/me?access_token=" + accessToken);
string userInfo = userRequest.GetResponse();
//Response.Write(userInfo);
FBAuth fuser = new FBAuth();
UserInfo oUser = fuser.GetUserInfo(userInfo);
}}}
How can I handle it?
I'm executing the following command:
var chatRequest:URLRequest = new URLRequest("http://test.com/videophonetest/scripts/get_put_peerID.php?peerID=" + myID);
chatLoader = new URLLoader(chatRequest);
configureListenersChatLoader(chatLoader);
chatLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
chatLoader.load(chatRequest);
configureListenersChatLoader(chatLoader) and friends looks like:
private function configureListenersDeleteLoader(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandlerDeleteLoader);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandlerChatLoader(event:Event):void {
writeText("Completed Connection!");
var urlVariables:URLVariables = new URLVariables(chatLoader.data);
writeText("Your ID: " + myID);
theirID = urlVariables.peerID;
writeText("Their ID: " + theirID);
}
private function completeHandlerDeleteLoader(event:Event):void {
writeText("Deletion Complete!");
var urlVariables:URLVariables = new URLVariables(deleteLoader.data);
writeText("Deleted: " + urlVariables.deleted);
writeText("Getting New Chat...");
initNewChatSession();
}
private function openHandler(event:Event):void {
writeText("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
writeText("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
writeText("securityErrorHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
writeText("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
writeText("ioErrorHandler: " + event);
}
The command works on my computer but when I try it on my roommates computer it returns an error:
httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=0 responseURL=null]
securityErrorHandler: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048"]
Any reason why this might be happening? It works on some computers but not others. Firewalls?
You will need to allow the location of the file on your friends' computer in this settings manager:
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html
All,
While this is similar to another post, that post (does not indicate how to perform this (if it can be done) without instantiating an object. Also, without success I have tried multiple variations on the theme of:
class[method](arg)
class[method].call(arg)
method.apply(class, arg)
I am new to Flex, but have used Reflection in both C# and Java. BTW, the code that I am attempting to get to work in Flex is mirrored in both those languages and works as expected.
Any help is appreciated,
Thanks,
Todd
Non-functioning Flex Code:
private function ipMethodTester( ipMethodName:String,
shouldPass:Array, shouldFail:Array):void
{
var message:String;
var entry:String;
for each(entry in shouldPass)
{
message = ipMethodName + ": " + entry + " should pass";
try
{
Assert.assertTrue(message,
FieldValidator[ipMethodName](entry));
}
catch(e:Error)
{
Assert.fail(e.message + " " + message);
}
}
for each(entry in shouldFail)
{
message = ipMethodName + ": " + entry + " should fail";
try
{
Assert.assertFalse(message,
FieldValidator[ipMethodName](entry));
}
catch(e:Error)
{
Assert.fail(e.message + " " + message);
}
}
}
Java Code:
private void ipMethodTester(final String ipMethodName,
final String[] shouldPass, final String[] shouldFail)
{
Method method;
try
{
method = InetUtil.class.getDeclaredMethod(ipMethodName, String.class);
method.setAccessible(true);
for(String entry : shouldPass)
{
Object[] invokeArgs = { entry };
boolean passed = (Boolean)method.invoke(null, invokeArgs);
assertTrue(ipMethodName + ": " + entry + " should pass", passed);
}
for(String entry : shouldFail)
{
Object[] invokeArgs = { entry };
boolean passed = (Boolean)method.invoke(null, invokeArgs);
assertFalse(ipMethodName + ": " + entry + " should fail", passed);
}
}
catch (final Exception e)
{
fail(e.getClass().toString());
}
}
C# code:
private void ipMethodTester(string ipMethodName, string[] shouldPass, string[] shouldFail)
{
Type type = typeof (ValidateUtil);
BindingFlags bindingFlags = BindingFlags.InvokeMethod
| BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
MethodInfo method = type.GetMethod(ipMethodName, bindingFlags);
foreach(string entry in shouldPass)
{
object[] invokeArgs = { entry };
bool passed = (bool)method.Invoke(null, invokeArgs);
Assert.IsTrue(passed, ipMethodName + ": " + entry + " should pass");
}
foreach(string entry in shouldFail)
{
object[] invokeArgs = { entry };
bool passed = (bool)method.Invoke(null, invokeArgs);
Assert.IsFalse(passed, ipMethodName + ": " + entry + " should fail");
}
}
This works for me:
MyClass['myMethod']('arg1','arg2');
This also works:
MyClass['myMethod'].call(MyClass, 'arg1', 'arg2');
Note: the first argument of the 'call' method (MyClass in this case) just specifies which object is referenced when you use the 'this' keyword inside the function.