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;
...@@ -45,11 +43,12 @@ public class Credentials { ...@@ -45,11 +43,12 @@ public class Credentials {
/** /**
* Get protocol * Get protocol
*
* @return protocol * @return protocol
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getProtocol() { public String getProtocol() {
return protocol; return protocol;
} }
...@@ -65,11 +64,12 @@ public class Credentials { ...@@ -65,11 +64,12 @@ public class Credentials {
/** /**
* Get tokenType * Get tokenType
*
* @return tokenType * @return tokenType
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getTokenType() { public String getTokenType() {
return tokenType; return tokenType;
} }
...@@ -85,11 +85,12 @@ public class Credentials { ...@@ -85,11 +85,12 @@ public class Credentials {
/** /**
* Get token * Get token
*
* @return token * @return token
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getToken() { public String getToken() {
return token; return token;
} }
...@@ -113,11 +114,12 @@ public class Credentials { ...@@ -113,11 +114,12 @@ public class Credentials {
/** /**
* Get keys * Get keys
*
* @return keys * @return keys
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public Map<String, String> getKeys() { public Map<String, String> getKeys() {
return keys; return keys;
} }
...@@ -133,11 +135,12 @@ public class Credentials { ...@@ -133,11 +135,12 @@ public class Credentials {
/** /**
* Get user * Get user
*
* @return user * @return user
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getUser() { public String getUser() {
return user; return user;
} }
...@@ -153,11 +156,12 @@ public class Credentials { ...@@ -153,11 +156,12 @@ public class Credentials {
/** /**
* Get cloudProviderName * Get cloudProviderName
*
* @return cloudProviderName * @return cloudProviderName
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getCloudProviderName() { public String getCloudProviderName() {
return cloudProviderName; return cloudProviderName;
} }
...@@ -166,7 +170,6 @@ public class Credentials { ...@@ -166,7 +170,6 @@ public class Credentials {
this.cloudProviderName = cloudProviderName; this.cloudProviderName = cloudProviderName;
} }
@Override @Override
public boolean equals(java.lang.Object o) { public boolean equals(java.lang.Object o) {
if (this == o) { if (this == o) {
...@@ -176,12 +179,12 @@ public class Credentials { ...@@ -176,12 +179,12 @@ public class Credentials {
return false; return false;
} }
Credentials credentials = (Credentials) o; Credentials credentials = (Credentials) o;
return Objects.equals(this.protocol, credentials.protocol) && return Objects.equals(this.protocol, credentials.protocol)
Objects.equals(this.tokenType, credentials.tokenType) && && Objects.equals(this.tokenType, credentials.tokenType)
Objects.equals(this.token, credentials.token) && && Objects.equals(this.token, credentials.token)
Objects.equals(this.keys, credentials.keys) && && Objects.equals(this.keys, credentials.keys)
Objects.equals(this.user, credentials.user) && && Objects.equals(this.user, credentials.user)
Objects.equals(this.cloudProviderName, credentials.cloudProviderName); && Objects.equals(this.cloudProviderName, credentials.cloudProviderName);
} }
@Override @Override
...@@ -215,4 +218,3 @@ public class Credentials { ...@@ -215,4 +218,3 @@ public class Credentials {
return o.toString().replace("\n", "\n "); 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,15 +8,16 @@ import io.swagger.annotations.ApiModelProperty; ...@@ -8,15 +8,16 @@ 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")
...@@ -44,11 +45,12 @@ public class User { ...@@ -44,11 +45,12 @@ public class User {
/** /**
* Get id * Get id
*
* @return id * @return id
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public Long getId() { public Long getId() {
return id; return id;
} }
...@@ -64,11 +66,12 @@ public class User { ...@@ -64,11 +66,12 @@ public class User {
/** /**
* Get username * Get username
*
* @return username * @return username
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getUsername() { public String getUsername() {
return username; return username;
} }
...@@ -84,11 +87,12 @@ public class User { ...@@ -84,11 +87,12 @@ public class User {
/** /**
* Get firstName * Get firstName
*
* @return firstName * @return firstName
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }
...@@ -104,11 +108,12 @@ public class User { ...@@ -104,11 +108,12 @@ public class User {
/** /**
* Get lastName * Get lastName
*
* @return lastName * @return lastName
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getLastName() { public String getLastName() {
return lastName; return lastName;
} }
...@@ -124,11 +129,12 @@ public class User { ...@@ -124,11 +129,12 @@ public class User {
/** /**
* Get email * Get email
*
* @return email * @return email
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getEmail() { public String getEmail() {
return email; return email;
} }
...@@ -144,11 +150,12 @@ public class User { ...@@ -144,11 +150,12 @@ public class User {
/** /**
* Get password * Get password
*
* @return password * @return password
**/ *
*/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
public String getPassword() { public String getPassword() {
return password; return password;
} }
...@@ -164,11 +171,12 @@ public class User { ...@@ -164,11 +171,12 @@ public class User {
/** /**
* User Status * User Status
*
* @return userStatus * @return userStatus
**/ *
*/
@ApiModelProperty(value = "User Status") @ApiModelProperty(value = "User Status")
public Integer getUserStatus() { public Integer getUserStatus() {
return userStatus; return userStatus;
} }
...@@ -177,7 +185,6 @@ public class User { ...@@ -177,7 +185,6 @@ public class User {
this.userStatus = userStatus; this.userStatus = userStatus;
} }
@Override @Override
public boolean equals(java.lang.Object o) { public boolean equals(java.lang.Object o) {
if (this == o) { if (this == o) {
...@@ -187,13 +194,13 @@ public class User { ...@@ -187,13 +194,13 @@ public class User {
return false; return false;
} }
User user = (User) o; User user = (User) o;
return Objects.equals(this.id, user.id) && return Objects.equals(this.id, user.id)
Objects.equals(this.username, user.username) && && Objects.equals(this.username, user.username)
Objects.equals(this.firstName, user.firstName) && && Objects.equals(this.firstName, user.firstName)
Objects.equals(this.lastName, user.lastName) && && Objects.equals(this.lastName, user.lastName)
Objects.equals(this.email, user.email) && && Objects.equals(this.email, user.email)
Objects.equals(this.password, user.password) && && Objects.equals(this.password, user.password)
Objects.equals(this.userStatus, user.userStatus); && Objects.equals(this.userStatus, user.userStatus);
} }
@Override @Override
...@@ -228,4 +235,3 @@ public class User { ...@@ -228,4 +235,3 @@ public class User {
return o.toString().replace("\n", "\n "); 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
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);
}
/**
* Test of updateToscaTemplateByID method, of class ToscaTemplateService.
*/
@Test @Test
public void test_ml_always_return_true() { 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 correct type/impl /**
assertThat(ml, instanceOf(ToscaTemplateService.class)); * 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);
}
}
//assert true /**
assertThat(true, is(true)); * 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