groovy samples

To generate the Mock Json data for testing

import groovy.json.JsonGenerator
import groovy.json.JsonOutput

class MockJSONServers {
    def static main(args) {

        def servers = []
        500.times { i ->
            Server s = new Server(hostname: "dbhost${i}", ip: "192.168.10.${i}", fqdn: "fqdn${i}", domain: "domain${i}", cpu: Math.abs(new Random().nextInt() % 26) + 1,
                    memory: Math.abs(new Random().nextInt() % 360) + 1, owner: "owner${i}", application: "Application${i}")
            servers.add(s)
        }
        def generator = new JsonGenerator.Options()
                .excludeNulls()
                .dateFormat('yyyy@MM')
                .excludeFieldsByName('age', 'password')
                .excludeFieldsByType(URL)
                .build()

        def output = JsonOutput.prettyPrint(generator.toJson(servers))
        new File('servers5.json').write(output)
        println output
    }
}

class Server {
    String hostname
    String ip
    String fqdn
    String domain
    String application
    int cpu
    int memory
    String owner
}

Copyright © 2004, Software Groups