1 package de.uni_leipzig.wifa.iwi.mr3.service.impl;
2
3 import static org.easymock.EasyMock.createMock;
4 import static org.easymock.EasyMock.eq;
5 import static org.easymock.EasyMock.expect;
6 import static org.easymock.EasyMock.expectLastCall;
7 import static org.easymock.EasyMock.isA;
8 import static org.easymock.EasyMock.replay;
9 import static org.easymock.EasyMock.reset;
10 import static org.easymock.EasyMock.same;
11 import static org.easymock.EasyMock.verify;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import java.io.FileNotFoundException;
19 import java.util.Map;
20
21 import javax.xml.stream.XMLStreamException;
22
23 import org.apache.axiom.om.OMElement;
24 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
25 import org.apache.axis2.AxisFault;
26 import org.apache.axis2.context.ConfigurationContext;
27 import org.apache.axis2.context.ServiceContext;
28 import org.apache.axis2.context.ServiceGroupContext;
29 import org.apache.axis2.description.AxisService;
30 import org.apache.axis2.description.AxisServiceGroup;
31 import org.apache.axis2.description.Parameter;
32 import org.apache.axis2.engine.AxisConfiguration;
33 import org.eclipse.emf.ecore.EClass;
34 import org.eclipse.emf.ecore.EObject;
35 import org.eclipse.emf.ecore.EPackage;
36 import org.eclipse.emf.ecore.resource.Resource;
37 import org.eclipse.emf.ecore.resource.ResourceSet;
38 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
39 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
40 import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
41 import org.junit.Before;
42 import org.junit.Test;
43
44 import de.uni_leipzig.wifa.iwi.mr3.TestHelper;
45 import de.uni_leipzig.wifa.iwi.mr3.common.Comparison;
46 import de.uni_leipzig.wifa.iwi.mr3.common.Match;
47 import de.uni_leipzig.wifa.iwi.mr3.dao.ModelRepositoryDao;
48 import de.uni_leipzig.wifa.iwi.mr3.service.MRException;
49 import de.uni_leipzig.wifa.iwi.mr3.service.ServiceHelper;
50
51
52
53
54 public class ModelRepositoryServiceImplTest
55 {
56 private static final int ARRAY_SIZE = 3;
57
58 private AxisConfiguration axisConfiguration;
59 private AxisService axisService;
60 private AxisServiceGroup axisServiceGroup;
61 private Parameter compParam;
62 private CompareProcessorImpl compProc;
63 private ConfigurationContext configctx;
64 private ServiceContext context;
65 private ModelRepositoryDao daoMock;
66 private Parameter daoParam;
67 private Parameter dbParam;
68 private ServiceGroupContext serviceGroupContext;
69 private ServiceHelper serviceHelper;
70 private Parameter serviceParam;
71
72
73 private ModelRepositoryServiceImpl to;
74
75
76
77
78 @Before
79 public void setUp()
80 {
81 try
82 {
83 daoMock = createMock(ModelRepositoryDao.class);
84 replay(daoMock);
85
86 serviceHelper = new ServiceHelperImpl();
87
88 compProc = new CompareProcessorImpl();
89 compProc.setDao(daoMock);
90 compProc.setHelper(serviceHelper);
91
92 daoParam = new Parameter("dao", daoMock);
93 dbParam = new Parameter("ModelRepository.DatabasePath", "aFilePath");
94 serviceParam = new Parameter("service.helper", serviceHelper);
95 compParam = new Parameter("compare.processor", compProc);
96
97 axisService = new AxisService("TestService");
98 axisService.addParameter(daoParam);
99 axisService.addParameter(dbParam);
100 axisService.addParameter(compParam);
101 axisService.addParameter(serviceParam);
102
103 axisConfiguration = new AxisConfiguration();
104 axisConfiguration.addService(axisService);
105
106 axisServiceGroup = new AxisServiceGroup();
107 axisServiceGroup.addService(axisService);
108
109 configctx = new ConfigurationContext(axisConfiguration);
110
111 serviceGroupContext = new ServiceGroupContext(configctx, axisServiceGroup);
112 context = serviceGroupContext.getServiceContext(axisService);
113 to = new ModelRepositoryServiceImpl();
114 to.init(context);
115 }
116 catch (final AxisFault e)
117 {
118 fail("An error occured during setup of Axis2 ServiceContext.");
119 }
120 }
121
122
123
124
125
126
127 @Test
128 public void testComparison1()
129 {
130 final String leftUri = "d:/workspace/XMIParser/src/mr3/repository/backend/parser/staff2.ecore";
131 final String rightUri = "d:/workspace/XMIParser/src/mr3/repository/backend/parser/staff.ecore";
132
133 Comparison compare = null;
134
135 final String filepathleft = "src/test/resources/models/staff2.ecore";
136 final String filepathright = "src/test/resources/models/staff.ecore";
137
138 final EObject eObjLeft = TestHelper.parseEObjectFromFile(filepathleft);
139 final EObject eObjright = TestHelper.parseEObjectFromFile(filepathright);
140
141 reset(daoMock);
142 expect(daoMock.load(eq(leftUri), isA(Map.class))).andReturn(eObjLeft);
143 expect(daoMock.load(eq(rightUri), isA(Map.class))).andReturn(eObjright);
144 replay(daoMock);
145
146
147 try
148 {
149
150 compare = to.compare(leftUri, rightUri);
151 }
152 catch (final MRException e)
153 {
154 fail("Test should run normally.");
155 }
156
157
158 verify(daoMock);
159 }
160
161
162
163
164
165
166 @Test
167 public void testComparison2()
168 {
169 final String leftUri = "d:/workspace/XMIParser/src/mr3/repository/backend/parser/staff.ecore";
170
171 Comparison compare = null;
172
173 final String filepath = "src/test/resources/models/staff.ecore";
174
175 final EObject eObjLeft = TestHelper.parseEObjectFromFile(filepath);
176
177 reset(daoMock);
178 expect(daoMock.load(same(leftUri), isA(Map.class))).andReturn(eObjLeft).times(2);
179 replay(daoMock);
180
181
182 try
183 {
184
185 compare = to.compare(leftUri, leftUri);
186 }
187 catch (final MRException e)
188 {
189 fail("Test should run normally.");
190 }
191
192
193 verify(daoMock);
194
195 }
196
197
198
199
200
201
202 @Test
203 public void testComparison3()
204 {
205 final String filepath = "src/test/resources/models/leipzig_instance_of_flughafen.xml";
206 final String metaFilePath = "src/test/resources/models/flughafen.ecore";
207 final String modelFilePathleft = "src/test/resources/models/leipzig_instance_of_flughafen.xmi";
208 final String metaNsUri = "platform:/resource/Test/flughafen.ecore";
209 final String modelFilePathright = "src/test/resources/models/staff.ecore";
210
211 final String leftUri = metaNsUri + " [" + filepath + "]";
212 final String rightUri = "d:/workspace/XMIParser/src/mr3/repository/backend/parser/staff.ecore";
213
214 Comparison compare = null;
215
216
217
218 final ResourceSet resourceSet = new ResourceSetImpl();
219 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
220 final org.eclipse.emf.common.util.URI fileURIMeta = org.eclipse.emf.common.util.URI.createFileURI(metaFilePath);
221 final Resource resourceMeta = resourceSet.getResource(fileURIMeta, true);
222 final EObject eMetaLeft = resourceMeta.getContents().get(0);
223
224 resourceSet.getPackageRegistry().put(((EPackage) eMetaLeft).getNsURI(), eMetaLeft);
225
226 final org.eclipse.emf.common.util.URI fileURILeft = org.eclipse.emf.common.util.URI.createFileURI(modelFilePathleft);
227 final Resource resourceLeft = resourceSet.getResource(fileURILeft, true);
228 final EObject eObjLeft = resourceLeft.getContents().get(0);
229
230 final org.eclipse.emf.common.util.URI fileURIRight = org.eclipse.emf.common.util.URI.createFileURI(modelFilePathright);
231 final Resource resourceRight = resourceSet.getResource(fileURIRight, true);
232 final EObject eObjRight = resourceRight.getContents().get(0);
233
234 reset(daoMock);
235 expect(daoMock.load(eq(leftUri), isA(Map.class))).andReturn(eObjLeft);
236 expect(daoMock.load(eq(rightUri), isA(Map.class))).andReturn(eObjRight);
237 replay(daoMock);
238
239
240 try
241 {
242 compare = to.compare(leftUri, rightUri);
243 fail("Test fail because of different metamodels.");
244 }
245 catch (final MRException e)
246 {
247 }
248
249
250 verify(daoMock);
251
252 }
253
254
255
256
257
258
259
260 @Test
261 public void testComparison4()
262 {
263 final String metaFilePath = "src/test/resources/models/flughafen.ecore";
264 final String metaNsUri = "platform:/resource/Test/flughafen.ecore";
265
266 final String filepathleft = "src/test/resources/models/leipzig_instance_of_flughafen.xml";
267 final String modelFilePathleft = "src/test/resources/models/leipzig_instance_of_flughafen.xmi";
268 final String leftUri = metaNsUri + " [" + filepathleft + "]";
269
270 final String filepathright = "src/test/resources/models/paris_instance_of_flughafen.xml";
271 final String modelFilePathright = "src/test/resources/models/paris_instance_of_flughafen.xmi";
272 final String rightUri = metaNsUri + " [" + filepathright + "]";
273
274 Comparison compare = null;
275
276
277
278 final ResourceSet resourceSet = new ResourceSetImpl();
279 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
280 final org.eclipse.emf.common.util.URI fileURIMeta = org.eclipse.emf.common.util.URI.createFileURI(metaFilePath);
281 final Resource resourceMeta = resourceSet.getResource(fileURIMeta, true);
282 final EObject eMeta = resourceMeta.getContents().get(0);
283
284 resourceSet.getPackageRegistry().put(((EPackage) eMeta).getNsURI(), eMeta);
285
286 final org.eclipse.emf.common.util.URI fileURILeft = org.eclipse.emf.common.util.URI.createFileURI(modelFilePathleft);
287 final Resource resourceLeft = resourceSet.getResource(fileURILeft, true);
288 final EObject eObjLeft = resourceLeft.getContents().get(0);
289
290 final org.eclipse.emf.common.util.URI fileURIRight = org.eclipse.emf.common.util.URI.createFileURI(modelFilePathright);
291 final Resource resourceRight = resourceSet.getResource(fileURIRight, true);
292 final EObject eObjRight = resourceRight.getContents().get(0);
293
294 reset(daoMock);
295 expect(daoMock.load(eq(leftUri), isA(Map.class))).andReturn(eObjLeft);
296 expect(daoMock.load(eq(rightUri), isA(Map.class))).andReturn(eObjRight);
297 expect(daoMock.load(eq(metaNsUri), isA(Map.class))).andReturn(eMeta);
298 replay(daoMock);
299
300
301 try
302 {
303 compare = to.compare(leftUri, rightUri);
304 }
305 catch (final MRException e)
306 {
307 fail("Test should pass because of same metamodels.");
308 }
309
310
311 verify(daoMock);
312
313 }
314
315
316
317
318
319
320 @Test
321 public void testDelete()
322 {
323 final boolean cascade = true;
324 final String nsUri = "http://testmodel.org/modelOne";
325
326 try
327 {
328 reset(daoMock);
329 daoMock.delete(nsUri, cascade);
330 replay(daoMock);
331 to.delete(nsUri, cascade);
332 }
333 catch (final MRException e)
334 {
335 fail("No Exception should occure in this test");
336 }
337
338
339 verify(daoMock);
340 }
341
342
343
344
345
346
347 @Test
348 public void testDeleteErr()
349 {
350 final boolean cascade = true;
351 final String nsUri = "http://testmodel.org/modelOne";
352
353 try
354 {
355 reset(daoMock);
356 daoMock.delete(nsUri, true);
357 expectLastCall().andThrow(new MRException());
358 replay(daoMock);
359 to.delete(nsUri, cascade);
360 fail("An MRRuntimeException should have been thrown");
361 }
362 catch (final MRException e)
363 {
364 }
365
366
367 verify(daoMock);
368 }
369
370
371
372
373
374
375 @Test
376 public void testDestroy1()
377 {
378
379
380 to.destroy(context);
381 }
382
383
384
385
386
387
388 @Test
389 public void testFind1()
390 {
391 final Match[] result = null;
392
393 final String[] classifier = new String[2];
394 classifier[0] = EClass.class.getSimpleName();
395 classifier[1] = EPackage.class.getSimpleName();
396
397 final String expression = "modelName";
398 final boolean isRegEx = false;
399 final boolean isCaseSensitiv = true;
400
401 reset(daoMock);
402 expect(daoMock.find(expression, classifier, isRegEx, isCaseSensitiv)).andReturn(result);
403 replay(daoMock);
404
405
406 assertArrayEquals(result, to.find(classifier, expression, isRegEx, isCaseSensitiv));
407
408
409 verify(daoMock);
410 }
411
412
413
414
415
416
417 @Test
418 public void testGetInstanceModels()
419 {
420 final String metaModel = "http://metamodels.com/meta";
421
422 final String[] modelList = new String[ARRAY_SIZE];
423 modelList[0] = "http://testmodel.org/modelOne";
424 modelList[1] = "http://testmodel.org/secondOne";
425 modelList[2] = "http://another.org/thirdone";
426
427 reset(daoMock);
428 expect(daoMock.getInstanceModels(metaModel)).andReturn(modelList);
429 replay(daoMock);
430
431 assertArrayEquals(modelList, to.getInstanceModels(metaModel));
432
433
434 verify(daoMock);
435 }
436
437
438
439
440
441
442 @Test
443 public void testLoad()
444 {
445 final String modelNsUri = "http://www.elinson.de/library/20090120";
446
447 final String filepath = "src/test/resources/models/library.ecore";
448
449
450 final EObject eObj = TestHelper.parseEObjectFromFile(filepath);
451
452 reset(daoMock);
453 expect(daoMock.load(eq(modelNsUri), isA(Map.class))).andReturn(eObj);
454 replay(daoMock);
455 try
456 {
457 assertEquals(TestHelper.genOmFromEObj(eObj).toString(), to.load(modelNsUri).toString());
458 }
459 catch (final MRException e)
460 {
461 fail("No error should occure here.");
462 }
463 catch (final Exception e)
464 {
465 fail("Could not Parse the EObj to OMElement.");
466
467
468 verify(daoMock);
469 }
470 }
471
472
473
474
475
476
477 @Test
478 public void testLoad2()
479 {
480 final String modelNsUri = "http://www.elinson.de/library/20090120";
481
482 reset(daoMock);
483 expect(daoMock.load(eq(modelNsUri), isA(Map.class))).andReturn(null);
484 replay(daoMock);
485 try
486 {
487 assertNull(to.load(modelNsUri));
488 }
489 catch (final MRException e)
490 {
491 assertTrue(e.getMessage().contains("No model"));
492 }
493
494
495 verify(daoMock);
496 }
497
498
499
500
501
502
503 @Test
504 public void testLoadFalse1()
505 {
506 final String modelNsUri = "http://www.elinson.de/library/20090120";
507
508 reset(daoMock);
509 expect(daoMock.load(eq(modelNsUri), isA(Map.class))).andReturn(null);
510 replay(daoMock);
511 try
512 {
513 to.load(modelNsUri);
514 fail("An error should occure");
515 }
516 catch (final MRException e)
517 {
518 assertTrue(e.getCause() instanceof IllegalArgumentException);
519 }
520
521
522 verify(daoMock);
523 }
524
525
526
527
528
529
530 @Test
531 public void testSave1()
532 {
533 final String modelNsUri = "http://www.elinson.de/library/20090122";
534
535 final String filepath = "src/test/resources/models/library.ecore";
536 final boolean modelExists = false;
537
538
539 final EObject eObj = TestHelper.parseEObjectFromFile(filepath);
540
541 try
542 {
543 reset(daoMock);
544 expect(daoMock.modelExists(modelNsUri)).andReturn(modelExists);
545 daoMock.save(isA(XMIResourceImpl.class));
546 replay(daoMock);
547 final OMElement testOMElem = TestHelper.genOmFromEObj(eObj);
548 to.save(testOMElem, filepath);
549 }
550 catch (final MRException e)
551 {
552 fail("No error should occure here.");
553 }
554 catch (final Exception e)
555 {
556 fail("Could not parse EObject. Check the testfiles.");
557 }
558
559
560 verify(daoMock);
561 }
562
563
564
565
566
567
568 @Test
569 public void testSave2()
570 {
571 final String modelNsUri = "http://www.elinson.de/library/20090122";
572
573 final String filepath = "src/test/resources/models/library.ecore";
574 final boolean modelExists = true;
575
576
577 final EObject eObj = TestHelper.parseEObjectFromFile(filepath);
578
579 reset(daoMock);
580 expect(daoMock.modelExists(modelNsUri)).andReturn(modelExists);
581 replay(daoMock);
582 try
583 {
584 final OMElement testOMElem = TestHelper.genOmFromEObj(eObj);
585 to.save(testOMElem, filepath);
586 fail("Save should fail because Model exists");
587 }
588 catch (final MRException e)
589 {
590 }
591 catch (final Exception e)
592 {
593 fail("Could not parse EObject. Check the testfiles.");
594 }
595
596
597 verify(daoMock);
598 }
599
600
601
602
603
604
605 @Test
606 public void testSave3()
607 {
608 final String filepath = "src/test/resources/models/leipzig_instance_of_flughafen.xml";
609 final String metaNsUri = "platform:/resource/Test/flughafen.ecore";
610 OMElement testOMElem = null;
611
612 reset(daoMock);
613 expect(daoMock.load(eq(metaNsUri), isA(Map.class))).andReturn(null);
614 replay(daoMock);
615
616 try
617 {
618 testOMElem = new StAXOMBuilder(filepath).getDocumentElement();
619 }
620 catch (final FileNotFoundException e)
621 {
622 fail("Error on creating the testOMElement. Testfile not found.");
623 }
624 catch (final XMLStreamException e)
625 {
626 fail("Error on creating the testOMElement. Testfile is invalid.");
627 }
628
629 try
630 {
631 to.save(testOMElem, filepath);
632 fail("Test should fail because no Metamodel is present.");
633 }
634 catch (final MRException e)
635 {
636 }
637
638
639 verify(daoMock);
640 }
641
642
643
644
645
646
647 @Test
648 public void testSave4()
649 {
650 final String filepath = "src/test/resources/models/leipzig_instance_of_flughafen.xml";
651 final String metaFilePath = "src/test/resources/models/flughafen.ecore";
652 final String metaNsUri = "platform:/resource/Test/flughafen.ecore";
653 OMElement testOMElem = null;
654
655
656 final EObject eMetaObj = TestHelper.parseEObjectFromFile(metaFilePath);
657
658 try
659 {
660 testOMElem = new StAXOMBuilder(filepath).getDocumentElement();
661 }
662 catch (final FileNotFoundException e)
663 {
664 fail("Error on creating the testOMElement. Testfile not found.");
665 }
666 catch (final XMLStreamException e)
667 {
668 fail("Error on creating the testOMElement. Testfile is invalid.");
669 }
670
671 try
672 {
673 reset(daoMock);
674 expect(daoMock.load(eq(metaNsUri), isA(Map.class))).andReturn(eMetaObj);
675 expect(daoMock.modelExists(metaNsUri + " [" + filepath + "]")).andReturn(false);
676 daoMock.save(isA(XMIResourceImpl.class));
677 replay(daoMock);
678 to.save(testOMElem, filepath);
679 }
680 catch (final MRException e)
681 {
682 fail("No error should occure here.");
683 }
684
685
686 verify(daoMock);
687 }
688
689
690
691
692
693
694
695 @Test
696 public void testSave5()
697 {
698 final String modelNsUri = "http://www.elinson.de/library/20090122";
699 final String subPackNsUri = "http://www.ncsc.de";
700
701 final String filepath = "src/test/resources/models/library_with_sub_package.ecore";
702 final boolean modelExists = false;
703 final boolean subPackExists = false;
704
705
706 final EObject eObj = TestHelper.parseEObjectFromFile(filepath);
707
708 try
709 {
710 reset(daoMock);
711 expect(daoMock.modelExists(modelNsUri)).andReturn(modelExists);
712 expect(daoMock.modelExists(subPackNsUri)).andReturn(subPackExists);
713 daoMock.save(isA(XMIResourceImpl.class));
714 replay(daoMock);
715 final OMElement testOMElem = TestHelper.genOmFromEObj(eObj);
716 to.save(testOMElem, filepath);
717 }
718 catch (final MRException e)
719 {
720 fail("No error should occure here.");
721 }
722 catch (final Exception e)
723 {
724 fail("Could not parse EObject. Check the testfiles.");
725 }
726
727
728 verify(daoMock);
729 }
730
731
732
733
734
735
736
737 @Test
738 public void testSave6()
739 {
740 final String modelNsUri = "http://www.elinson.de/library/20090122";
741 final String subPackNsUri = "http://www.ncsc.de";
742
743 final String filepath = "src/test/resources/models/library_with_sub_package.ecore";
744 final boolean modelExists = false;
745 final boolean subPackExists = true;
746
747
748 final EObject eObj = TestHelper.parseEObjectFromFile(filepath);
749
750 reset(daoMock);
751 expect(daoMock.modelExists(modelNsUri)).andReturn(modelExists);
752 expect(daoMock.modelExists(subPackNsUri)).andReturn(subPackExists);
753 replay(daoMock);
754 try
755 {
756 final OMElement testOMElem = TestHelper.genOmFromEObj(eObj);
757 to.save(testOMElem, filepath);
758 fail("No error should occure here.");
759 }
760 catch (final MRException e)
761 {
762 }
763 catch (final Exception e)
764 {
765 fail("Could not parse EObject. Check the testfiles.");
766 }
767
768
769 verify(daoMock);
770 }
771
772
773
774
775
776
777 @Test
778 public void testSave7()
779 {
780 final String filepath = "src/test/resources/models/leipzig_instance_of_flughafen.xml";
781 final String metaFilePath = "src/test/resources/models/flughafen.ecore";
782 final String metaNsUri = "platform:/resource/Test/flughafen.ecore";
783 OMElement testOMElem = null;
784
785
786 final EObject eMetaObj = TestHelper.parseEObjectFromFile(metaFilePath);
787
788 reset(daoMock);
789 expect(daoMock.load(eq(metaNsUri), isA(Map.class))).andReturn(eMetaObj);
790 expect(daoMock.modelExists(metaNsUri + " [" + filepath + "]")).andReturn(true);
791 replay(daoMock);
792
793 try
794 {
795 testOMElem = new StAXOMBuilder(filepath).getDocumentElement();
796 }
797 catch (final FileNotFoundException e)
798 {
799 fail("Error on creating the testOMElement. Testfile not found.");
800 }
801 catch (final XMLStreamException e)
802 {
803 fail("Error on creating the testOMElement. Testfile is invalid.");
804 }
805
806 try
807 {
808 to.save(testOMElem, filepath);
809 fail("Should throw an error.");
810 }
811 catch (final MRException e)
812 {
813 }
814
815
816 verify(daoMock);
817 }
818
819
820
821
822
823
824 @Test
825 public void testShutDown()
826 {
827 reset(daoMock);
828 daoMock.shutDown();
829 replay(daoMock);
830
831
832 to.shutDown(configctx, axisService);
833
834
835 verify(daoMock);
836 }
837
838 }