Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Stojsavljevic committed May 30, 2017
1 parent 9f9dcc6 commit 5cfdc47
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ClientInterfaceDeclarationRule implements Rule<JPackage,JDefinedCla

@Override
public JDefinedClass apply(ApiResourceMetadata controllerMetadata, JPackage generatableType) {
String clientClassName = controllerMetadata.getResourceName() + CLIENT_SUFFIX;
String clientClassName = controllerMetadata.getName() + CLIENT_SUFFIX;
JDefinedClass definedClass;
try {
definedClass = generatableType._interface(clientClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import static org.junit.Assert.assertThat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -39,6 +41,7 @@ public abstract class AbstractRuleTestBase {

public static final boolean VISUALISE_CODE = false;
public static final String RESOURCE_BASE = "rules/";
public static final String LINE_END = System.getProperty("line.separator");
public static RamlRoot RAML;

protected Logger logger = Logger.getLogger(this.getClass());
Expand Down Expand Up @@ -148,6 +151,21 @@ public String stripSpace(String toBeStripped) {
}
return result;
}

}

protected String removeSerialVersionUID(String serializedModel) throws IOException {

BufferedReader bufReader = new BufferedReader(new StringReader(serializedModel));
StringWriter stringWriter = new StringWriter();
BufferedWriter bufWriter = new BufferedWriter(stringWriter);
String line = null;
while ((line = bufReader.readLine()) != null) {
if (!line.contains("serialVersionUID = ")) {
bufWriter.write(line + LINE_END);
}
}
bufWriter.flush();
bufWriter.close();
return stringWriter.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.phoenixnap.oss.ramlapisync.generation.rules;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -22,8 +16,6 @@ public class Issue158RulesTest extends AbstractRuleTestBase {

private Rule<JCodeModel, JDefinedClass, ApiResourceMetadata> rule;

private static String LINE_END = System.getProperty("line.separator");

@BeforeClass
public static void initRaml() throws InvalidRamlResourceException {
AbstractRuleTestBase.RAML = RamlLoader.loadRamlFromFile(AbstractRuleTestBase.RESOURCE_BASE + "issue-158.raml");
Expand All @@ -37,20 +29,4 @@ public void applySpring4ControllerStubRule_shouldCreate_validCode() throws Excep
String removedSerialVersionUID = removeSerialVersionUID(serializeModel());
verifyGeneratedCode("Issue158Spring4ControllerStub", removedSerialVersionUID);
}

private String removeSerialVersionUID(String serializedModel) throws IOException {

BufferedReader bufReader = new BufferedReader(new StringReader(serializedModel));
StringWriter stringWriter = new StringWriter();
BufferedWriter bufWriter = new BufferedWriter(stringWriter);
String line = null;
while ((line = bufReader.readLine()) != null) {
if (!line.contains("serialVersionUID = ")) {
bufWriter.write(line + LINE_END);
}
}
bufWriter.flush();
bufWriter.close();
return stringWriter.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.phoenixnap.oss.ramlapisync.generation.rules;

import org.junit.BeforeClass;
import org.junit.Test;

import com.phoenixnap.oss.ramlapisync.data.ApiResourceMetadata;
import com.phoenixnap.oss.ramlapisync.generation.RamlParser;
import com.phoenixnap.oss.ramlapisync.pojo.PojoGenerationConfig;
import com.phoenixnap.oss.ramlapisync.raml.InvalidRamlResourceException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;

/**
* @author aleksandars
* @since 0.10.5
*/
public class Issue159RulesTest extends AbstractRuleTestBase {

private Rule<JCodeModel, JDefinedClass, ApiResourceMetadata> rule;

public Issue159RulesTest() {
super();
defaultRamlParser = new RamlParser(new PojoGenerationConfig().withPackage("com.gen.test", null), "/api", false,
false, 2);
}

@BeforeClass
public static void initRaml() throws InvalidRamlResourceException {
AbstractRuleTestBase.RAML = RamlLoader.loadRamlFromFile(AbstractRuleTestBase.RESOURCE_BASE + "issue-159.raml");
}

@Test
public void applySpring4ClientStubRule_shouldCreate_validCode() throws Exception {

rule = new Spring4RestTemplateClientRule();
rule.apply(getControllerMetadata(), jCodeModel);
String removedSerialVersionUID = removeSerialVersionUID(serializeModel());
verifyGeneratedCode("Issue159Spring4ControllerStub", removedSerialVersionUID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
-----------------------------------com.gen.test.model.Manager.java-----------------------------------

package com.gen.test.model;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public class Manager implements Serializable
{

private String firstName;
private String lastName;
private String departman;

/**
* Creates a new Manager.
*
*/
public Manager() {
super();
}

/**
* Creates a new Manager.
*
*/
public Manager(String firstName, String lastName, String departman) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.departman = departman;
}

public String toString() {
return ToStringBuilder.reflectionToString(this);
}

public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Manager) == false) {
return false;
}
Manager otherObject = ((Manager) other);
return new EqualsBuilder().isEquals();
}

public int hashCode() {
return new HashCodeBuilder().toHashCode();
}

/**
* Returns the firstName.
*
* @return
* firstName
*/
public String getFirstName() {
return firstName;
}

/**
* Set the firstName.
*
* @param firstName
* the new firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
* Returns the lastName.
*
* @return
* lastName
*/
public String getLastName() {
return lastName;
}

/**
* Set the lastName.
*
* @param lastName
* the new lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* Returns the departman.
*
* @return
* departman
*/
public String getDepartman() {
return departman;
}

/**
* Set the departman.
*
* @param departman
* the new departman
*/
public void setDepartman(String departman) {
this.departman = departman;
}

}
-----------------------------------com.gen.test.PersonManagerClient.java-----------------------------------

package com.gen.test;

import java.util.List;
import com.gen.test.model.Manager;
import org.springframework.http.ResponseEntity;


/**
* No description
* (Generated with springmvc-raml-parser v.${project.version})
*
*/
public interface PersonManagerClient {


/**
* No description
*
*/
public ResponseEntity<List<Manager>> getPersonsManagers();

/**
* No description
*
* @param manager The Request Body Payload
*/
public ResponseEntity<Manager> createPersonsManager(Manager manager);

}
-----------------------------------com.gen.test.PersonManagerClientImpl.java-----------------------------------

package com.gen.test;

import java.util.ArrayList;
import java.util.List;
import com.gen.test.model.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;


/**
* No description
* (Generated with springmvc-raml-parser v.${project.version})
*
*/
@Component
public class PersonManagerClientImpl
implements PersonManagerClient
{

@Autowired
private RestTemplate restTemplate;
@Value("${client.url}")
private String baseUrl;

/**
* No description
*
*/
public ResponseEntity<List<Manager>> getPersonsManagers() {
HttpHeaders httpHeaders = new HttpHeaders();
// Add Accepts Headers and Body Content-Type
ArrayList<MediaType> acceptsList = new ArrayList<MediaType>();
acceptsList.add(MediaType.valueOf("application/json"));
httpHeaders.setAccept(acceptsList);
String url = baseUrl.concat("/organization/persons/managers");
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
UriComponents uriComponents = builder.build();
HttpEntity httpEntity = new HttpEntity(httpHeaders);
class _P extends org.springframework.core.ParameterizedTypeReference<java.util.List<com.gen.test.model.Manager>>{};
ParameterizedTypeReference<List<Manager>> typeReference = new _P();
return this.restTemplate.exchange(uriComponents.encode().toUri(), HttpMethod.GET, httpEntity, typeReference);
}

/**
* No description
*
*/
public ResponseEntity<Manager> createPersonsManager(Manager manager) {
HttpHeaders httpHeaders = new HttpHeaders();
// Add Accepts Headers and Body Content-Type
ArrayList<MediaType> acceptsList = new ArrayList<MediaType>();
httpHeaders.setContentType(MediaType.valueOf("application/json"));
acceptsList.add(MediaType.valueOf("application/json"));
httpHeaders.setAccept(acceptsList);
String url = baseUrl.concat("/organization/persons/managers");
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
UriComponents uriComponents = builder.build();
HttpEntity httpEntity = new HttpEntity(manager, httpHeaders);
return this.restTemplate.exchange(uriComponents.encode().toUri(), HttpMethod.POST, httpEntity, Manager.class);
}

}
25 changes: 25 additions & 0 deletions springmvc-raml-parser/src/test/resources/rules/issue-159.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#%RAML 1.0
title: Managers
mediaType: application/json
baseUri: /api
types:
Manager:
properties:
firstName: string
lastName: string
departman: string

/organization:
/persons:
/managers:
get:
responses:
200:
body: Manager[]
post:
body: Manager
responses:
201:
body:
application/json:
type: Manager

0 comments on commit 5cfdc47

Please sign in to comment.