Commit a5d45408 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added handleExceptions

parent c98d954e
package nl.uva.sne.drip.api; package nl.uva.sne.drip.api;
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z") import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
public class ApiException extends Exception{ public class ApiException extends Exception {
static ResponseEntity<String> handleExceptions(ApiException ex) {
switch (ex.getCode()) {
case 404:
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
case 409:
return new ResponseEntity<>(HttpStatus.CONFLICT);
default:
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* @return the code
*/
public int getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(int code) {
this.code = code;
}
private int code; private int code;
public ApiException (int code, String msg) {
public ApiException(int code, String msg) {
super(msg); super(msg);
this.code = code; this.code = code;
} }
......
...@@ -68,7 +68,7 @@ public class ToscaTemplateApiController implements ToscaTemplateApi { ...@@ -68,7 +68,7 @@ public class ToscaTemplateApiController implements ToscaTemplateApi {
if (accept != null && accept.contains("text/plain")) { if (accept != null && accept.contains("text/plain")) {
try { try {
String ymlStr = toscaTemplateService.findByID(id); String ymlStr = toscaTemplateService.findByID(id);
return new ResponseEntity<>(objectMapper.readValue(ymlStr, String.class), HttpStatus.OK); return new ResponseEntity<>(ymlStr, HttpStatus.OK);
} catch (IOException e) { } catch (IOException e) {
log.error("Couldn't serialize response for content type ", e); log.error("Couldn't serialize response for content type ", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
...@@ -79,32 +79,38 @@ public class ToscaTemplateApiController implements ToscaTemplateApi { ...@@ -79,32 +79,38 @@ public class ToscaTemplateApiController implements ToscaTemplateApi {
} }
@Override @Override
public ResponseEntity<String> updateToscaTemplateByID(@ApiParam(value = "ID of topolog template to return", required = true) @PathVariable("id") String id, @ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile file) { public ResponseEntity<String> updateToscaTemplateByID(@ApiParam(
value = "ID of topolog template to return", required = true)
@PathVariable("id") String id, @ApiParam(value = "file detail")
@Valid @RequestPart("file") MultipartFile file) {
String accept = request.getHeader("Accept"); String accept = request.getHeader("Accept");
if (accept != null && accept.contains("")) { if (accept != null && accept.contains("text/plain")) {
try { try {
id = toscaTemplateService.updateToscaTemplateByID(id); id = toscaTemplateService.updateToscaTemplateByID(id, file);
return new ResponseEntity<>(objectMapper.readValue(String.valueOf(id), String.class), HttpStatus.OK); return new ResponseEntity<>(id, HttpStatus.OK);
} catch (IOException e) { } catch (IOException e) {
log.error("Couldn't serialize response for content type ", e); log.error("Couldn't serialize response for content type ", e);
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} catch (ApiException ex) {
return ApiException.handleExceptions(ex);
} }
} }
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} }
@Override
public ResponseEntity<String> uploadToscaTemplate(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile file) { public ResponseEntity<String> uploadToscaTemplate(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile file) {
String accept = request.getHeader("Accept"); String accept = request.getHeader("Accept");
if (accept != null && accept.contains("*/*")) { if (accept != null && accept.contains("*/*")) {
try { try {
String id = toscaTemplateService.saveFile(file); String id = toscaTemplateService.saveFile(file);
return new ResponseEntity<>(String.valueOf(id), HttpStatus.OK); return new ResponseEntity<>(id, HttpStatus.OK);
} catch (IOException e) { } catch (IOException e) {
log.error("Couldn't serialize response for content type application/json", e); log.error("Couldn't serialize response for content type application/json", e);
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} catch (ApiException ex) { } catch (ApiException ex) {
return new ResponseEntity<String>(HttpStatus.CONFLICT); return ApiException.handleExceptions(ex);
} }
} }
......
package nl.uva.sne.drip.model; package nl.uva.sne.drip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects; import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.*;
/** /**
* Credentials * Credentials
*/ */
@Validated @Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z") @JsonInclude(Include.NON_NULL)
public class Credentials {
public class Credentials { @JsonProperty("protocol")
@JsonProperty("protocol") private String protocol = null;
private String protocol = null;
@JsonProperty("token_type") @JsonProperty("token_type")
private String tokenType = null; private String tokenType = null;
@JsonProperty("token") @JsonProperty("token")
private String token = null; private String token = null;
@JsonProperty("keys") @JsonProperty("keys")
@Valid @Valid
private Map<String, String> keys = null; private Map<String, String> keys = null;
@JsonProperty("user") @JsonProperty("user")
private String user = null; private String user = null;
@JsonProperty("cloud_provider_name") @JsonProperty("cloud_provider_name")
private String cloudProviderName = null; private String cloudProviderName = null;
public Credentials protocol(String protocol) { public Credentials protocol(String protocol) {
this.protocol = protocol; this.protocol = protocol;
return this; return this;
} }
/** /**
* Get protocol * Get protocol
* @return protocol *
**/ * @return protocol
@ApiModelProperty(value = "") *
*/
@ApiModelProperty(value = "")
public String getProtocol() {
return protocol;
}
public String getProtocol() { public void setProtocol(String protocol) {
return protocol; this.protocol = protocol;
} }
public void setProtocol(String protocol) { public Credentials tokenType(String tokenType) {
this.protocol = protocol; this.tokenType = tokenType;
} return this;
}
public Credentials tokenType(String tokenType) { /**
this.tokenType = tokenType; * Get tokenType
return this; *
} * @return tokenType
*
*/
@ApiModelProperty(value = "")
/** public String getTokenType() {
* Get tokenType return tokenType;
* @return tokenType }
**/
@ApiModelProperty(value = "")
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public String getTokenType() { public Credentials token(String token) {
return tokenType; this.token = token;
} return this;
}
public void setTokenType(String tokenType) { /**
this.tokenType = tokenType; * Get token
} *
* @return token
*
*/
@ApiModelProperty(value = "")
public Credentials token(String token) { public String getToken() {
this.token = token; return token;
return this; }
}
/** public void setToken(String token) {
* Get token this.token = token;
* @return token }
**/
@ApiModelProperty(value = "")
public Credentials keys(Map<String, String> keys) {
this.keys = keys;
return this;
}
public String getToken() { public Credentials putKeysItem(String key, String keysItem) {
return token; if (this.keys == null) {
} this.keys = new HashMap<String, String>();
}
this.keys.put(key, keysItem);
return this;
}
public void setToken(String token) { /**
this.token = token; * Get keys
} *
* @return keys
public Credentials keys(Map<String, String> keys) { *
this.keys = keys; */
return this; @ApiModelProperty(value = "")
}
public Map<String, String> getKeys() {
public Credentials putKeysItem(String key, String keysItem) { return keys;
if (this.keys == null) { }
this.keys = new HashMap<String, String>();
} public void setKeys(Map<String, String> keys) {
this.keys.put(key, keysItem); this.keys = keys;
return this; }
}
public Credentials user(String user) {
/** this.user = user;
* Get keys return this;
* @return keys }
**/
@ApiModelProperty(value = "") /**
* Get user
*
public Map<String, String> getKeys() { * @return user
return keys; *
} */
@ApiModelProperty(value = "")
public void setKeys(Map<String, String> keys) {
this.keys = keys;
}
public Credentials user(String user) {
this.user = user;
return this;
}
/**
* Get user
* @return user
**/
@ApiModelProperty(value = "")
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Credentials cloudProviderName(String cloudProviderName) {
this.cloudProviderName = cloudProviderName;
return this;
}
/**
* Get cloudProviderName
* @return cloudProviderName
**/
@ApiModelProperty(value = "")
public String getCloudProviderName() {
return cloudProviderName;
}
public void setCloudProviderName(String cloudProviderName) {
this.cloudProviderName = cloudProviderName;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Credentials credentials = (Credentials) o;
return Objects.equals(this.protocol, credentials.protocol) &&
Objects.equals(this.tokenType, credentials.tokenType) &&
Objects.equals(this.token, credentials.token) &&
Objects.equals(this.keys, credentials.keys) &&
Objects.equals(this.user, credentials.user) &&
Objects.equals(this.cloudProviderName, credentials.cloudProviderName);
}
@Override
public int hashCode() {
return Objects.hash(protocol, tokenType, token, keys, user, cloudProviderName);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Credentials {\n");
sb.append(" protocol: ").append(toIndentedString(protocol)).append("\n");
sb.append(" tokenType: ").append(toIndentedString(tokenType)).append("\n");
sb.append(" token: ").append(toIndentedString(token)).append("\n");
sb.append(" keys: ").append(toIndentedString(keys)).append("\n");
sb.append(" user: ").append(toIndentedString(user)).append("\n");
sb.append(" cloudProviderName: ").append(toIndentedString(cloudProviderName)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Credentials cloudProviderName(String cloudProviderName) {
this.cloudProviderName = cloudProviderName;
return this;
}
/**
* Get cloudProviderName
*
* @return cloudProviderName
*
*/
@ApiModelProperty(value = "")
public String getCloudProviderName() {
return cloudProviderName;
}
public void setCloudProviderName(String cloudProviderName) {
this.cloudProviderName = cloudProviderName;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Credentials credentials = (Credentials) o;
return Objects.equals(this.protocol, credentials.protocol)
&& Objects.equals(this.tokenType, credentials.tokenType)
&& Objects.equals(this.token, credentials.token)
&& Objects.equals(this.keys, credentials.keys)
&& Objects.equals(this.user, credentials.user)
&& Objects.equals(this.cloudProviderName, credentials.cloudProviderName);
}
@Override
public int hashCode() {
return Objects.hash(protocol, tokenType, token, keys, user, cloudProviderName);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Credentials {\n");
sb.append(" protocol: ").append(toIndentedString(protocol)).append("\n");
sb.append(" tokenType: ").append(toIndentedString(tokenType)).append("\n");
sb.append(" token: ").append(toIndentedString(token)).append("\n");
sb.append(" keys: ").append(toIndentedString(keys)).append("\n");
sb.append(" user: ").append(toIndentedString(user)).append("\n");
sb.append(" cloudProviderName: ").append(toIndentedString(cloudProviderName)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
package nl.uva.sne.drip.model; package nl.uva.sne.drip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects; import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -11,14 +11,12 @@ import java.util.List; ...@@ -11,14 +11,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.*;
/** /**
* NodeTemplate * NodeTemplate
*/ */
@Validated @Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z") @JsonInclude(Include.NON_NULL)
public class NodeTemplate { public class NodeTemplate {
@JsonProperty("name") @JsonProperty("name")
private String name = null; private String name = null;
......
package nl.uva.sne.drip.model; package nl.uva.sne.drip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects; import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -11,14 +11,12 @@ import java.util.List; ...@@ -11,14 +11,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.*;
/** /**
* TopologyTemplate * TopologyTemplate
*/ */
@Validated @Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z") @JsonInclude(Include.NON_NULL)
public class TopologyTemplate { public class TopologyTemplate {
@JsonProperty("description") @JsonProperty("description")
private String description = null; private String description = null;
......
...@@ -2,10 +2,9 @@ package nl.uva.sne.drip.model; ...@@ -2,10 +2,9 @@ package nl.uva.sne.drip.model;
import java.util.Objects; import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -13,13 +12,13 @@ import java.util.List; ...@@ -13,13 +12,13 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.*;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
/** /**
* ToscaTemplate * ToscaTemplate
*/ */
@Validated @Validated
@JsonInclude(Include.NON_NULL)
public class ToscaTemplate { public class ToscaTemplate {
@Id @Id
......
...@@ -8,224 +8,230 @@ import io.swagger.annotations.ApiModelProperty; ...@@ -8,224 +8,230 @@ import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.*; import javax.validation.constraints.*;
import org.springframework.data.annotation.Id;
/** /**
* User * User
*/ */
@Validated @Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z") public class User {
public class User { @JsonProperty("id")
@JsonProperty("id") @Id
private Long id = null; private Long id = null;
@JsonProperty("username") @JsonProperty("username")
private String username = null; private String username = null;
@JsonProperty("firstName") @JsonProperty("firstName")
private String firstName = null; private String firstName = null;
@JsonProperty("lastName") @JsonProperty("lastName")
private String lastName = null; private String lastName = null;
@JsonProperty("email") @JsonProperty("email")
private String email = null; private String email = null;
@JsonProperty("password") @JsonProperty("password")
private String password = null; private String password = null;
@JsonProperty("userStatus") @JsonProperty("userStatus")
private Integer userStatus = null; private Integer userStatus = null;
public User id(Long id) { public User id(Long id) {
this.id = id; this.id = id;
return this; return this;
} }
/**
* Get id
*
* @return id
*
*/
@ApiModelProperty(value = "")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/** public User username(String username) {
* Get id this.username = username;
* @return id return this;
**/ }
@ApiModelProperty(value = "")
/**
* Get username
*
* @return username
*
*/
@ApiModelProperty(value = "")
public Long getId() { public String getUsername() {
return id; return username;
} }
public void setId(Long id) { public void setUsername(String username) {
this.id = id; this.username = username;
} }
public User username(String username) { public User firstName(String firstName) {
this.username = username; this.firstName = firstName;
return this; return this;
} }
/** /**
* Get username * Get firstName
* @return username *
**/ * @return firstName
@ApiModelProperty(value = "") *
*/
@ApiModelProperty(value = "")
public String getFirstName() {
return firstName;
}
public String getUsername() { public void setFirstName(String firstName) {
return username; this.firstName = firstName;
} }
public void setUsername(String username) { public User lastName(String lastName) {
this.username = username; this.lastName = lastName;
} return this;
}
public User firstName(String firstName) { /**
this.firstName = firstName; * Get lastName
return this; *
} * @return lastName
*
*/
@ApiModelProperty(value = "")
/** public String getLastName() {
* Get firstName return lastName;
* @return firstName }
**/
@ApiModelProperty(value = "")
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() { public User email(String email) {
return firstName; this.email = email;
} return this;
}
public void setFirstName(String firstName) { /**
this.firstName = firstName; * Get email
} *
* @return email
*
*/
@ApiModelProperty(value = "")
public User lastName(String lastName) { public String getEmail() {
this.lastName = lastName; return email;
return this; }
}
/** public void setEmail(String email) {
* Get lastName this.email = email;
* @return lastName }
**/
@ApiModelProperty(value = "")
public User password(String password) {
this.password = password;
return this;
}
public String getLastName() { /**
return lastName; * Get password
} *
* @return password
public void setLastName(String lastName) { *
this.lastName = lastName; */
} @ApiModelProperty(value = "")
public User email(String email) { public String getPassword() {
this.email = email; return password;
return this; }
}
public void setPassword(String password) {
/** this.password = password;
* Get email }
* @return email
**/ public User userStatus(Integer userStatus) {
@ApiModelProperty(value = "") this.userStatus = userStatus;
return this;
}
public String getEmail() {
return email; /**
} * User Status
*
public void setEmail(String email) { * @return userStatus
this.email = email; *
} */
@ApiModelProperty(value = "User Status")
public User password(String password) {
this.password = password; public Integer getUserStatus() {
return this; return userStatus;
} }
/** public void setUserStatus(Integer userStatus) {
* Get password this.userStatus = userStatus;
* @return password }
**/
@ApiModelProperty(value = "") @Override
public boolean equals(java.lang.Object o) {
if (this == o) {
public String getPassword() { return true;
return password; }
} if (o == null || getClass() != o.getClass()) {
return false;
public void setPassword(String password) { }
this.password = password; User user = (User) o;
} return Objects.equals(this.id, user.id)
&& Objects.equals(this.username, user.username)
public User userStatus(Integer userStatus) { && Objects.equals(this.firstName, user.firstName)
this.userStatus = userStatus; && Objects.equals(this.lastName, user.lastName)
return this; && Objects.equals(this.email, user.email)
} && Objects.equals(this.password, user.password)
&& Objects.equals(this.userStatus, user.userStatus);
/** }
* User Status
* @return userStatus @Override
**/ public int hashCode() {
@ApiModelProperty(value = "User Status") return Objects.hash(id, username, firstName, lastName, email, password, userStatus);
}
public Integer getUserStatus() { @Override
return userStatus; public String toString() {
} StringBuilder sb = new StringBuilder();
sb.append("class User {\n");
public void setUserStatus(Integer userStatus) {
this.userStatus = userStatus; sb.append(" id: ").append(toIndentedString(id)).append("\n");
} sb.append(" username: ").append(toIndentedString(username)).append("\n");
sb.append(" firstName: ").append(toIndentedString(firstName)).append("\n");
sb.append(" lastName: ").append(toIndentedString(lastName)).append("\n");
@Override sb.append(" email: ").append(toIndentedString(email)).append("\n");
public boolean equals(java.lang.Object o) { sb.append(" password: ").append(toIndentedString(password)).append("\n");
if (this == o) { sb.append(" userStatus: ").append(toIndentedString(userStatus)).append("\n");
return true; sb.append("}");
} return sb.toString();
if (o == null || getClass() != o.getClass()) { }
return false;
} /**
User user = (User) o; * Convert the given object to string with each line indented by 4 spaces
return Objects.equals(this.id, user.id) && * (except the first line).
Objects.equals(this.username, user.username) && */
Objects.equals(this.firstName, user.firstName) && private String toIndentedString(java.lang.Object o) {
Objects.equals(this.lastName, user.lastName) && if (o == null) {
Objects.equals(this.email, user.email) && return "null";
Objects.equals(this.password, user.password) && }
Objects.equals(this.userStatus, user.userStatus); return o.toString().replace("\n", "\n ");
} }
}
@Override
public int hashCode() {
return Objects.hash(id, username, firstName, lastName, email, password, userStatus);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class User {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" username: ").append(toIndentedString(username)).append("\n");
sb.append(" firstName: ").append(toIndentedString(firstName)).append("\n");
sb.append(" lastName: ").append(toIndentedString(lastName)).append("\n");
sb.append(" email: ").append(toIndentedString(email)).append("\n");
sb.append(" password: ").append(toIndentedString(password)).append("\n");
sb.append(" userStatus: ").append(toIndentedString(userStatus)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
...@@ -54,19 +54,14 @@ public class ToscaTemplateService { ...@@ -54,19 +54,14 @@ public class ToscaTemplateService {
byte[] bytes = file.getBytes(); byte[] bytes = file.getBytes();
String ymlStr = new String(bytes, "UTF-8"); String ymlStr = new String(bytes, "UTF-8");
ToscaTemplate tt = objectMapper.readValue(ymlStr, ToscaTemplate.class); ToscaTemplate tt = objectMapper.readValue(ymlStr, ToscaTemplate.class);
Example<ToscaTemplate> templateExample = Example.of(tt);
Optional<ToscaTemplate> result = dao.findOne(templateExample);
if (result.equals(tt)) {
throw new ApiException(409, "Tosca Template already exists");
}
save(tt); save(tt);
return tt.getId(); return tt.getId();
} }
public String updateToscaTemplateByID(String id, MultipartFile file) throws IOException { public String updateToscaTemplateByID(String id, MultipartFile file) throws IOException, ApiException {
ToscaTemplate tt = dao.findById(id).get(); ToscaTemplate tt = dao.findById(id).get();
if (tt == null) { if (tt == null) {
throw new NullPointerException(); throw new ApiException(404, "Tosca Template with id :" + id + " not found");
} }
byte[] bytes = file.getBytes(); byte[] bytes = file.getBytes();
String ymlStr = new String(bytes, "UTF-8"); String ymlStr = new String(bytes, "UTF-8");
...@@ -75,14 +70,10 @@ public class ToscaTemplateService { ...@@ -75,14 +70,10 @@ public class ToscaTemplateService {
return save(tt); return save(tt);
} }
public String updateToscaTemplateByID(String id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String findByID(String id) throws JsonProcessingException { public String findByID(String id) throws JsonProcessingException {
ToscaTemplate tt = dao.findById(id).get(); ToscaTemplate tt = dao.findById(id).get();
String ymlStr = objectMapper.writeValueAsString(tt);
return objectMapper.writeValueAsString(tt); return ymlStr;
} }
public void deleteByID(String id) { public void deleteByID(String id) {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package nl.uva.sne.drip.service; package nl.uva.sne.drip.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -26,15 +27,26 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; ...@@ -26,15 +27,26 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net; import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version; import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network; import de.flapdoodle.embed.process.runtime.Network;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import nl.uva.sne.drip.Swagger2SpringBoot; import nl.uva.sne.drip.Swagger2SpringBoot;
import nl.uva.sne.drip.api.ApiException;
import nl.uva.sne.drip.model.ToscaTemplate;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
...@@ -42,7 +54,7 @@ import org.springframework.web.context.WebApplicationContext; ...@@ -42,7 +54,7 @@ import org.springframework.web.context.WebApplicationContext;
public class ToscaTemplateServiceTest { public class ToscaTemplateServiceTest {
@Autowired @Autowired
ToscaTemplateService ml; ToscaTemplateService instance;
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
...@@ -51,6 +63,8 @@ public class ToscaTemplateServiceTest { ...@@ -51,6 +63,8 @@ public class ToscaTemplateServiceTest {
private static final MongodStarter starter = MongodStarter.getDefaultInstance(); private static final MongodStarter starter = MongodStarter.getDefaultInstance();
private static MongodExecutable _mongodExe; private static MongodExecutable _mongodExe;
private static MongodProcess _mongod; private static MongodProcess _mongod;
private static final String testApplicationExampleToscaFilePath = ".." + File.separator + "TOSCA" + File.separator + "application_example.yaml";
private static final String testUpdatedApplicationExampleToscaFilePath = ".." + File.separator + "TOSCA" + File.separator + "application_example_updated.yaml";
@BeforeClass @BeforeClass
public static void setUpClass() { public static void setUpClass() {
...@@ -72,6 +86,8 @@ public class ToscaTemplateServiceTest { ...@@ -72,6 +86,8 @@ public class ToscaTemplateServiceTest {
_mongodExe.stop(); _mongodExe.stop();
} }
private String toscaTemplateID;
private String testApplicationExampleToscaContents;
@Before @Before
public void setUp() { public void setUp() {
...@@ -83,14 +99,127 @@ public class ToscaTemplateServiceTest { ...@@ -83,14 +99,127 @@ public class ToscaTemplateServiceTest {
public void tearDown() { public void tearDown() {
} }
/**
* Test of saveFile method, of class ToscaTemplateService.
*/
@Test @Test
public void test_ml_always_return_true() { public void testSaveFile() throws Exception {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.INFO, "saveFile");
FileInputStream in = new FileInputStream(testApplicationExampleToscaFilePath);
MultipartFile file = new MockMultipartFile("file", in);
toscaTemplateID = instance.saveFile(file);
Assert.assertNotNull(toscaTemplateID);
testApplicationExampleToscaContents = instance.findByID(toscaTemplateID);
Assert.assertNotNull(testApplicationExampleToscaContents);
}
//assert correct type/impl /**
assertThat(ml, instanceOf(ToscaTemplateService.class)); * Test of updateToscaTemplateByID method, of class ToscaTemplateService.
*/
@Test
public void testUpdateToscaTemplateByID_String_MultipartFile() {
FileInputStream in = null;
try {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.INFO, "updateToscaTemplateByID");
if (toscaTemplateID == null) {
testSaveFile();
}
in = new FileInputStream(testUpdatedApplicationExampleToscaFilePath);
MultipartFile file = new MockMultipartFile("file", in);
String expResult = toscaTemplateID;
String result = instance.updateToscaTemplateByID(toscaTemplateID, file);
assertEquals(expResult, result);
String updatedTemplate = instance.findByID(result);
Assert.assertNotNull(updatedTemplate);
Assert.assertNotEquals(result, testApplicationExampleToscaContents);
} catch (FileNotFoundException ex) {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Test of updateToscaTemplateByID method, of class ToscaTemplateService.
*/
@Test
public void testUpdateToscaTemplateByID_Exception_MultipartFile() {
FileInputStream in = null;
try {
in = new FileInputStream(testUpdatedApplicationExampleToscaFilePath);
MultipartFile file = new MockMultipartFile("file", in);
String result = instance.updateToscaTemplateByID("0", file);
} catch (FileNotFoundException ex) {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | ApiException ex) {
if (!(ex instanceof NoSuchElementException)) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
try {
in.close();
} catch (IOException ex) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//assert true /**
assertThat(true, is(true)); * Test of findByID method, of class ToscaTemplateService.
*/
@Test
public void testFindByID() {
try {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.INFO, "findByID");
if (toscaTemplateID == null) {
testSaveFile();
}
String result = instance.findByID(toscaTemplateID);
Assert.assertNotNull(result);
assertEquals(testApplicationExampleToscaContents, result);
} catch (JsonProcessingException ex) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Test of deleteByID method, of class ToscaTemplateService.
*/
@Test
public void testDeleteByID() {
try {
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.INFO, "deleteByID");
if (toscaTemplateID == null) {
testSaveFile();
}
instance.deleteByID(toscaTemplateID);
String id = instance.findByID(toscaTemplateID);
} catch (Exception ex) {
if (!(ex instanceof NoSuchElementException)) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
try {
testSaveFile();
} catch (Exception ex) {
fail(ex.getMessage());
Logger.getLogger(ToscaTemplateServiceTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment