Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RulePad] Incorrect violations are produced for a design rule. #28

Open
SaharMehrpour opened this issue Aug 28, 2024 · 0 comments
Open
Labels
bug Something isn't working
Milestone

Comments

@SaharMehrpour
Copy link
Collaborator

  • RulePad:
    Screenshot 2024-08-28 at 12 29 09 PM
    Screenshot 2024-08-28 at 12 29 51 PM

  • Code:

package io.hexlet.typoreporter.controller;

import io.hexlet.typoreporter.domain.typo.TypoEvent;
import io.hexlet.typoreporter.service.TypoService;
import io.hexlet.typoreporter.service.WorkspaceService;
import io.hexlet.typoreporter.handler.exception.TypoNotFoundException;
import io.hexlet.typoreporter.handler.exception.WorkspaceNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Optional;

//TODO add tests
@Slf4j
@Controller
@RequestMapping("/typos")
@RequiredArgsConstructor
public class TypoController {

    private final TypoService typoService;

    private final WorkspaceService workspaceService;

    @PatchMapping("/{id}/status")
    public String updateTypoStatus(@PathVariable Long id,
                                   @RequestParam Optional<Long> wksId,
                                   @RequestParam Optional<TypoEvent> event,
                                   @RequestParam Optional<String> next) {
        if (wksId.isEmpty() || !workspaceService.existsWorkspaceById(wksId.get())) {
            //TODO send to error page
            final var e = new WorkspaceNotFoundException(wksId.orElse(0L));
            log.error(e.toString(), e);
            return "redirect:/workspaces";
        }
        if (event.isEmpty()) {
            //TODO send to error page
            log.error("TypoEvent={} must not be null", event.orElse(null));
            return ("redirect:/workspace/") + wksId.get() + next.orElse("/typos");
        }
        final var updatedTypo = typoService.updateTypoStatus(id, event.get());
        if (updatedTypo.isEmpty()) {
            //TODO send to error page
            final var e = new TypoNotFoundException(id);
            log.error(e.getMessage(), e);
        }
        return ("redirect:/workspace/") + wksId.get() + next.orElse("/typos");
    }

}
  • Authored DesignRule:
[
  {
    "index": "1724830895",
    "title": "Controller class must have a service field",
    "description": "Controller class must have service class field to handle business logic processing",
    "tags": [],
    "checkForFilesFoldersConstraints": "INCLUDE",
    "checkForFilesFolders": [
      "hexlet-correction/src/main/java/io/hexlet/typoreporter/controller"
    ],
    "processFilesFolders": "WITHIN",
    "quantifierXPathQuery": [
      "src:unit/src:class"
    ],
    "constraintXPathQuery": [
      "src:unit/src:class[src:annotation/src:name[text()=\"Controller\"] and src:specifier[text()=\"public\"] and src:name[substring(text(),string-length(text())-string-length(\"Controller\")+1)=\"Controller\"] and descendant-or-self::src:decl_stmt/src:decl[(src:specifier[text()=\"private\"] and src:specifier[text()=\"final\"] and src:name[substring(text(),string-length(text())-string-length(\"Service\")+1)=\"Service\"])]]"
    ],
    "grammar": "class with declaration statement with ( visibility \"private\" and specifier \"final\" and name \"...Service\" ) must have annotation \"Controller\" and visibility \"public\" and name \"...Controller\" and declaration statement with ( visibility \"private\" and specifier \"final\" and name \"...Service\" )"
  }
]
@SaharMehrpour SaharMehrpour added the bug Something isn't working label Aug 28, 2024
@SaharMehrpour SaharMehrpour added this to the v0.0.2 milestone Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant