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 {
@JsonProperty("protocol")
private String protocol = null;
......@@ -45,11 +43,12 @@ public class Credentials {
/**
* Get protocol
*
* @return protocol
**/
*
*/
@ApiModelProperty(value = "")
public String getProtocol() {
return protocol;
}
......@@ -65,11 +64,12 @@ public class Credentials {
/**
* Get tokenType
*
* @return tokenType
**/
*
*/
@ApiModelProperty(value = "")
public String getTokenType() {
return tokenType;
}
......@@ -85,11 +85,12 @@ public class Credentials {
/**
* Get token
*
* @return token
**/
*
*/
@ApiModelProperty(value = "")
public String getToken() {
return token;
}
......@@ -113,11 +114,12 @@ public class Credentials {
/**
* Get keys
*
* @return keys
**/
*
*/
@ApiModelProperty(value = "")
public Map<String, String> getKeys() {
return keys;
}
......@@ -133,11 +135,12 @@ public class Credentials {
/**
* Get user
*
* @return user
**/
*
*/
@ApiModelProperty(value = "")
public String getUser() {
return user;
}
......@@ -153,11 +156,12 @@ public class Credentials {
/**
* Get cloudProviderName
*
* @return cloudProviderName
**/
*
*/
@ApiModelProperty(value = "")
public String getCloudProviderName() {
return cloudProviderName;
}
......@@ -166,7 +170,6 @@ public class Credentials {
this.cloudProviderName = cloudProviderName;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
......@@ -176,12 +179,12 @@ public class Credentials {
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);
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
......@@ -215,4 +218,3 @@ public class Credentials {
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,15 +8,16 @@ 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 {
@JsonProperty("id")
@Id
private Long id = null;
@JsonProperty("username")
......@@ -44,11 +45,12 @@ public class User {
/**
* Get id
*
* @return id
**/
*
*/
@ApiModelProperty(value = "")
public Long getId() {
return id;
}
......@@ -64,11 +66,12 @@ public class User {
/**
* Get username
*
* @return username
**/
*
*/
@ApiModelProperty(value = "")
public String getUsername() {
return username;
}
......@@ -84,11 +87,12 @@ public class User {
/**
* Get firstName
*
* @return firstName
**/
*
*/
@ApiModelProperty(value = "")
public String getFirstName() {
return firstName;
}
......@@ -104,11 +108,12 @@ public class User {
/**
* Get lastName
*
* @return lastName
**/
*
*/
@ApiModelProperty(value = "")
public String getLastName() {
return lastName;
}
......@@ -124,11 +129,12 @@ public class User {
/**
* Get email
*
* @return email
**/
*
*/
@ApiModelProperty(value = "")
public String getEmail() {
return email;
}
......@@ -144,11 +150,12 @@ public class User {
/**
* Get password
*
* @return password
**/
*
*/
@ApiModelProperty(value = "")
public String getPassword() {
return password;
}
......@@ -164,11 +171,12 @@ public class User {
/**
* User Status
*
* @return userStatus
**/
*
*/
@ApiModelProperty(value = "User Status")
public Integer getUserStatus() {
return userStatus;
}
......@@ -177,7 +185,6 @@ public class User {
this.userStatus = userStatus;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
......@@ -187,13 +194,13 @@ public class User {
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);
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
......@@ -228,4 +235,3 @@ public class User {
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 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
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