@Test public void testValidSwaggerFile() throws Exception { File jsonFile = this.folder.newFile(); GenerateSwaggerJsonForWebSockets gen = new GenerateSwaggerJsonForWebSockets(); gen.generate( "v1", jsonFile ); Swagger swagger = ValidateSwaggerJsonFiles.validate( jsonFile ); Assert.assertEquals( GenerateSwaggerJsonForWebSockets.NAME_TO_DESC.size(), swagger.getPaths().size()); }
void generate( String roboconfVersion, File jsonFile ) throws Exception { Swagger swagger = new Swagger(); swagger.setBasePath( ServletRegistrationComponent.WEBSOCKET_CONTEXT ); swagger.setHost( "localhost:8181" ); Tag tag = new Tag( ); tag.setName( "FromServer" ); swagger.setTags( Arrays.asList( tag )); Contact contact = new Contact(); contact.setName( "the Roboconf team" ); contact.setUrl( "http: License license = new License(); license.setUrl( "http: license.setName( "The Apache Software License, Version 2.0" ); Info info = new Info(); info.setContact( contact ); info.setLicense( license ); info.setVersion( roboconfVersion ); info.setTitle( "Web Socket API" ); info.setDescription( "The Web Socket API for Roboconf's Administration" ); swagger.setInfo( info ); Map<String,Path> paths = new HashMap<>( NAME_TO_DESC.size()); swagger.setPaths( paths ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { ModelImpl model = new ModelImpl(); model.setType( ModelImpl.OBJECT ); model.setTitle( entry.getKey()); swagger.addDefinition( "json_" + entry.getKey(), model ); Operation operation = new Operation(); operation.setDescription( entry.getValue()); operation.setSummary( entry.getValue()); operation.setTags( Arrays.asList( tag.getName())); operation.setOperationId( entry.getKey()); operation.setProduces( Arrays.asList( MediaType.APPLICATION_JSON )); Response response = new Response(); response.setDescription( NAME_TO_PARAM.get( entry.getKey())); RefProperty schema = new RefProperty(); schema.set$ref( "json_" + entry.getKey()); response.setSchema( schema ); operation.addResponse( "default", response ); Path path = new Path(); path.setPost( operation ); paths.put( entry.getKey(), path ); } String swaggerJson = Json.pretty().writeValueAsString( swagger ); Map<String,String> definitions = prepareNewDefinitions(); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse( swaggerJson ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { String serialization = definitions.get( entry.getKey()); JsonElement serializationAsJson = jsonParser.parse( serialization ); jsonTree.getAsJsonObject() .get( "definitions" ).getAsJsonObject() .get( "json_" + entry.getKey()).getAsJsonObject() .add( "example", serializationAsJson ); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); swaggerJson = gson.toJson( jsonTree );; Utils.writeStringInto( swaggerJson, jsonFile ); }
GenerateSwaggerJsonForWebSockets { void generate( String roboconfVersion, File jsonFile ) throws Exception { Swagger swagger = new Swagger(); swagger.setBasePath( ServletRegistrationComponent.WEBSOCKET_CONTEXT ); swagger.setHost( "localhost:8181" ); Tag tag = new Tag( ); tag.setName( "FromServer" ); swagger.setTags( Arrays.asList( tag )); Contact contact = new Contact(); contact.setName( "the Roboconf team" ); contact.setUrl( "http: License license = new License(); license.setUrl( "http: license.setName( "The Apache Software License, Version 2.0" ); Info info = new Info(); info.setContact( contact ); info.setLicense( license ); info.setVersion( roboconfVersion ); info.setTitle( "Web Socket API" ); info.setDescription( "The Web Socket API for Roboconf's Administration" ); swagger.setInfo( info ); Map<String,Path> paths = new HashMap<>( NAME_TO_DESC.size()); swagger.setPaths( paths ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { ModelImpl model = new ModelImpl(); model.setType( ModelImpl.OBJECT ); model.setTitle( entry.getKey()); swagger.addDefinition( "json_" + entry.getKey(), model ); Operation operation = new Operation(); operation.setDescription( entry.getValue()); operation.setSummary( entry.getValue()); operation.setTags( Arrays.asList( tag.getName())); operation.setOperationId( entry.getKey()); operation.setProduces( Arrays.asList( MediaType.APPLICATION_JSON )); Response response = new Response(); response.setDescription( NAME_TO_PARAM.get( entry.getKey())); RefProperty schema = new RefProperty(); schema.set$ref( "json_" + entry.getKey()); response.setSchema( schema ); operation.addResponse( "default", response ); Path path = new Path(); path.setPost( operation ); paths.put( entry.getKey(), path ); } String swaggerJson = Json.pretty().writeValueAsString( swagger ); Map<String,String> definitions = prepareNewDefinitions(); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse( swaggerJson ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { String serialization = definitions.get( entry.getKey()); JsonElement serializationAsJson = jsonParser.parse( serialization ); jsonTree.getAsJsonObject() .get( "definitions" ).getAsJsonObject() .get( "json_" + entry.getKey()).getAsJsonObject() .add( "example", serializationAsJson ); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); swaggerJson = gson.toJson( jsonTree );; Utils.writeStringInto( swaggerJson, jsonFile ); } }
GenerateSwaggerJsonForWebSockets { void generate( String roboconfVersion, File jsonFile ) throws Exception { Swagger swagger = new Swagger(); swagger.setBasePath( ServletRegistrationComponent.WEBSOCKET_CONTEXT ); swagger.setHost( "localhost:8181" ); Tag tag = new Tag( ); tag.setName( "FromServer" ); swagger.setTags( Arrays.asList( tag )); Contact contact = new Contact(); contact.setName( "the Roboconf team" ); contact.setUrl( "http: License license = new License(); license.setUrl( "http: license.setName( "The Apache Software License, Version 2.0" ); Info info = new Info(); info.setContact( contact ); info.setLicense( license ); info.setVersion( roboconfVersion ); info.setTitle( "Web Socket API" ); info.setDescription( "The Web Socket API for Roboconf's Administration" ); swagger.setInfo( info ); Map<String,Path> paths = new HashMap<>( NAME_TO_DESC.size()); swagger.setPaths( paths ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { ModelImpl model = new ModelImpl(); model.setType( ModelImpl.OBJECT ); model.setTitle( entry.getKey()); swagger.addDefinition( "json_" + entry.getKey(), model ); Operation operation = new Operation(); operation.setDescription( entry.getValue()); operation.setSummary( entry.getValue()); operation.setTags( Arrays.asList( tag.getName())); operation.setOperationId( entry.getKey()); operation.setProduces( Arrays.asList( MediaType.APPLICATION_JSON )); Response response = new Response(); response.setDescription( NAME_TO_PARAM.get( entry.getKey())); RefProperty schema = new RefProperty(); schema.set$ref( "json_" + entry.getKey()); response.setSchema( schema ); operation.addResponse( "default", response ); Path path = new Path(); path.setPost( operation ); paths.put( entry.getKey(), path ); } String swaggerJson = Json.pretty().writeValueAsString( swagger ); Map<String,String> definitions = prepareNewDefinitions(); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse( swaggerJson ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { String serialization = definitions.get( entry.getKey()); JsonElement serializationAsJson = jsonParser.parse( serialization ); jsonTree.getAsJsonObject() .get( "definitions" ).getAsJsonObject() .get( "json_" + entry.getKey()).getAsJsonObject() .add( "example", serializationAsJson ); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); swaggerJson = gson.toJson( jsonTree );; Utils.writeStringInto( swaggerJson, jsonFile ); }  }
GenerateSwaggerJsonForWebSockets { void generate( String roboconfVersion, File jsonFile ) throws Exception { Swagger swagger = new Swagger(); swagger.setBasePath( ServletRegistrationComponent.WEBSOCKET_CONTEXT ); swagger.setHost( "localhost:8181" ); Tag tag = new Tag( ); tag.setName( "FromServer" ); swagger.setTags( Arrays.asList( tag )); Contact contact = new Contact(); contact.setName( "the Roboconf team" ); contact.setUrl( "http: License license = new License(); license.setUrl( "http: license.setName( "The Apache Software License, Version 2.0" ); Info info = new Info(); info.setContact( contact ); info.setLicense( license ); info.setVersion( roboconfVersion ); info.setTitle( "Web Socket API" ); info.setDescription( "The Web Socket API for Roboconf's Administration" ); swagger.setInfo( info ); Map<String,Path> paths = new HashMap<>( NAME_TO_DESC.size()); swagger.setPaths( paths ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { ModelImpl model = new ModelImpl(); model.setType( ModelImpl.OBJECT ); model.setTitle( entry.getKey()); swagger.addDefinition( "json_" + entry.getKey(), model ); Operation operation = new Operation(); operation.setDescription( entry.getValue()); operation.setSummary( entry.getValue()); operation.setTags( Arrays.asList( tag.getName())); operation.setOperationId( entry.getKey()); operation.setProduces( Arrays.asList( MediaType.APPLICATION_JSON )); Response response = new Response(); response.setDescription( NAME_TO_PARAM.get( entry.getKey())); RefProperty schema = new RefProperty(); schema.set$ref( "json_" + entry.getKey()); response.setSchema( schema ); operation.addResponse( "default", response ); Path path = new Path(); path.setPost( operation ); paths.put( entry.getKey(), path ); } String swaggerJson = Json.pretty().writeValueAsString( swagger ); Map<String,String> definitions = prepareNewDefinitions(); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse( swaggerJson ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { String serialization = definitions.get( entry.getKey()); JsonElement serializationAsJson = jsonParser.parse( serialization ); jsonTree.getAsJsonObject() .get( "definitions" ).getAsJsonObject() .get( "json_" + entry.getKey()).getAsJsonObject() .add( "example", serializationAsJson ); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); swaggerJson = gson.toJson( jsonTree );; Utils.writeStringInto( swaggerJson, jsonFile ); }  static void main( String[] args ); void run( String[] args ); Map<String,String> prepareNewDefinitions(); }
GenerateSwaggerJsonForWebSockets { void generate( String roboconfVersion, File jsonFile ) throws Exception { Swagger swagger = new Swagger(); swagger.setBasePath( ServletRegistrationComponent.WEBSOCKET_CONTEXT ); swagger.setHost( "localhost:8181" ); Tag tag = new Tag( ); tag.setName( "FromServer" ); swagger.setTags( Arrays.asList( tag )); Contact contact = new Contact(); contact.setName( "the Roboconf team" ); contact.setUrl( "http: License license = new License(); license.setUrl( "http: license.setName( "The Apache Software License, Version 2.0" ); Info info = new Info(); info.setContact( contact ); info.setLicense( license ); info.setVersion( roboconfVersion ); info.setTitle( "Web Socket API" ); info.setDescription( "The Web Socket API for Roboconf's Administration" ); swagger.setInfo( info ); Map<String,Path> paths = new HashMap<>( NAME_TO_DESC.size()); swagger.setPaths( paths ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { ModelImpl model = new ModelImpl(); model.setType( ModelImpl.OBJECT ); model.setTitle( entry.getKey()); swagger.addDefinition( "json_" + entry.getKey(), model ); Operation operation = new Operation(); operation.setDescription( entry.getValue()); operation.setSummary( entry.getValue()); operation.setTags( Arrays.asList( tag.getName())); operation.setOperationId( entry.getKey()); operation.setProduces( Arrays.asList( MediaType.APPLICATION_JSON )); Response response = new Response(); response.setDescription( NAME_TO_PARAM.get( entry.getKey())); RefProperty schema = new RefProperty(); schema.set$ref( "json_" + entry.getKey()); response.setSchema( schema ); operation.addResponse( "default", response ); Path path = new Path(); path.setPost( operation ); paths.put( entry.getKey(), path ); } String swaggerJson = Json.pretty().writeValueAsString( swagger ); Map<String,String> definitions = prepareNewDefinitions(); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse( swaggerJson ); for( Map.Entry<String,String> entry : NAME_TO_DESC.entrySet()) { String serialization = definitions.get( entry.getKey()); JsonElement serializationAsJson = jsonParser.parse( serialization ); jsonTree.getAsJsonObject() .get( "definitions" ).getAsJsonObject() .get( "json_" + entry.getKey()).getAsJsonObject() .add( "example", serializationAsJson ); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); swaggerJson = gson.toJson( jsonTree );; Utils.writeStringInto( swaggerJson, jsonFile ); }  static void main( String[] args ); void run( String[] args ); Map<String,String> prepareNewDefinitions();  }