88f29b8d5e051fc17b45907b1f5403b5e0390b6d
[utils] / system / general / src / test / java / org / wamblee / system / graph / component / ConnectRequiredProvidedEdgeFilterTest.java
1 /*
2  * Copyright 2008 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.wamblee.system.graph.component;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.wamblee.system.container.Application;
23 import org.wamblee.system.core.Environment;
24 import org.wamblee.system.graph.DefaultEdge;
25 import org.wamblee.system.graph.Edge;
26 import org.wamblee.system.graph.EdgeFilter;
27 import org.wamblee.system.graph.Node;
28 import org.wamblee.test.AssertionUtils;
29
30 import junit.framework.TestCase;
31
32 public class ConnectRequiredProvidedEdgeFilterTest extends TestCase {
33
34     private Application app1 = new Application("app1", "pf1.");
35     private Application app2 = new Application("app2", "pf2.");
36     
37     private Environment env1 = new Environment("env1", "pf3.");
38     private Environment env2 = new Environment("env2", "pf4.");
39  
40     
41     private void compare(Boolean[] aExpected, EdgeFilter aRestriction) { 
42        List<Boolean> result = new ArrayList<Boolean>();
43        
44       
45        
46        // order will be: 
47        //     env1, app1
48        //     env1, app2
49        //     env2, app1
50        //     env2, app2
51        for (Environment env: new Environment[] { env1, env2} ) { 
52            for (Application app: new Application[] { app1, app2} ) {
53                Node from = new RequiredInterfaceNode(
54                        app, app.getRequiredInterfaces().get(0));
55                Node to = new ProvidedInterfaceNode(
56                        env, env.getProvidedInterfaces().get(0));
57                Edge edge = new DefaultEdge(from, to); 
58                result.add(aRestriction.isViolated(edge));
59            }
60        }
61      
62        
63        assertEquals(Arrays.asList(aExpected), result);
64     }
65
66     public void testNoRestriction() { 
67         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
68             @Override
69             public void run() throws Exception {
70                 EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(null, null, null, null);
71                 
72             }
73         }, IllegalArgumentException.class);
74         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
75             @Override
76             public void run() throws Exception {
77                 EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(null, null, "x", "y");
78             }
79         }, IllegalArgumentException.class);
80     }
81     
82     public void testClientServer() { 
83         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1", null, "env1", null);
84         compare(new Boolean[] { false, false, true, false}, restriction);
85     }
86     
87     public void testNoConnectionsAtAll() { 
88         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1", null, null, null);
89         compare(new Boolean[] { true, false, true, false}, restriction);
90     }
91     
92     public void testExplicitConfig() { 
93         app1 = new Application("app1");
94         app2 = new Application("app2");
95         env1 = new Environment("env1");
96         env2 = new Environment("env2");
97         
98         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(
99                 "app2", "string", "env1", "datasource");
100         compare(new Boolean[] { false, false, false, true}, restriction);
101         
102     }
103 }