1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.oscal.lib.profile.resolver;
7   
8   import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
9   import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory;
10  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
11  import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly;
12  import gov.nist.secauto.oscal.lib.OscalBindingContext;
13  import gov.nist.secauto.oscal.lib.model.Catalog;
14  import gov.nist.secauto.oscal.lib.model.control.AbstractParameter;
15  import gov.nist.secauto.oscal.lib.model.control.AbstractPart;
16  import gov.nist.secauto.oscal.lib.model.control.catalog.AbstractCatalogGroup;
17  import gov.nist.secauto.oscal.lib.model.control.catalog.AbstractControl;
18  import gov.nist.secauto.oscal.lib.model.metadata.AbstractProperty;
19  import gov.nist.secauto.oscal.lib.model.metadata.IProperty;
20  import gov.nist.secauto.oscal.lib.profile.resolver.support.IIdentifierMapper;
21  
22  import java.nio.file.Paths;
23  import java.util.Map;
24  import java.util.UUID;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  import edu.umd.cs.findbugs.annotations.NonNull;
28  
29  public final class TestUtil {
30  
31    @NonNull
32    public static final IIdentifierMapper UUID_CONCAT_ID_MAPPER = new IIdentifierMapper() {
33      private final Map<String, String> idToReassignmentMap = new ConcurrentHashMap<>();
34  
35      @NonNull
36      private String reassign(@NonNull String identifier) {
37        String retval = idToReassignmentMap.get(identifier);
38        if (retval == null) {
39          retval = identifier + "-" + UUID.randomUUID().toString();
40          idToReassignmentMap.put(identifier, retval);
41        }
42        return retval;
43      }
44  
45      @Override
46      public String mapRoleIdentifier(@NonNull String identifier) {
47        return reassign(identifier);
48      }
49  
50      @Override
51      public String mapControlIdentifier(@NonNull String identifier) {
52        return reassign(identifier);
53      }
54  
55      @Override
56      public String mapGroupIdentifier(@NonNull String identifier) {
57        return reassign(identifier);
58      }
59  
60      @Override
61      public String mapParameterIdentifier(@NonNull String identifier) {
62        return reassign(identifier);
63      }
64  
65      @Override
66      public @NonNull
67      String mapPartIdentifier(@NonNull String identifier) {
68        return reassign(identifier);
69      }
70    };
71  
72    private TestUtil() {
73      // disable construction
74    }
75  
76    @NonNull
77    public static IDocumentNodeItem newImportedCatalog() {
78  
79      // setup the imported catalog
80      Catalog importedCatalog = new Catalog();
81      importedCatalog.setUuid(UUID.randomUUID());
82  
83      importedCatalog.addParam(AbstractParameter.builder("param1")
84          .build());
85  
86      importedCatalog.addGroup(AbstractCatalogGroup.builder("group1")
87          .title("Group 1")
88          .part(AbstractPart.builder("statement")
89              .prose("group 1 part 1")
90              .build())
91          .param(AbstractParameter.builder("param2")
92              .build())
93          .control(AbstractControl.builder("control1")
94              .title("Control 1")
95              .param(AbstractParameter.builder("param3")
96                  .build())
97              .part(AbstractPart.builder("statement")
98                  .prose("A {{ insert: param, param1}} reference.")
99                  .build())
100             .part(AbstractPart.builder("statement")
101                 .prose("group 1 control 1 part 1")
102                 .part(AbstractPart.builder("statement")
103                     .prose("group 1 control 1 part 1.a")
104                     .build())
105                 .part(AbstractPart.builder("statement")
106                     .prose("group 1 control 1 part 1.b")
107                     .build())
108                 .build())
109             .part(AbstractPart.builder("statement")
110                 .prose("group 1 control 1 part 2")
111                 .build())
112             .build())
113         // to be filtered
114         .control(AbstractControl.builder("control2")
115             .title("Control 2")
116             .part(AbstractPart.builder("statement")
117                 .prose("A {{ insert: param, param2}} reference.")
118                 .build())
119             .build())
120         .build());
121     importedCatalog.addGroup(AbstractCatalogGroup.builder("group2")
122         .title("Group 2")
123         .param(AbstractParameter.builder("param4")
124             .prop(AbstractProperty.builder("aggregates")
125                 .namespace(IProperty.RMF_NAMESPACE)
126                 .value("param2")
127                 .build())
128             .build())
129         .control(AbstractControl.builder("control3")
130             .title("Control 3")
131             .build())
132         .control(AbstractControl.builder("control4")
133             .title("Control 4")
134             .build())
135         .group(AbstractCatalogGroup.builder("group3")
136             .title("Group 3")
137             // to be filtered
138             .control(AbstractControl.builder("control5")
139                 .title("Control 5")
140                 .build())
141             .build())
142         .control(AbstractControl.builder("control6")
143             .title("Control 6")
144             .part(AbstractPart.builder("statement")
145                 .prose("A {{ insert: param, param4}} reference.")
146                 .build())
147             .build())
148         // to be filtered
149         .control(AbstractControl.builder("control7")
150             .title("Control 7")
151             .param(AbstractParameter.builder("param5")
152                 .build())
153             .control(AbstractControl.builder("control8")
154                 .title("Control 8")
155                 .part(AbstractPart.builder("statement")
156                     .prose("A {{ insert: param, param5}} reference.")
157                     .build())
158                 .build())
159             .build())
160         .build());
161 
162     return INodeItemFactory.instance().newDocumentNodeItem(
163         ObjectUtils.requireNonNull(
164             (IBoundDefinitionModelAssembly) OscalBindingContext.instance().getBoundDefinitionForClass(Catalog.class)),
165         ObjectUtils.notNull(Paths.get(System.getProperty("user.dir")).toUri()),
166         importedCatalog);
167   }
168 }