aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/cps/temporal/architecture/LayeredArchitectureTest.java
blob: d47e8a57e5ec8e0661132203b4920947d8fc6d75 (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
/*
 * ============LICENSE_START=======================================================
 * Copyright (c) 2021 Bell Canada.
 * ================================================================================
 * Licensed 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.cps.temporal.architecture;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.library.Architectures.layeredArchitecture;

import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;

/**
 * Test class responsible for layered architecture.
 */
@AnalyzeClasses(packages = "org.onap.cps.temporal", importOptions = { ImportOption.DoNotIncludeTests.class })
public class LayeredArchitectureTest {

    private static final String CONTROLLER_PACKAGE = "org.onap.cps.temporal.controller..";
    private static final String SERVICE_PACKAGE = "org.onap.cps.temporal.service..";
    private static final String REPOSITORY_PACKAGE = "org.onap.cps.temporal.repository..";

    // 'access' catches only violations by real accesses,
    // i.e. accessing a field, calling a method; compare 'dependOn' further down

    @ArchTest
    public static final ArchRule layeredArchitectureRule =
            layeredArchitecture()
                    .layer("Controller").definedBy(CONTROLLER_PACKAGE)
                    .layer("Service").definedBy(SERVICE_PACKAGE)
                    .layer("Repository").definedBy(REPOSITORY_PACKAGE)
                    .whereLayer("Controller").mayNotBeAccessedByAnyLayer()
                    .whereLayer("Service").mayOnlyBeAccessedByLayers("Controller")
                    .whereLayer("Repository").mayOnlyBeAccessedByLayers("Service");

    // 'dependOn' catches a wider variety of violations,
    // e.g. having fields of type, having method parameters of type, extending type ...

    @ArchTest
    static final ArchRule controllerDependencyRule =
            classes().that().resideInAPackage(CONTROLLER_PACKAGE)
                    .should().onlyHaveDependentClassesThat()
                    .resideInAPackage(CONTROLLER_PACKAGE);

    @ArchTest
    static final ArchRule serviceDependencyRule =
            classes().that().resideInAPackage(SERVICE_PACKAGE)
                    .should().onlyHaveDependentClassesThat()
                    .resideInAnyPackage(CONTROLLER_PACKAGE, SERVICE_PACKAGE);

    @ArchTest
    static final ArchRule repositoryDependencyRule =
            classes().that().resideInAPackage(REPOSITORY_PACKAGE)
                    .should().onlyHaveDependentClassesThat()
                    .resideInAnyPackage(SERVICE_PACKAGE, REPOSITORY_PACKAGE);

}