1
0
mirror of https://github.com/firstBitMarksistskaya/jenkins-lib.git synced 2024-11-28 09:33:03 +02:00

Наметки по созданию конфигурационного файла

This commit is contained in:
Nikita Gryzlov 2020-03-27 17:23:59 +03:00
parent a605819dda
commit 91e869a2a6
No known key found for this signature in database
GPG Key ID: C1EAE411FEF0BF2F
11 changed files with 157 additions and 3 deletions

View File

@ -16,17 +16,25 @@ val junitVersion = "5.6.1"
val spockVersion = "1.3-groovy-2.4"
val groovyVersion = "2.4.19"
val slf4jVersion = "1.8.0-beta4"
var jacksonVersion = "2.9.8"
dependencies {
implementation("org.codehaus.groovy", "groovy-all", groovyVersion)
// jackson
implementation("com.fasterxml.jackson.module", "jackson-module-jsonSchema", jacksonVersion)
// unit-tests
testImplementation("org.junit.jupiter", "junit-jupiter-api", junitVersion)
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", junitVersion)
testImplementation("org.assertj", "assertj-core", "3.15.0")
testImplementation("org.mockito", "mockito-core", "3.3.3")
testImplementation("org.spockframework", "spock-core", spockVersion)
testImplementation("org.slf4j", "slf4j-api", slf4jVersion)
testImplementation("org.slf4j", "slf4j-simple", slf4jVersion)
// integration-tests
integrationTestImplementation("org.spockframework", "spock-core", spockVersion)
integrationTestImplementation("org.codehaus.groovy", "groovy-all", groovyVersion)

View File

@ -0,0 +1,4 @@
{
"$schema": "schema.json",
"sonarScannerToolName": "sonar-scanner"
}

14
resources/schema.json Normal file
View File

@ -0,0 +1,14 @@
{
"type" : "object",
"id" : "urn:jsonschema:ru:pulsar:jenkins:library:configuration:JobConfiguration",
"properties" : {
"v8version" : {
"type" : "string",
"description" : "Версия платформы 1С:Предприятие в формате 8.3.хх.хххх."
},
"sonarScannerToolName" : {
"type" : "string",
"description" : "Имя настроенной утилиты sonar-scanner."
}
}
}

View File

@ -0,0 +1,27 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
import ru.pulsar.jenkins.library.configuration.JobConfiguration;
import java.io.File;
import java.io.StringWriter;
public class JobConfigurationSchemaGenerator {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = generator.generateSchema(JobConfiguration.class);
StringWriter json = new StringWriter();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.writeValue(json, jsonSchema);
File jsonSchemaFile = new File("./resources/schema.json");
mapper.writeValue(jsonSchemaFile, jsonSchema);
System.out.println(json.toString());
}
}

View File

@ -8,4 +8,6 @@ interface IStepExecutor {
int bat(String script, boolean returnStatus, String encoding)
String libraryResource(String path)
}

View File

@ -22,4 +22,9 @@ class StepExecutor implements IStepExecutor {
int bat(String script, boolean returnStatus, String encoding) {
steps.bat script: script, returnStatus: returnStatus, encoding: encoding
}
@Override
String libraryResource(String path) {
steps.libraryResource path
}
}

View File

@ -0,0 +1,28 @@
package ru.pulsar.jenkins.library.configuration
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.commons.beanutils.BeanUtils
import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.ioc.ContextRegistry
class ConfigurationReader implements Serializable {
static JobConfiguration create(String config) {
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
def mapper = new ObjectMapper()
def globalConfig = steps.libraryResource 'globalConfiguration.json'
def globalConfiguration = mapper.readValue(globalConfig, JobConfiguration.class)
def jobConfiguration = mapper.readValue(config, JobConfiguration.class)
BeanUtils.describe(jobConfiguration).entrySet().stream()
.filter({ e -> e.getValue() != null })
.filter({ e -> e.getKey() != "class" })
.filter({ e -> e.getKey() != "metaClass" })
.forEach { e ->
BeanUtils.setProperty(globalConfiguration, e.getKey(), e.getValue());
}
return globalConfiguration
}
}

View File

@ -1,8 +1,13 @@
package ru.pulsar.jenkins.library.configuration
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonPropertyDescription
@JsonIgnoreProperties(ignoreUnknown = true)
class JobConfiguration {
class JobConfiguration implements Serializable {
@JsonPropertyDescription("Версия платформы 1С:Предприятие в формате 8.3.хх.хххх.")
String v8version
@JsonPropertyDescription("Имя настроенной утилиты sonar-scanner.")
String sonarScannerToolName
}

View File

@ -0,0 +1,48 @@
package ru.pulsar.jenkins.library.configuration;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import ru.pulsar.jenkins.library.IStepExecutor;
import ru.pulsar.jenkins.library.MockStepExecutor;
import ru.pulsar.jenkins.library.ioc.ContextRegistry;
import ru.pulsar.jenkins.library.ioc.IContext;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
class ConfigurationReaderTest {
@BeforeEach
void setUp() {
IContext context = mock(IContext.class);
IStepExecutor steps = spy(new MockStepExecutor());
when(context.getStepExecutor()).thenReturn(steps);
ContextRegistry.registerContext(context);
}
@Test
void testCreateJobConfigurationObject() throws IOException {
// given
String config = IOUtils.resourceToString(
"jobConfiguration.json",
StandardCharsets.UTF_8,
this.getClass().getClassLoader()
);
// when
JobConfiguration jobConfiguration = ConfigurationReader.create(config);
// then
assertThat(jobConfiguration.getV8version()).isEqualTo("8.3.14.1944");
assertThat(jobConfiguration.getSonarScannerToolName()).isEqualTo("sonar-scanner");
}
}

View File

@ -0,0 +1,3 @@
{
"v8version": "8.3.14.1944"
}

View File

@ -0,0 +1,10 @@
import ru.pulsar.jenkins.library.configuration.ConfigurationReader
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry
JobConfiguration call(String path = "") {
ContextRegistry.registerDefaultContext(this)
def config = readFile(path)
return ConfigurationReader.create(config)
}