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 package gov.nist.secauto.metaschema.core.testing;
28
29 import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
30 import gov.nist.secauto.metaschema.core.model.IAssemblyInstance;
31 import gov.nist.secauto.metaschema.core.model.IFlagInstance;
32 import gov.nist.secauto.metaschema.core.model.INamedModelInstance;
33 import gov.nist.secauto.metaschema.core.util.CollectionUtil;
34
35 import org.jmock.Expectations;
36 import org.jmock.Mockery;
37
38 import java.util.List;
39 import java.util.Map;
40 import java.util.function.Function;
41 import java.util.stream.Collectors;
42
43 import edu.umd.cs.findbugs.annotations.NonNull;
44 import edu.umd.cs.findbugs.annotations.Nullable;
45
46 public class AssemblyBuilder
47 extends AbstractModelBuilder<AssemblyBuilder>
48 implements IModelInstanceBuilder {
49
50 private List<FlagBuilder> flags;
51 private List<? extends IModelInstanceBuilder> modelInstances;
52
53 private AssemblyBuilder(@NonNull Mockery ctx) {
54 super(ctx);
55 }
56
57 @NonNull
58 public static AssemblyBuilder builder(@NonNull Mockery ctx) {
59 return new AssemblyBuilder(ctx).reset();
60 }
61
62 @Override
63 public AssemblyBuilder reset() {
64 this.flags = CollectionUtil.emptyList();
65 this.modelInstances = CollectionUtil.emptyList();
66 return this;
67 }
68
69 public AssemblyBuilder flags(@Nullable List<FlagBuilder> flags) {
70 this.flags = flags == null ? CollectionUtil.emptyList() : flags;
71 return this;
72 }
73
74 public AssemblyBuilder modelInstances(@Nullable List<? extends IModelInstanceBuilder> modelInstances) {
75 this.modelInstances = modelInstances == null ? CollectionUtil.emptyList() : modelInstances;
76 return this;
77 }
78
79 @Override
80 @NonNull
81 public IAssemblyInstance toInstance(
82 @NonNull IAssemblyDefinition parent) {
83 IAssemblyDefinition def = toDefinition();
84 return toInstance(parent, def);
85 }
86
87 @NonNull
88 public IAssemblyInstance toInstance(
89 @NonNull IAssemblyDefinition parent,
90 @NonNull IAssemblyDefinition definition) {
91 validate();
92
93 IAssemblyInstance retval = mock(IAssemblyInstance.class);
94 applyNamedInstance(retval, definition, parent);
95 return retval;
96 }
97
98 @SuppressWarnings("null")
99 @NonNull
100 public IAssemblyDefinition toDefinition() {
101 validate();
102
103 IAssemblyDefinition retval = mock(IAssemblyDefinition.class);
104 applyDefinition(retval);
105
106 Map<String, IFlagInstance> flags = this.flags.stream()
107 .map(builder -> builder.toInstance(retval))
108 .collect(Collectors.toUnmodifiableMap(
109 IFlagInstance::getEffectiveName,
110 Function.identity()));
111
112 Map<String, ? extends INamedModelInstance> modelInstances = this.modelInstances.stream()
113 .map(builder -> builder.toInstance(retval))
114 .collect(Collectors.toUnmodifiableMap(
115 INamedModelInstance::getEffectiveName,
116 Function.identity()));
117
118 getContext().checking(new Expectations() {
119 {
120 allowing(retval).getFlagInstances();
121 will(returnValue(flags.values()));
122 flags.forEach((key, value) -> {
123 allowing(retval).getFlagInstanceByName(with(key));
124 will(returnValue(value));
125 });
126 allowing(retval).getModelInstances();
127 will(returnValue(modelInstances.values()));
128 modelInstances.forEach((key, value) -> {
129 allowing(retval).getModelInstanceByName(with(key));
130 will(returnValue(value));
131 });
132 }
133 });
134
135 return retval;
136 }
137 }