Skip to content

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

@SaharMehrpour

Description

@SaharMehrpour
  • 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\" )"
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions