View Javadoc
1   /*
2    * Copyright 2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.core.chain;
17  
18  import org.junit.BeforeClass;
19  import org.junit.Test;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import static org.junit.Assert.assertEquals;
26  
27  /**
28   * @author Dmytro Rud
29   */
30  public class ChainUtilsTest {
31      private static final List<Chainable> INITIAL = new ArrayList<>();
32  
33  
34      @BeforeClass
35      public static void setUpClass() {
36          for (int i = 1; i <= 7; ++i) {
37              INITIAL.add(new MyChainable("i" + i, "", ""));
38          }
39      }
40  
41  
42      private static void doTest(List<Chainable> custom, String expected) {
43          List<Chainable> chain = ChainUtils.createChain(INITIAL, custom);
44          StringBuilder sb = new StringBuilder();
45          for (Chainable c : chain) {
46              sb.append(c.getId()).append(" ");
47          }
48          assertEquals(expected, sb.toString().trim());
49      }
50  
51  
52      @Test
53      public void testHappyCase() {
54          List<Chainable> custom = Arrays.asList(
55                  new MyChainable("c1", "", ""),
56                  new MyChainable("c1", "", ""),
57                  new MyChainable("c2", "i5 i6 i7", "i3"),
58                  new MyChainable("c4", "c2", "")
59          );
60          doTest(custom, "i1 i2 i3 c4 c2 i4 i5 i6 i7 c1");
61      }
62  
63      @Test
64      public void testAlreadyExistent() {
65          List<Chainable> custom = Arrays.asList(
66                  new MyChainable("i1", "", ""),
67                  new MyChainable("i2", "", ""),
68                  new MyChainable("i3", "i5 i6 i7", "i3"),
69                  new MyChainable("i4", "", "")
70          );
71          doTest(custom, "i1 i2 i3 i4 i5 i6 i7");
72      }
73  
74  
75      @Test(expected = ChainException.class)
76      public void testBeforeEqualsToAfter() {
77          List<Chainable> custom = Arrays.asList(
78                  new MyChainable("c1", "", ""),
79                  new MyChainable("c1", "", ""),
80                  new MyChainable("c2", "i5 i6 i7", "i3"),
81                  new MyChainable("c4", "c2", ""),
82                  new MyChainable("c5", "i4", "i4")        // should fail, Before==After
83          );
84          doTest(custom, "dummy");
85      }                                               
86  
87  
88      @Test(expected = ChainException.class)
89      public void testBeforeGreaterThanAfter() {
90          List<Chainable> custom = Arrays.asList(
91                  new MyChainable("c1", "", ""),
92                  new MyChainable("c1", "", ""),
93                  new MyChainable("c2", "i5 i6 i7", "i3"),
94                  new MyChainable("c4", "c2", ""),
95                  new MyChainable("c5", "i3", "i4")        // should fail, Before>After
96          );
97          doTest(custom, "dummy");
98      }
99  
100 
101     @Test(expected = ChainException.class)
102     public void testDependencyLoop() {
103         List<Chainable> custom = Arrays.asList(
104                 new MyChainable("c1", "c2", "c3"),
105                 new MyChainable("c2", "c1", "c3"),
106                 new MyChainable("c3", "c1", "c2")
107         );
108         doTest(custom, "dummy");
109     }
110 
111 }