Commit a5d45408 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added handleExceptions

parent c98d954e
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;
public ApiException (int code, String msg) {
public ApiException(int code, String msg) {
super(msg);
this.code = code;
}
......
......@@ -68,7 +68,7 @@ public class ToscaTemplateApiController implements ToscaTemplateApi {
if (accept != null && accept.contains("text/plain")) {
try {
String ymlStr = toscaTemplateService.findByID(id);
return new ResponseEntity<>(objectMapper.readValue(ymlStr, String.class), HttpStatus.OK);
return new ResponseEntity<>(ymlStr, HttpStatus.OK);
} catch (IOException e) {
log.error("Couldn't serialize response for content type ", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
......@@ -79,32 +79,38 @@ public class ToscaTemplateApiController implements ToscaTemplateApi {
}
@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");
if (accept != null && accept.contains("")) {
if (accept != null && accept.contains("text/plain")) {
try {
id = toscaTemplateService.updateToscaTemplateByID(id);
return new ResponseEntity<>(objectMapper.readValue(String.valueOf(id), String.class), HttpStatus.OK);
id = toscaTemplateService.updateToscaTemplateByID(id, file);
return new ResponseEntity<>(id, HttpStatus.OK);
} catch (IOException 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) {
String accept = request.getHeader("Accept");
if (accept != null && accept.contains("*/*")) {
try {
String id = toscaTemplateService.saveFile(file);
return new ResponseEntity<>(String.valueOf(id), HttpStatus.OK);
return new ResponseEntity<>(id, HttpStatus.OK);
} catch (IOException 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) {
return new ResponseEntity<String>(HttpStatus.CONFLICT);
return ApiException.handleExceptions(ex);
}
}
......
package nl.uva.sne.drip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* Credentials
*/
@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")
private String protocol = null;
@JsonProperty("protocol")
private String protocol = null;
@JsonProperty("token_type")
private String tokenType = null;
@JsonProperty("token_type")
private String tokenType = null;
@JsonProperty("token")
private String token = null;
@JsonProperty("token")
private String token = null;
@JsonProperty("keys")
@Valid
private Map<String, String> keys = null;
@JsonProperty("keys")
@Valid
private Map<String, String> keys = null;
@JsonProperty("user")
private String user = null;
@JsonProperty("user")
private String user = null;
@JsonProperty("cloud_provider_name")
private String cloudProviderName = null;
@JsonProperty("cloud_provider_name")
private String cloudProviderName = null;
public Credentials protocol(String protocol) {
this.protocol = protocol;
return this;
}
public Credentials protocol(String protocol) {
this.protocol = protocol;
return this;
}
/**
* Get protocol
* @return protocol
**/
@ApiModelProperty(value = "")
/**
* Get protocol
*
* @return protocol
*
*/
@ApiModelProperty(value = "")
public String getProtocol() {
return protocol;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public Credentials tokenType(String tokenType) {
this.tokenType = tokenType;
return this;
}
public Credentials tokenType(String tokenType) {
this.tokenType = tokenType;
return this;
}
/**
* Get tokenType
*
* @return tokenType
*
*/
@ApiModelProperty(value = "")
/**
* Get tokenType
* @return tokenType
**/
@ApiModelProperty(value = "")
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public String getTokenType() {
return tokenType;
}
public Credentials token(String token) {
this.token = token;
return this;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
/**
* Get token
*
* @return token
*
*/
@ApiModelProperty(value = "")
public Credentials token(String token) {
this.token = token;
return this;
}
public String getToken() {
return token;
}
/**
* Get token
* @return token
**/
@ApiModelProperty(value = "")
public void setToken(String token) {
this.token = token;
}
public Credentials keys(Map<String, String> keys) {
this.keys = keys;
return this;
}
public String getToken() {
return token;
}
public Credentials putKeysItem(String key, String keysItem) {
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;
}
public Credentials keys(Map<String, String> keys) {
this.keys = keys;
return this;
}
public Credentials putKeysItem(String key, String keysItem) {
if (this.keys == null) {
this.keys = new HashMap<String, String>();
}
this.keys.put(key, keysItem);
return this;
}
/**
* Get keys
* @return keys
**/
@ApiModelProperty(value = "")
public Map<String, String> getKeys() {
return keys;
}
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 ");
}
}
/**
* Get keys
*
* @return keys
*
*/
@ApiModelProperty(value = "")
public Map<String, String> getKeys() {
return keys;
}
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 ");
}
}
package nl.uva.sne.drip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -11,14 +11,12 @@ import java.util.List;
import java.util.Map;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* NodeTemplate
*/
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z")
@JsonInclude(Include.NON_NULL)
public class NodeTemplate {
@JsonProperty("name")
private String name = null;
......
package nl.uva.sne.drip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -11,14 +11,12 @@ import java.util.List;
import java.util.Map;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* TopologyTemplate
*/
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-10T17:15:46.465Z")
@JsonInclude(Include.NON_NULL)
public class TopologyTemplate {
@JsonProperty("description")
private String description = null;
......
......@@ -2,10 +2,9 @@ package nl.uva.sne.drip.model;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -13,13 +12,13 @@ import java.util.List;
import java.util.Map;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
import org.springframework.data.annotation.Id;
/**
* ToscaTemplate
*/
@Validated
@JsonInclude(Include.NON_NULL)
public class ToscaTemplate {
@Id
......
......@@ -8,224 +8,230 @@ import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
import org.springframework.data.annotation.Id;
/**
* User
*/
@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")
private Long id = null;
@JsonProperty("id")
@Id
private Long id = null;
@JsonProperty("username")
private String username = null;
@JsonProperty("username")
private String username = null;
@JsonProperty("firstName")
private String firstName = null;
@JsonProperty("firstName")
private String firstName = null;
@JsonProperty("lastName")
private String lastName = null;
@JsonProperty("lastName")
private String lastName = null;
@JsonProperty("email")
private String email = null;
@JsonProperty("email")
private String email = null;
@JsonProperty("password")
private String password = null;
@JsonProperty("password")
private String password = null;
@JsonProperty("userStatus")
private Integer userStatus = null;
@JsonProperty("userStatus")
private Integer userStatus = null;
public User id(Long id) {
this.id = id;
return this;
}
public User id(Long id) {
this.id = id;
return this;
}
/**
* Get id
*
* @return id
*
*/
@ApiModelProperty(value = "")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* Get id
* @return id
**/
@ApiModelProperty(value = "")
public User username(String username) {
this.username = username;
return this;
}
/**
* Get username
*
* @return username
*
*/
@ApiModelProperty(value = "")
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public User username(String username) {
this.username = username;
return this;
}
public User firstName(String firstName) {
this.firstName = firstName;
return this;
}
/**
* Get username
* @return username
**/
@ApiModelProperty(value = "")
/**
* Get firstName
*
* @return firstName
*
*/
@ApiModelProperty(value = "")
public String getFirstName() {
return firstName;
}
public String getUsername() {
return username;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setUsername(String username) {
this.username = username;
}
public User lastName(String lastName) {
this.lastName = lastName;
return this;
}
public User firstName(String firstName) {
this.firstName = firstName;
return this;
}
/**
* Get lastName
*
* @return lastName
*
*/
@ApiModelProperty(value = "")
/**
* Get firstName
* @return firstName
**/
@ApiModelProperty(value = "")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public User email(String email) {
this.email = email;
return this;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Get email
*
* @return email
*
*/
@ApiModelProperty(value = "")
public User lastName(String lastName) {
this.lastName = lastName;
return this;
}
public String getEmail() {
return email;
}
/**
* Get lastName
* @return lastName
**/
@ApiModelProperty(value = "")
public void setEmail(String email) {
this.email = email;
}
public User password(String password) {
this.password = password;
return this;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public User email(String email) {
this.email = email;
return this;
}
/**
* Get email
* @return email
**/
@ApiModelProperty(value = "")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User password(String password) {
this.password = password;
return this;
}
/**
* Get password
* @return password
**/
@ApiModelProperty(value = "")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User userStatus(Integer userStatus) {
this.userStatus = userStatus;
return this;
}
/**
* User Status
* @return userStatus
**/
@ApiModelProperty(value = "User Status")
public Integer getUserStatus() {
return userStatus;
}
public void setUserStatus(Integer userStatus) {
this.userStatus = userStatus;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return Objects.equals(this.id, user.id) &&
Objects.equals(this.username, user.username) &&
Objects.equals(this.firstName, user.firstName) &&
Objects.equals(this.lastName, user.lastName) &&
Objects.equals(this.email, user.email) &&
Objects.equals(this.password, user.password) &&
Objects.equals(this.userStatus, user.userStatus);
}
@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 ");
}
}
/**
* Get password
*
* @return password
*
*/
@ApiModelProperty(value = "")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User userStatus(Integer userStatus) {
this.userStatus = userStatus;
return this;
}
/**
* User Status
*
* @return userStatus
*
*/
@ApiModelProperty(value = "User Status")
public Integer getUserStatus() {
return userStatus;
}
public void setUserStatus(Integer userStatus) {
this.userStatus = userStatus;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return Objects.equals(this.id, user.id)
&& Objects.equals(this.username, user.username)
&& Objects.equals(this.firstName, user.firstName)
&& Objects.equals(this.lastName, user.lastName)
&& Objects.equals(this.email, user.email)
&& Objects.equals(this.password, user.password)
&& Objects.equals(this.userStatus, user.userStatus);
}
@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 {
byte[] bytes = file.getBytes();
String ymlStr = new String(bytes, "UTF-8");
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);
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();
if (tt == null) {
throw new NullPointerException();
throw new ApiException(404, "Tosca Template with id :" + id + " not found");
}
byte[] bytes = file.getBytes();
String ymlStr = new String(bytes, "UTF-8");
......@@ -75,14 +70,10 @@ public class ToscaTemplateService {
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 {
ToscaTemplate tt = dao.findById(id).get();
return objectMapper.writeValueAsString(tt);
String ymlStr = objectMapper.writeValueAsString(tt);
return ymlStr;
}
public void deleteByID(String id) {
......
......@@ -5,6 +5,7 @@
*/
package nl.uva.sne.drip.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -26,15 +27,26 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
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.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
......@@ -42,7 +54,7 @@ import org.springframework.web.context.WebApplicationContext;
public class ToscaTemplateServiceTest {
@Autowired
ToscaTemplateService ml;
ToscaTemplateService instance;
@Autowired
private WebApplicationContext wac;
......@@ -51,6 +63,8 @@ public class ToscaTemplateServiceTest {
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
private static MongodExecutable _mongodExe;
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
public static void setUpClass() {
......@@ -72,6 +86,8 @@ public class ToscaTemplateServiceTest {
_mongodExe.stop();
}
private String toscaTemplateID;
private String testApplicationExampleToscaContents;
@Before
public void setUp() {
......@@ -83,14 +99,127 @@ public class ToscaTemplateServiceTest {
public void tearDown() {
}
/**
* Test of saveFile method, of class ToscaTemplateService.
*/
@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