Recipe Name:
Use AssertJ's contains() instead of hasItem()
Description:
Can be replaced with AssertJ style assertions
Level:
warning
Language:
  • java
Tags:
  • AssertJ
  • Hamcrest
Recipe
id: scw:assertj:hamcrest-has-item
version: 10
metadata:
  name: Use AssertJ's contains() instead of hasItem()
  shortDescription: Can be replaced with AssertJ style assertions
  level: warning
  language: java
  enabled: true
  tags: AssertJ; Hamcrest
search:
  methodcall:
    name: assertThat
    anyOf:
    - args:
        2:
          value:
            is:
              methodcall:
                args:
                  1:
                    anyOf:
                    - not:
                        value:
                          is:
                            methodcall: {}
                    - value:
                        is:
                          methodcall:
                            anyOf:
                            - name: equalTo
                              type: org.hamcrest.Matchers
                            - name: equalTo
                              type: org.hamcrest.CoreMatchers
                            - name: equalTo
                              type: org.hamcrest.core.IsEqual
                            - args:
                                1:
                                  value:
                                    not:
                                      is:
                                        methodcall: {}
                              name: is
                              type: org.hamcrest.Matchers
                            - args:
                                1:
                                  value:
                                    not:
                                      is:
                                        methodcall: {}
                              name: is
                              type: org.hamcrest.CoreMatchers
                            - args:
                                1:
                                  value:
                                    not:
                                      is:
                                        methodcall: {}
                              name: is
                              type: org.hamcrest.core.Is
                argCount: 1
                anyOf:
                - name: hasItem
                  type: org.hamcrest.Matchers
                - name: hasItem
                  type: org.hamcrest.CoreMatchers
                - name: hasItem
                  type: org.hamcrest.core.IsIterableContaining
      argCount: 2
    - args:
        1:
          type: String
        3:
          value:
            is:
              methodcall:
                args:
                  1:
                    anyOf:
                    - not:
                        value:
                          is:
                            methodcall: {}
                    - value:
                        is:
                          methodcall:
                            anyOf:
                            - name: equalTo
                              type: org.hamcrest.Matchers
                            - name: equalTo
                              type: org.hamcrest.CoreMatchers
                            - name: equalTo
                              type: org.hamcrest.core.IsEqual
                            - args:
                                1:
                                  value:
                                    not:
                                      is:
                                        methodcall: {}
                              name: is
                              type: org.hamcrest.Matchers
                            - args:
                                1:
                                  value:
                                    not:
                                      is:
                                        methodcall: {}
                              name: is
                              type: org.hamcrest.CoreMatchers
                            - args:
                                1:
                                  value:
                                    not:
                                      is:
                                        methodcall: {}
                              name: is
                              type: org.hamcrest.core.Is
                argCount: 1
                anyOf:
                - name: hasItem
                  type: org.hamcrest.Matchers
                - name: hasItem
                  type: org.hamcrest.CoreMatchers
                - name: hasItem
                  type: org.hamcrest.core.IsIterableContaining
      argCount: 3
    type: org.hamcrest.MatcherAssert
availableFixes:
- doStaticImports: true
  name: Change to assertThat(collection).contains(expectedElement)
  availableIf:
    markedElement:
      is:
        methodcall:
          args:
            2:
              value:
                is:
                  methodcall:
                    args:
                      1:
                        not:
                          type:
                            reference:
                              matches: org.hamcrest.Matcher<.*>
                            checkInheritance: true
                    argCount: 1
          argCount: 2
  actions:
  - rewrite:
      to: org.assertj.core.api.Assertions.assertThat({{{ arguments.0 }}}).contains({{{ arguments.1.arguments }}})
- doStaticImports: true
  name: Change to assertThat(collection).as(reason).contains(expectedElement)
  availableIf:
    markedElement:
      is:
        methodcall:
          args:
            3:
              value:
                is:
                  methodcall:
                    args:
                      1:
                        not:
                          type:
                            reference:
                              matches: org.hamcrest.Matcher<.*>
                            checkInheritance: true
                    argCount: 1
          argCount: 3
  actions:
  - rewrite:
      to: org.assertj.core.api.Assertions.assertThat({{{ arguments.1 }}}).as({{{ arguments.0 }}}).contains({{{ arguments.2.arguments }}})
- doStaticImports: true
  name: Simplify to assertThat(collection).contains(expectedElement)
  availableIf:
    markedElement:
      is:
        methodcall:
          args:
            2:
              value:
                is:
                  methodcall:
                    args:
                      1:
                        value:
                          is:
                            methodcall:
                              args:
                                1:
                                  not:
                                    type:
                                      reference:
                                        matches: org.hamcrest.Matcher<.*>
                                      checkInheritance: true
                              argCount: 1
                              name:
                                matches: (equalTo|is)
                    argCount: 1
          argCount: 2
  actions:
  - rewrite:
      to: org.assertj.core.api.Assertions.assertThat({{{ arguments.0 }}}).contains({{{ arguments.1.arguments.0.arguments }}})
- doStaticImports: true
  name: Simplify to assertThat(collection).as(reason).contains(expectedElement)
  availableIf:
    markedElement:
      is:
        methodcall:
          args:
            3:
              value:
                is:
                  methodcall:
                    args:
                      1:
                        value:
                          is:
                            methodcall:
                              argCount: 1
                              name:
                                matches: (equalTo|is)
                    argCount: 1
          argCount: 3
  actions:
  - rewrite:
      to: org.assertj.core.api.Assertions.assertThat({{{ arguments.1 }}}).as({{{ arguments.0 }}}).contains({{{ arguments.2.arguments.0.arguments }}})