View Javadoc
1   /*
2    * Cloudformation Plugin for SonarQube
3    * Copyright (C) 2019 James Pether Sörling
4    * james@hack23.com
5    *
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with this program; if not, write to the Free Software Foundation,
18   * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   */
20  package com.hack23.sonar.cloudformation.reports;
21  
22  import java.util.List;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.hack23.sonar.cloudformation.reports.cfnnag.CfnNagScanReport;
28  import com.hack23.sonar.cloudformation.reports.checkov.CheckovReport;
29  import com.openpojo.random.RandomFactory;
30  import com.openpojo.reflection.PojoClass;
31  import com.openpojo.reflection.PojoClassFilter;
32  import com.openpojo.reflection.PojoField;
33  import com.openpojo.reflection.filters.FilterPackageInfo;
34  import com.openpojo.reflection.impl.PojoClassFactory;
35  import com.openpojo.validation.Validator;
36  import com.openpojo.validation.ValidatorBuilder;
37  import com.openpojo.validation.affirm.Affirm;
38  import com.openpojo.validation.rule.impl.EqualsAndHashCodeMatchRule;
39  import com.openpojo.validation.rule.impl.GetterMustExistRule;
40  import com.openpojo.validation.rule.impl.SetterMustExistRule;
41  import com.openpojo.validation.test.Tester;
42  import com.openpojo.validation.test.impl.GetterTester;
43  import com.openpojo.validation.test.impl.SetterTester;
44  
45  /**
46   * The Class ModelSanityTest.
47   */
48  public final class ModelSanityTest extends Assert {
49  
50  	/** The Constant FilterPackageInfo. */
51  	private static final FilterPackageInfo FilterPackageInfo = new FilterPackageInfo();
52  
53  	/** The Constant EXPECT_CLASSES_IN_PACKAGE. */
54  	private static final String EXPECT_CLASSES_IN_PACKAGE = "Expect classes in package";
55  
56  
57  	/**
58  	 * Check cfn nag report package test.
59  	 */
60  	@Test
61  	public void checkCfnNagReportPackageTest() {
62  		assertTrue(EXPECT_CLASSES_IN_PACKAGE,
63  				checkAllClassesInPackage(CfnNagScanReport.class.getPackage().getName()));
64  
65  	}
66  
67  	/**
68  	 * Check checkov report package test.
69  	 */
70  	@Test
71  	public void checkCheckovReportPackageTest() {
72  		assertTrue(EXPECT_CLASSES_IN_PACKAGE,
73  				checkAllClassesInPackage(CheckovReport.class.getPackage().getName()));
74  
75  	}
76  
77  	/**
78  	 * Check all classes in package.
79  	 *
80  	 * @param string the string
81  	 * @return true, if successful
82  	 */
83  	protected boolean checkAllClassesInPackage(final String string) {
84  		final List<PojoClass> pojoClassesRecursively = PojoClassFactory.getPojoClassesRecursively(string,
85  				new FilterTestClasses());
86  
87  		final Validator validator = ValidatorBuilder.create().with(new SetterMustExistRule(), new GetterMustExistRule())
88  				.with(new SetterTester(), new GetterTester()).with(new InvokeToStringTester())
89  				.with(new InvokeHashcodeTester()).with(new DummyEqualsTester()).with(new EqualsAndHashCodeMatchRule()).build();
90  		validator.validate(pojoClassesRecursively);
91  
92  
93  		return true;
94  	}
95  
96  	/**
97  	 * The Class FilterTestClasses.
98  	 */
99  	private static class FilterTestClasses implements PojoClassFilter {
100 
101 		/**
102 		 * Include.
103 		 *
104 		 * @param pojoClass the pojo class
105 		 * @return true, if successful
106 		 */
107 		@Override
108 		public boolean include(final PojoClass pojoClass) {
109 			return (!pojoClass.getSourcePath().contains("/test-classes/") && !pojoClass.getClazz().getName().contains("_") && !pojoClass.isEnum() && !pojoClass.isAbstract())
110 					&& FilterPackageInfo.include(pojoClass);
111 		}
112 	}
113 
114 	/**
115 	 * The Class InvokeToStringTester.
116 	 */
117 	private static class InvokeToStringTester implements Tester {
118 
119 		/**
120 		 * Run.
121 		 *
122 		 * @param pojoClass the pojo class
123 		 */
124 		@Override
125 		public void run(final PojoClass pojoClass) {
126 			final Object instance = RandomFactory.getRandomValue(pojoClass.getClazz());
127 			Affirm.affirmNotNull("toStringFailure", instance.toString());
128 		}
129 	}
130 
131 	/**
132 	 * The Class InvokeHashcodeTester.
133 	 */
134 	private static class InvokeHashcodeTester implements Tester {
135 
136 		/**
137 		 * Run.
138 		 *
139 		 * @param pojoClass the pojo class
140 		 */
141 		@Override
142 		public void run(final PojoClass pojoClass) {
143 			final Object instance = RandomFactory.getRandomValue(pojoClass.getClazz());
144 			Affirm.affirmFalse("hashCodeFailure", 0 == instance.hashCode());
145 		}
146 	}
147 
148 	/**
149 	 * The Class DummyEqualsTester.
150 	 */
151 	private static class DummyEqualsTester implements Tester {
152 
153 		/**
154 		 * Run.
155 		 *
156 		 * @param pojoClass the pojo class
157 		 */
158 		@Override
159 		public void run(final PojoClass pojoClass) {
160 			final Object instance = randomValues(pojoClass);
161 
162 			Affirm.affirmFalse("EqualsCompareNullFailure", instance.equals(null));
163 			Affirm.affirmFalse("EqualsCompareWrongClassFailure", "WrongClass".equals(instance));
164 			Affirm.affirmTrue("EqualsCompareSelfFailure", instance.equals(instance));
165 
166 			final Object instance2 = randomValues(pojoClass);
167 
168 			instance.equals(instance2);
169 		}
170 
171 		/**
172 		 * Random values.
173 		 *
174 		 * @param pojoClass the pojo class
175 		 * @return the object
176 		 */
177 		private Object randomValues(final PojoClass pojoClass) {
178 			final Object instance = RandomFactory.getRandomValue(pojoClass.getClazz());
179 			randomValues(instance, pojoClass);
180 
181 			return instance;
182 		}
183 
184 		/**
185 		 * Random values.
186 		 *
187 		 * @param instance the instance
188 		 * @param pojoClass the pojo class
189 		 */
190 		private static void randomValues(final Object instance, final PojoClass pojoClass) {
191 			if (pojoClass == null) {
192 				return;
193 			}
194 
195 			for (final PojoField fieldEntry : pojoClass.getPojoFields()) {
196 				if (fieldEntry.hasSetter()) {
197 					final Object value;
198 
199 					value = RandomFactory.getRandomValue(fieldEntry);
200 					fieldEntry.invokeSetter(instance, value);
201 				}
202 			}
203 			randomValues(instance, pojoClass.getSuperClass());
204 		}
205 	}
206 
207 }