|
| 1 | +package com.ericsson.eiffel.remrem.generate.service; |
| 2 | + |
| 3 | +import ch.qos.logback.classic.Logger; |
| 4 | +import com.ericsson.eiffel.remrem.protocol.MsgService; |
| 5 | +import com.ericsson.eiffel.remrem.semantics.EiffelEventType; |
| 6 | +import com.ericsson.eiffel.remrem.semantics.factory.EiffelOutputValidatorFactory; |
| 7 | +import com.ericsson.eiffel.remrem.semantics.validator.EiffelValidator; |
| 8 | +import com.google.gson.JsonElement; |
| 9 | +import com.google.gson.JsonObject; |
| 10 | +import com.google.gson.JsonParser; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.lang.reflect.Field; |
| 22 | + |
| 23 | +import static org.junit.Assert.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * This test basically test the difference of version between |
| 27 | + * generated schemas and the event template |
| 28 | + */ |
| 29 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 30 | +@EnableAutoConfiguration |
| 31 | +@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 32 | +public class EiffelTemplateGenerateTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private MsgService msgServices; |
| 36 | + |
| 37 | + private Logger log = (Logger) LoggerFactory.getLogger(EiffelTemplateGenerateTest.class); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testEiffelSemanticsTemplate() throws IOException { |
| 41 | + for (EiffelEventType enumValue:EiffelEventType.values()) { |
| 42 | + testEventType(enumValue.getEventName()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * In this basically we generate event and schemas version and compare it |
| 48 | + * @param eventName name of the event |
| 49 | + */ |
| 50 | + protected void testEventType(String eventName){ |
| 51 | + |
| 52 | + JsonElement jsonTemplate= msgServices.getEventTemplate(eventName); |
| 53 | + JsonObject jsonObjectTemplate = jsonTemplate.getAsJsonObject(); |
| 54 | + |
| 55 | + String templateVersion = jsonObjectTemplate.getAsJsonObject("msgParams") |
| 56 | + .getAsJsonObject("meta").get("version").getAsString(); |
| 57 | + |
| 58 | + |
| 59 | + EiffelEventType eiffelType = EiffelEventType.fromString(eventName); |
| 60 | + EiffelValidator validator = EiffelOutputValidatorFactory.getEiffelValidator(eiffelType); |
| 61 | + |
| 62 | + String schemaResourceName = null; |
| 63 | + try { |
| 64 | + // Note, that this isn't a standard solution. Unfortunately, there's no standard way how to access schema |
| 65 | + // resource name from validator. That's why Java reflection takes place. It's acceptable just because it's |
| 66 | + // a takes case, and it isn't part of production code. |
| 67 | + // Get access to EiffelValidator.schemaResourceName even it's declared as private. |
| 68 | + Field schemaResourceNameField = EiffelValidator.class.getDeclaredField("schemaResourceName"); |
| 69 | + schemaResourceNameField.setAccessible(true); |
| 70 | + schemaResourceName = (String)schemaResourceNameField.get(validator); |
| 71 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 72 | + assertNull("cannot get field name", e); |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + assertNotNull("Cannot get the schema file name", schemaResourceName); |
| 76 | + |
| 77 | + ClassLoader classLoader = validator.getClass().getClassLoader(); |
| 78 | + InputStream inputStream = classLoader.getResourceAsStream(schemaResourceName); |
| 79 | + String stringTypeSchemas = null; |
| 80 | + try { |
| 81 | + stringTypeSchemas = new String(inputStream.readAllBytes()); |
| 82 | + } catch (IOException e) { |
| 83 | + fail("Cannot read resource " + schemaResourceName); |
| 84 | + } |
| 85 | + |
| 86 | + JsonObject SchemasAsJsonObject = JsonParser.parseString(stringTypeSchemas).getAsJsonObject(); |
| 87 | + String schemasVersion = SchemasAsJsonObject.getAsJsonObject("properties").getAsJsonObject("meta"). |
| 88 | + getAsJsonObject("properties").getAsJsonObject("version"). |
| 89 | + get("default").getAsString(); |
| 90 | + |
| 91 | + if (!templateVersion.equals(schemasVersion)){ |
| 92 | + // At the moment only semantics library 2.2.1 has been hot-fixed. The later versions still have the issue |
| 93 | + // (it hasn't been decided how/when/if the issue will be resolved). If standard "assert" was used the test |
| 94 | + // would fail for the other versions of the library, and this is unwanted as the test would fail as a whole. |
| 95 | + log.warn("Versions of event " + eventName + " must be the same; found: " + templateVersion + |
| 96 | + ", expected: " + schemasVersion); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments