View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
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 }