summaryrefslogtreecommitdiffstats
path: root/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
blob: 34efb04812f41d50fe7a6cf186f907a64a68b8a3 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.binding.erlang.testing;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.apache.tuscany.sca.binding.erlang.impl.TypeMismatchException;
import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException;
import org.apache.tuscany.sca.binding.erlang.testing.dynaignore.IgnorableRunner;
import org.apache.tuscany.sca.binding.erlang.testing.dynaignore.IgnoreTest;
import org.apache.tuscany.sca.host.embedded.SCADomain;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osoa.sca.ServiceRuntimeException;

import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangDouble;
import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangLong;
import com.ericsson.otp.erlang.OtpErlangString;
import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpMbox;
import com.ericsson.otp.erlang.OtpNode;

//this test runner will ignore tests if epmd is not available
@RunWith(IgnorableRunner.class)
public class ReferenceServiceTestCase {

	private static final String EPMD_COMMAND = "epmd";

	private static MboxInterface mboxReference;
	private static ServiceInterface moduleReference;
	private static ServiceInterface clonedModuleReference;
	private static OtpNode node;
	private static OtpMbox mbox;
	private static Process epmdProcess;

	@BeforeClass
	public static void init() throws IOException {
		try {
			epmdProcess = Runtime.getRuntime().exec(EPMD_COMMAND);
			SCADomain domain = SCADomain
					.newInstance("ErlangReference.composite");
			SCADomain.newInstance("ErlangService.composite");
			ReferenceTestComponentImpl component = domain.getService(
					ReferenceTestComponentImpl.class, "ReferenceTest");
			mboxReference = component.getMboxReference();
			moduleReference = component.getModuleReference();
			clonedModuleReference = component.getClonedModuleReference();
			node = new OtpNode("MboxServer");
			mbox = node.createMbox("sendArgs");
		} catch (IOException e) {
			System.out.println("Problem executing " + EPMD_COMMAND + ": "
					+ e.getLocalizedMessage() + ". Tests will be IGNORED.");
		}
	}

	@AfterClass
	public static void clean() {
		if (epmdProcess != null) {
			epmdProcess.destroy();
		}
	}

	@Before
	public void before() {
		if (epmdProcess == null) {
			throw new IgnoreTest();
		}
	}

	/**
	 * Tests passing strings
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testStrings() throws Exception {
		String strArg = "Test message";
		String strResult = "OK";
		MboxListener mboxListener = new MboxListener(mbox, strResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		String testResult = mboxReference.sendArgs(strArg);
		assertEquals(strArg, ((OtpErlangString) mboxListener.getMsg().getMsg())
				.stringValue());
		assertEquals(strResult, testResult);
	}

	/**
	 * Tests passing booleans
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testBooleans() throws Exception {
		boolean booleanArg = true;
		boolean booleanResult = false;
		MboxListener mboxListener = new MboxListener(mbox, booleanResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		boolean testResult = mboxReference.sendArgs(booleanArg);
		assertEquals(booleanArg, ((OtpErlangAtom) mboxListener.getMsg()
				.getMsg()).booleanValue());
		assertEquals(booleanResult, testResult);
	}

	/**
	 * Tests passing floats
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testFloats() throws Exception {
		float floatArg = 1.0f;
		float floatResult = 2.0f;
		MboxListener mboxListener = new MboxListener(mbox, floatResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		float testResult = mboxReference.sendArgs(floatArg);
		assertEquals(floatArg, ((OtpErlangDouble) mboxListener.getMsg()
				.getMsg()).doubleValue(), 0);
		assertEquals(floatResult, testResult, 0);
	}

	/**
	 * Tests passing doubles
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testDoubles() throws Exception {
		double doubleArg = 1.0f;
		double doubleResult = 2.0f;
		MboxListener mboxListener = new MboxListener(mbox, doubleResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		double testResult = mboxReference.sendArgs(doubleArg);
		assertEquals(doubleArg, ((OtpErlangDouble) mboxListener.getMsg()
				.getMsg()).doubleValue(), 0);
		assertEquals(doubleResult, testResult, 0);
	}

	/**
	 * Tests passing long values
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testLongs() throws Exception {
		long longArg = 1;
		long longResult = 2;
		MboxListener mboxListener = new MboxListener(mbox, longResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		long testResult = mboxReference.sendArgs(longArg);
		assertEquals(longArg, ((OtpErlangLong) mboxListener.getMsg().getMsg())
				.longValue(), 0);
		assertEquals(longResult, testResult, 0);
	}

	/**
	 * Tests passing integers
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testInts() throws Exception {
		int intArg = 1;
		int intResult = 2;
		MboxListener mboxListener = new MboxListener(mbox, intResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		int testResult = mboxReference.sendArgs(intArg);
		assertEquals(intArg, ((OtpErlangLong) mboxListener.getMsg().getMsg())
				.intValue(), 0);
		assertEquals(intResult, testResult, 0);
	}

	/**
	 * Tests passing chars
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testChars() throws Exception {
		char charArg = 1;
		char charResult = 2;
		MboxListener mboxListener = new MboxListener(mbox, charResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		char testResult = mboxReference.sendArgs(charArg);
		assertEquals(charArg, ((OtpErlangLong) mboxListener.getMsg().getMsg())
				.charValue(), 0);
		assertEquals(charResult, testResult, 0);
	}

	/**
	 * Tests passing shorts
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testShorts() throws Exception {
		short shortArg = 1;
		short shortResult = 2;
		MboxListener mboxListener = new MboxListener(mbox, shortResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		short testResult = mboxReference.sendArgs(shortArg);
		assertEquals(shortArg, ((OtpErlangLong) mboxListener.getMsg().getMsg())
				.shortValue(), 0);
		assertEquals(shortResult, testResult, 0);
	}

	/**
	 * Tests passing bytes
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testBytes() throws Exception {
		byte byteArg = 1;
		byte byteResult = 2;
		MboxListener mboxListener = new MboxListener(mbox, byteResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		byte testResult = mboxReference.sendArgs(byteArg);
		assertEquals(byteArg, ((OtpErlangLong) mboxListener.getMsg().getMsg())
				.byteValue(), 0);
		assertEquals(byteResult, testResult, 0);
	}

	/**
	 * Tests passing multiple arguments
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testMultipleArguments() throws Exception {
		MboxListener mboxListener = new MboxListener(mbox, true);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		String testString = "TupleString";
		int testInt = 10;
		mboxReference.sendArgs(testInt, testString);
		assertEquals(testInt, ((OtpErlangLong) ((OtpErlangTuple) mboxListener
				.getMsg().getMsg()).elementAt(0)).longValue());
		assertEquals(testString,
				((OtpErlangString) ((OtpErlangTuple) mboxListener.getMsg()
						.getMsg()).elementAt(1)).stringValue());
	}

	/**
	 * Tests passing tuples
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testTuples() throws Exception {
		StructuredTuple tupleResult = new StructuredTuple();
		tupleResult.arg1.arg1 = 1;
		tupleResult.arg1.arg2 = "Tuple inside tuple";
		tupleResult.arg2 = "Tuple!";
		tupleResult.arg3 = true;
		MboxListener mboxListener = new MboxListener(mbox, tupleResult);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		StructuredTuple testArg = new StructuredTuple();
		testArg.arg2 = "Arg2a";
		testArg.arg3 = true;
		testArg.arg1.arg1 = 10;
		testArg.arg1.arg2 = "Arg2b";
		StructuredTuple testResult = mboxReference.sendArgs(testArg);
		assertEquals(tupleResult, testResult);
		OtpErlangTuple received = (OtpErlangTuple) mboxListener.getMsg()
				.getMsg();
		assertEquals(testArg.arg1.arg1,
				((OtpErlangLong) ((OtpErlangTuple) received.elementAt(0))
						.elementAt(0)).longValue());
		assertEquals(testArg.arg1.arg2,
				((OtpErlangString) ((OtpErlangTuple) received.elementAt(0))
						.elementAt(1)).stringValue());
		assertEquals(testArg.arg2, ((OtpErlangString) received.elementAt(1))
				.stringValue());
		assertEquals(testArg.arg3, ((OtpErlangAtom) received.elementAt(2))
				.booleanValue());
	}

	/**
	 * Tests passing lists
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testLists() throws Exception {
		String[] testArg = new String[] { "One", "Two", "Three" };
		MboxListener mboxListener = new MboxListener(mbox, testArg);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		String[] testResult = mboxReference.sendArgs(testArg);
		assertEquals(testArg.length, testResult.length);
		for (int i = 0; i < testArg.length; i++) {
			assertEquals(testArg[i], testResult[i]);
		}
		OtpErlangList received = (OtpErlangList) mboxListener.getMsg().getMsg();
		assertEquals(testArg.length, received.arity());
		for (int i = 0; i < testArg.length; i++) {
			assertEquals(testArg[i], ((OtpErlangString) received.elementAt(i))
					.stringValue());
		}
	}

	/**
	 * Tests passing multidimensional lists
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testMultiDimLists() throws Exception {
		String[][] testArg = new String[][] { { "One", "Two" },
				{ "Three", "Four", "Five" }, { "Six" } };
		MboxListener mboxListener = new MboxListener(mbox, testArg);
		Thread mboxThread = new Thread(mboxListener);
		mboxThread.start();
		String[][] testResult = mboxReference.sendArgs(testArg);
		assertEquals(testArg.length, testResult.length);
		for (int i = 0; i < testArg.length; i++) {
			for (int j = 0; j < testArg[i].length; j++) {
				assertEquals(testArg[i][j], testResult[i][j]);
			}
		}
		OtpErlangList received = (OtpErlangList) mboxListener.getMsg().getMsg();
		assertEquals(testArg.length, received.arity());
		for (int i = 0; i < testArg.length; i++) {
			for (int j = 0; j < testArg[i].length; j++) {
				assertEquals(testArg[i][j],
						(((OtpErlangString) ((OtpErlangList) received
								.elementAt(i)).elementAt(j)).stringValue()));
			}
		}
	}

	/**
	 * Tests mismatched interface
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void typeMismatch() throws Exception {
		try {
			StructuredTuple arg = new StructuredTuple();
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			String[] arg = new String[] { "test" };
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			long arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			int arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			short arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			char arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			byte arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			double arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			float arg = 1;
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			String arg = "1";
			MboxListener mboxListener = new MboxListener(mbox, true);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}

		try {
			boolean arg = true;
			MboxListener mboxListener = new MboxListener(mbox, 1);
			Thread mboxThread = new Thread(mboxListener);
			mboxThread.start();
			mboxReference.sendArgs(arg);
		} catch (Exception e) {
			assertEquals(TypeMismatchException.class, e.getClass());
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Basic RPC test, without arguments
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testRPC() throws Exception {
		String[] result = moduleReference.sayHellos();
		assertEquals(2, result.length);
		assertEquals("1", result[0]);
		assertEquals("2", result[1]);
	}

	/**
	 * Tests RPC with arguments
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testRPCWithArgs() throws Exception {
		String arg1 = "One";
		String arg2 = "Two";
		String testResult = moduleReference.sayHello(arg1, arg2);
		assertEquals("Hello " + arg1 + " " + arg2, testResult);
	}

	/**
	 * Tests RPC with structured arguments
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testRPCWithComplexArgs() throws Exception {
		StructuredTuple arg = new StructuredTuple();
		arg.arg1.arg2 = "Not empty";
		arg.arg2 = "Not empty";
		StructuredTuple testResult = moduleReference.passComplexArgs(arg,
				new String[] { "some", "array" });
		assertEquals(arg, testResult);
	}

	/**
	 * Tests handling requests pointing to unknown functions
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testUnknownFunction() throws Exception {

		// following functions differs by parameters

		try {
			moduleReference.sayHello();
		} catch (Exception e) {
			assertEquals(ErlangException.class, e.getClass());
		}

		try {
			moduleReference.sayHello("1");
		} catch (Exception e) {
			assertEquals(ErlangException.class, e.getClass());
		}

		try {
			moduleReference.sayHello(1, 2);
		} catch (Exception e) {
			assertEquals(ErlangException.class, e.getClass());
		}

		// for following ones name not exists

		moduleReference.notExist();

		try {
			moduleReference.notExistWithException();
		} catch (Exception e) {
			assertEquals(ErlangException.class, e.getClass());
		}
	}

	/**
	 * Tests using multiple Erlang modules on one SCA Erlang node
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testMultipleModulesOnNode() throws Exception {
		String[] mr = moduleReference.sayHellos();
		String[] cmr = clonedModuleReference.sayHellos();
		assertEquals("1", mr[0]);
		assertEquals("2", mr[1]);

		assertEquals("-1", cmr[0]);
		assertEquals("-2", cmr[1]);
	}

	/**
	 * Tests nodes with duplcated components (the same node and module
	 * parameters)
	 * 
	 * @throws Exception
	 */
	@Test(timeout = 1000)
	public void testModuleDuplicatedOnNode() throws Exception {
		try {
			SCADomain.newInstance("ErlangServiceModuleDuplicate.composite");
		} catch (ServiceRuntimeException e) {
			assertEquals(ErlangException.class, e.getCause().getClass());
		}
	}

}