Restapi УДАЛИТЬ метод не работает и отправить статус 200 - PullRequest
1 голос
/ 07 марта 2019

Я делаю свой первый веб-сервис RESTful (использующий java). Публикация и проверка, если сообщение существует, работает хорошо, но мой метод DELETE не работает, и я не могу проверить, хорошо ли работает checkIfPostWasDeleted, я попытаюсь добавить bodyrequest в postComment.deleteComment

Редактировать: если я отлаживаю и оцениваю client.deleteComment(headermap,id); его работу

StepDefs:

@When("i try to add a comment")
    public void postComment() {
        Map<String, Object> headermap = new HashMap<>();
        headermap.put("Content-Type", "application/json");
        CommentBody requestComment = new CommentBody();
        CommentRes commentRes = postComment.postComment(headermap, requestComment.RequestCommentBody());
        id = commentRes.getId();
        LOGGER.info("Created: " + DateFormat.getInstance().format(now));
    }

    @When("i again check if comment exist")
    public void checkCommentAgain() throws IOException {
        final URL url = new URL(("http://localhost:3000/comments/" + id));
        HttpURLConnection huc = (HttpURLConnection) url.openConnection();
        int responseCode = huc.getResponseCode();
        if (responseCode == 200) {
            System.out.println("comment exist");
        } else {
            Assert.fail("comment doesn't exist");
        }
    }

    @When("when i try to delete comment")
    public void deleteComment() {
        Map<String, Object> headermap = new HashMap<>();
        headermap.put("Content-Type", "application/json");
        CommentBody requestComment = new CommentBody();
        CommentRes commentRes = postComment.deleteComment(headermap,id);
    }


   @When("i check if comment was deleted")
    public void checkIfPostWasDeleted()throws IOException{
        final URL url = new URL(("http://localhost:3000/comments/" + id));
        HttpURLConnection huc = (HttpURLConnection) url.openConnection();
        int responseCode = huc.getResponseCode();
        if (responseCode == 404) {
            System.out.println("deleted");
        } else {
            Assert.fail("not deleted");
        }

    }

Интерфейс:

public interface Interface {
    @RequestLine("POST /")
    CommentRes postComment(@HeaderMap Map<String, Object> headerMap, CommentRq commentRes);
    @RequestLine("DELETE /{id}")
    CommentRes deleteComment (@HeaderMap Map<String, Object> headerMap, @Param("id")String id) ;
    @RequestLine("PUT /{id}")
    CommentRes updateComment(@HeaderMap Map<String, Object> headerMap, CommentRes commentRes, @Param("id")String id);
    @RequestLine("GET /{id}")
    CommentRes getCommentById(@HeaderMap Map<String, Object> headerMap, @Param("id")String id);

}

CommentRes:

public class CommentRes {
    @JsonUnwrapped
    private CommentModel commentModel;
    @JsonProperty("id")
    String id;
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class CommentModel {
        @JsonProperty("comment")
        String resourceName;

        @JsonProperty("date")
        String resourceNamed;


    }
}

CommentBody: import com.fasterxml.jackson.annotation.JsonInclude;import lombok. *;

import java.text.DateFormat;
import java.util.Date;
public class CommentBody {

    Date now = new Date();
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public CommentRq RequestCommentBody() {
        return CommentRq.builder()
                .commentModel(new CommentRq.CommentModel(
                        "komentarz",
                        (DateFormat.getInstance().format(now))

                ))
                .build();
    }
}

CommentRq:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class CommentRq {



    @JsonUnwrapped
    private CommentModel commentModel;

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Builder
    public static class CommentModel {
        @JsonProperty("comment")
        String resourceName;

        @JsonProperty("date")
        String resourceNamed;

    }
}
...