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.IDefinition;
30 import gov.nist.secauto.metaschema.core.model.IFlagContainer;
31 import gov.nist.secauto.metaschema.core.model.IModelElement;
32 import gov.nist.secauto.metaschema.core.model.INamedInstance;
33 import gov.nist.secauto.metaschema.core.model.INamedModelElement;
34 import gov.nist.secauto.metaschema.core.util.CollectionUtil;
35
36 import org.apache.commons.lang3.ObjectUtils;
37 import org.jmock.Expectations;
38 import org.jmock.Mockery;
39
40 import edu.umd.cs.findbugs.annotations.NonNull;
41
42 public abstract class AbstractModelBuilder<T extends AbstractModelBuilder<T>>
43 extends MockFactory {
44
45 private String name;
46
47 protected AbstractModelBuilder(@NonNull Mockery ctx) {
48 super(ctx);
49 }
50
51 @NonNull
52 @SuppressWarnings("unchecked")
53 public T reset() {
54 this.name = null;
55 return (T) this;
56 }
57
58 @SuppressWarnings("unchecked")
59 @NonNull
60 public T name(@NonNull String name) {
61 this.name = name;
62 return (T) this;
63 }
64
65 protected void validate() {
66 ObjectUtils.requireNonEmpty(name, "name");
67 }
68
69 protected void applyDefinition(@NonNull IDefinition definition) {
70 applyNamedModelElement(definition);
71 getContext().checking(new Expectations() {
72 {
73 allowing(definition).getName();
74 will(returnValue(name));
75 allowing(definition).getEffectiveName();
76 will(returnValue(name));
77 allowing(definition).getUseName();
78 will(returnValue(null));
79 allowing(definition).getProperties();
80 will(returnValue(CollectionUtil.emptyMap()));
81 }
82 });
83 }
84
85 protected <DEF extends IDefinition> void applyNamedInstance(
86 @NonNull INamedInstance instance,
87 @NonNull DEF definition,
88 @NonNull IFlagContainer parent) {
89 applyNamedModelElement(instance);
90 getContext().checking(new Expectations() {
91 {
92 allowing(instance).getName();
93 will(returnValue(name));
94 allowing(instance).getEffectiveName();
95 will(returnValue(name));
96 allowing(instance).getUseName();
97 will(returnValue(null));
98 allowing(instance).getProperties();
99 will(returnValue(CollectionUtil.emptyMap()));
100 allowing(instance).getDefinition();
101 will(returnValue(definition));
102 allowing(instance).getContainingDefinition();
103 will(returnValue(parent));
104 allowing(instance).getParentContainer();
105 will(returnValue(parent));
106
107 }
108 });
109 }
110
111 protected void applyNamedModelElement(@NonNull INamedModelElement element) {
112 applyModelElement(element);
113 getContext().checking(new Expectations() {
114 {
115 allowing(element).getFormalName();
116 will(returnValue(null));
117 allowing(element).getDescription();
118 will(returnValue(null));
119 allowing(element).getProperties();
120 will(returnValue(CollectionUtil.emptyMap()));
121 allowing(element).getUseName();
122 will(returnValue(null));
123 }
124 });
125 }
126
127 protected void applyModelElement(@NonNull IModelElement element) {
128 getContext().checking(new Expectations() {
129 {
130 allowing(element).getRemarks();
131 will(returnValue(null));
132 }
133 });
134 }
135 }