bc5ad18963a292bbd221c1a3944ce79253ff9424
[cps.git] / cps-application / src / test / java / org / onap / cps / architecture / LayeredArchitectureTest.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
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  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.architecture;
21
22
23 import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
24 import static com.tngtech.archunit.library.freeze.FreezingArchRule.freeze;
25
26 import com.tngtech.archunit.core.importer.ImportOption;
27 import com.tngtech.archunit.junit.AnalyzeClasses;
28 import com.tngtech.archunit.junit.ArchTest;
29 import com.tngtech.archunit.lang.ArchRule;
30
31 /**
32  * Test class responsible for layered architecture.
33  */
34 @AnalyzeClasses(packages = "org.onap.cps", importOptions = {ImportOption.DoNotIncludeTests.class})
35 public class LayeredArchitectureTest {
36
37     private static final String REST_CONTROLLER_PACKAGE = "org.onap.cps.rest..";
38     private static final String NCMP_REST_PACKAGE = "org.onap.cps.ncmp.rest..";
39     private static final String API_SERVICE_PACKAGE = "org.onap.cps.api..";
40     private static final String SPI_SERVICE_PACKAGE = "org.onap.cps.spi..";
41     private static final String NCMP_SERVICE_PACKAGE = "org.onap.cps.ncmp.api..";
42     private static final String SPI_REPOSITORY_PACKAGE = "org.onap.cps.spi.repository..";
43     private static final String YANG_SCHEMA_PACKAGE = "org.onap.cps.yang..";
44     private static final String NOTIFICATION_PACKAGE = "org.onap.cps.notification..";
45
46     @ArchTest
47     static final ArchRule restControllerShouldOnlyDependOnRestController =
48         classes().that().resideInAPackage(REST_CONTROLLER_PACKAGE).should().onlyHaveDependentClassesThat()
49             .resideInAPackage(REST_CONTROLLER_PACKAGE);
50
51     @ArchTest
52     static final ArchRule apiOrSpiServiceShouldOnlyBeDependedOnByControllerAndServices =
53         freeze(classes().that().resideInAPackage(API_SERVICE_PACKAGE)
54             .or().resideInAPackage(SPI_SERVICE_PACKAGE).should().onlyHaveDependentClassesThat()
55             .resideInAnyPackage(REST_CONTROLLER_PACKAGE, API_SERVICE_PACKAGE, SPI_SERVICE_PACKAGE, NCMP_REST_PACKAGE,
56                 NCMP_SERVICE_PACKAGE, YANG_SCHEMA_PACKAGE, NOTIFICATION_PACKAGE));
57
58     @ArchTest
59     static final ArchRule repositoryShouldOnlyBeDependedOnByServicesAndRepository =
60         classes().that().resideInAPackage(SPI_REPOSITORY_PACKAGE).should().onlyHaveDependentClassesThat()
61             .resideInAnyPackage(API_SERVICE_PACKAGE, SPI_SERVICE_PACKAGE, SPI_REPOSITORY_PACKAGE);
62 }