summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java
blob: e3d747fc28b2bde2acd2cf6190e826a8a875ff0b (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
/*
 * 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.implementation.java.introspect.impl;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory;
import org.apache.tuscany.sca.implementation.java.JavaImplementation;
import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory;
import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.apache.tuscany.sca.interfacedef.java.impl.PolicyJavaInterfaceVisitor;
import org.apache.tuscany.sca.policy.Intent;
import org.apache.tuscany.sca.policy.PolicySubject;
import org.junit.Before;
import org.junit.Test;
import org.oasisopen.sca.annotation.Requires;
import org.oasisopen.sca.annotation.Service;

/**
 * @version $Rev$ $Date$
 */
public class PolicyProcessorTestCase {
    private ServiceProcessor serviceProcessor;
    private PolicyProcessor policyProcessor;
    private PolicyJavaInterfaceVisitor visitor;
    private JavaImplementation type;

    // This actually is a test for PolicyJavaInterfaceProcessor. It will get
    // invoked via the call to ImplementationProcessorServiceImpl.createService in
    // ServiceProcessor. Of course ServiceProcessor class has to be working.
    @Test
    public void testSingleInterfaceWithIntentsOnInterfaceAtInterfaceLevel() throws Exception {
        serviceProcessor.visitClass(Service1.class, type);
        visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface());
        policyProcessor.visitClass(Service1.class, type);
        verifyIntents(Service1.class, type);
    }

    @Test
    public void testMultipleInterfacesWithIntentsOnInterfaceAtInterfaceLevel() throws Exception {
        serviceProcessor.visitClass(Service2.class, type);
        visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface());
        visitor.visitInterface((JavaInterface)type.getServices().get(1).getInterfaceContract().getInterface());
        policyProcessor.visitClass(Service2.class, type);
        verifyIntents(Service2.class, type);
    }

    @Test
    public void testSingleInterfaceWithIntentsOnImplAtClassLevel() throws Exception {
        serviceProcessor.visitClass(Service3.class, type);
        visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface());
        policyProcessor.visitClass(Service3.class, type);
        verifyIntents(Service3.class, type);
    }

    @Test
    public void testMultipleInterfacesWithIntentsOnImplAtClassLevel() throws Exception {
        serviceProcessor.visitClass(Service4.class, type);
        visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface());
        policyProcessor.visitClass(Service4.class, type);
        verifyIntents(Service4.class, type);
    }

    public void stestSingleInterfaceWithIntentsOnInterfaceAtMethodLevel() throws Exception {
        serviceProcessor.visitClass(Service5.class, type);
        visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface());
        policyProcessor.visitClass(Service5.class, type);
        verifyIntents(Service5.class, type);
    }

    @Test
    public void testSingleInterfaceWithIntentsOnServiceAndInterfaceAtImplAndInertfaceAndMethodLevel() throws Exception {
        serviceProcessor.visitClass(Service6.class, type);
        visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface());
        policyProcessor.visitClass(Service6.class, type);
        for (Method method : Service6.class.getDeclaredMethods()) {
            policyProcessor.visitMethod(method, type);
        }
        verifyIntents(Service6.class, type);
    }

    private void verifyIntents(Class<?> serviceImplClass, JavaImplementation type) {
        if ( !(type instanceof PolicySubject) ) {
            fail("No Intents on the service ");
        }
        Requires serviceImplIntentAnnotation = (Requires)serviceImplClass.getAnnotation(Requires.class);
        if (serviceImplIntentAnnotation != null) {
            String[] serviceImplIntents = serviceImplIntentAnnotation.value();
            List<Intent> requiredIntents = ((PolicySubject)type).getRequiredIntents();
            if (serviceImplIntents.length > 0) {
                if (requiredIntents == null || requiredIntents.size() == 0) {
                    fail("No Intents on the service ");
                }
                Map<String, Intent> intentMap = new HashMap<String, Intent>();
                for (Intent intent : requiredIntents) {
                    intentMap.put(intent.getName().getLocalPart(), intent);
                }
                for (String intent : serviceImplIntents) {
                    assertTrue("ComponentType for Service class " + serviceImplClass.getName()
                        + " did not contain Service Implementation intent "
                        + intent, intentMap.containsKey(intent));
                }
            }
        }

        // This should match what was specified on @Service for a Service Implementation
        // If we use these to get the Service names and we get a null Service
        // name then it would seem that wrong values were put on the @Service annotation
        // or the wrong interfaces were specified on the implements list of the class
        // statement?
        Map<String, org.apache.tuscany.sca.assembly.Service> serviceMap = new HashMap<String, org.apache.tuscany.sca.assembly.Service>();
        for (org.apache.tuscany.sca.assembly.Service service: type.getServices()) {
            serviceMap.put(service.getName(), service);
        }
        for (Class<?> interfaceClass : serviceImplClass.getInterfaces()) {
            Requires interfaceIntentAnnotation = (Requires)interfaceClass.getAnnotation(Requires.class);
            org.apache.tuscany.sca.assembly.Service service = serviceMap.get(interfaceClass.getSimpleName());
            if (service == null) {
                fail("No service defined for interface " + interfaceClass.getSimpleName()
                    + " on Service Implementation "
                    + serviceImplClass.getName());
            }

            if (interfaceIntentAnnotation != null) {
                String[] interfaceIntents = interfaceIntentAnnotation.value();
                List<Intent> requiredIntents = service.getInterfaceContract().getInterface().getRequiredIntents();
                if (interfaceIntents.length > 0) {
                    if (requiredIntents == null || requiredIntents.size() == 0) {
                        fail("No Intents on the service " + service.getName());
                    }
                    Map<String, Intent> intentMap = new HashMap<String, Intent>();
                    for (Intent intent : requiredIntents) {
                        intentMap.put(intent.getName().getLocalPart(), intent);
                    }
                    for (String intent : interfaceIntents) {
                        assertTrue("Interface " + service.getName()
                            + " did not contain Service Interface intent "
                            + intent, intentMap.containsKey(intent));
                    }
                }
            }

            /*
            for (Method method : interfaceClass.getDeclaredMethods()) {
                Requires methodIntentAnnotation = method.getAnnotation(Requires.class);

                // Verify that each of the Intents on each of the Service
                // Interface Methods exist on their associated operation.
                if (methodIntentAnnotation != null) {
                    String[] methodIntents = methodIntentAnnotation.value();
                    if (methodIntents.length > 0) {
                        List<Intent> requiredIntents = null;
                        for ( ConfiguredOperation confOp : service.getConfiguredOperations() ) {
                            if ( confOp.getName().equals(method.getName()) &&
                                    confOp.getContractName().equals(service.getName()) ) {
                                requiredIntents = confOp.getRequiredIntents();
                            }
                        }
                        
                        if (requiredIntents == null || requiredIntents.size() == 0) {
                            fail("No Intents on operation " + method.getName());
                        }
                        for (String intent : methodIntents) {
                            boolean found = false;
                            for (Intent requiredIntent: requiredIntents) {
                                if (requiredIntent.getName().getLocalPart().equals(intent)) {
                                    found = true;
                                    break;
                                }
                            }
                            assertTrue("Operation " + method.getName()
                                + " did not contain Service Interface method intent "
                                + intent, found);
                        }
                    }
                }
            }
            
            for (Method method : serviceImplClass.getDeclaredMethods()) {
                Requires methodIntentAnnotation = method.getAnnotation(Requires.class);

                // Verify that each of the Intents on each of the Service
                // Implementation Methods exist on their associated
                // operation.
                if (methodIntentAnnotation != null) {
                    String[] methodIntents = methodIntentAnnotation.value();
                    if (methodIntents.length > 0) {
                        List<Intent> requiredIntents = null;
                        for ( ConfiguredOperation confOp : ((OperationsConfigurator)type).getConfiguredOperations() ) {
                            if ( confOp.getName().equals(method.getName())  ) {
                                requiredIntents = confOp.getRequiredIntents();
                            }
                        }
                        
                        if (requiredIntents == null || requiredIntents.size() == 0) {
                            fail("No Intents on operation " + method.getName());
                        }
                        
                        for (String intent : methodIntents) {
                            boolean found = false;
                            for (Intent requiredIntent: requiredIntents) {
                                if (requiredIntent.getName().getLocalPart().equals(intent)) {
                                    found = true;
                                    break;
                                }
                            }
                            assertTrue("Operation " + method.getName()
                                + " did not contain Implementation method intent "
                                + intent, found);
                        }
                    }
                }
            }
            */
        }
    }

    @Before
    public void setUp() throws Exception {
        ExtensionPointRegistry registry = new DefaultExtensionPointRegistry();
        serviceProcessor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry));
        policyProcessor = new PolicyProcessor(registry);
        visitor = new PolicyJavaInterfaceVisitor(registry);
        JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory();
        type = javaImplementationFactory.createJavaImplementation();
    }

    // @Remotable
    @Requires( {"transaction.global"})
    private interface Interface1 {
        int method1();

        int method2();

        int method3();

        int method4();
    }

    @Service(Interface1.class)
    private class Service1 implements Interface1 {
        public int method1() {
            return 0;
        }

        public int method2() {
            return 0;
        }

        public int method3() {
            return 0;
        }

        public int method4() {
            return 0;
        }
    }

    // @Remotable
    @Requires( {"transaction.local"})
    private interface Interface2 {
        int method5();

        int method6();
    }

    @Service({Interface1.class, Interface2.class})
    private class Service2 implements Interface1, Interface2 {
        public int method1() {
            return 0;
        }

        public int method2() {
            return 0;
        }

        public int method3() {
            return 0;
        }

        public int method4() {
            return 0;
        }

        public int method5() {
            return 0;
        }

        public int method6() {
            return 0;
        }
    }

    // @Remotable
    private interface Interface3 {
        int method1();

        int method2();

        int method3();

        int method4();
    }

    @Service(Interface3.class)
    @Requires( {"transaction.global"})
    private class Service3 implements Interface3 {
        public int method1() {
            return 0;
        }

        public int method2() {
            return 0;
        }

        public int method3() {
            return 0;
        }

        public int method4() {
            return 0;
        }
    }

    // @Remotable
    private interface Interface4 {
        int method5();

        int method6();
    }

    @Service({Interface3.class, Interface4.class})
    @Requires( {"transaction.local"})
    private class Service4 implements Interface3, Interface4 {
        public int method1() {
            return 0;
        }

        public int method2() {
            return 0;
        }

        public int method3() {
            return 0;
        }

        public int method4() {
            return 0;
        }

        public int method5() {
            return 0;
        }

        public int method6() {
            return 0;
        }
    }

    private interface Interface5 {
        @Requires( {"transaction.global"})
        int method1();

        @Requires( {"transaction.local"})
        int method2();
    }

    @Service(Interface5.class)
    private class Service5 implements Interface5 {
        public int method1() {
            return 0;
        }

        public int method2() {
            return 0;
        }
    }

    @Requires( {"transaction.global.Interface6"})
    private interface Interface6 {
        @Requires( {"transaction.global.Interface6.method1"})
        int method1();

        @Requires( {"transaction.local.Interface6.method2"})
        int method2();
    }

    @Service(Interface6.class)
    @Requires( {"transaction.global.Service6"})
    private class Service6 implements Interface6 {
        // @Requires( {"transaction.global.Service6.method1"})
        public int method1() {
            return 0;
        }

        // @Requires( {"transaction.global.Service6.method1"})
        public int method2() {
            return 0;
        }
    }

}