content: package org.ays;
  
  import io.jsonwebtoken.Claims;
  import io.jsonwebtoken.JwtBuilder;
  import io.jsonwebtoken.Jwts;
  import org.apache.commons.lang3.time.DateUtils;
  import org.ays.auth.config.AysApplicationConfigurationParameter;
  import org.ays.auth.model.AysToken;
  import org.ays.auth.model.AysUser;
  import org.ays.auth.model.enums.AysTokenClaims;
  import org.ays.auth.port.AysUserReadPort;
  import org.ays.common.util.AysRandomUtil;
  import org.ays.parameter.model.AysParameter;
  import org.ays.parameter.port.AysParameterReadPort;
  import org.ays.util.AysValidTestData;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.mockito.Mock;
  import org.mockito.Mockito;
  import org.mockito.junit.jupiter.MockitoExtension;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.security.oauth2.core.OAuth2AccessToken;
  import org.springframework.test.context.ActiveProfiles;
  
  import java.util.Date;
  import java.util.List;
  import java.util.Map;
  import java.util.Optional;
  
  @SpringBootTest
  @AutoConfigureMockMvc
  @ActiveProfiles({"default", "test"})
  @ExtendWith(MockitoExtension.class)
  public abstract class AysRestControllerTest extends AysTestContainerConfiguration {
  
      @Autowired
      protected AysMockMvc aysMockMvc;
  
      @Autowired
      protected AysUserReadPort userReadPort;
  
  
      protected AysToken mockSuperAdminToken;
      protected AysToken mockAdminToken;
      protected AysToken mockUserToken;
  
  
      @Mock
      private AysApplicationConfigurationParameter applicationConfigurationParameter;
  
      @Mock
      private AysParameterReadPort parameterReadPort;
  
      @BeforeEach
      @SuppressWarnings("OptionalGetWithoutIsPresent disabled because of the test data is valid")
      public void initializeAuth() {
  
          List<AysParameter> parameters = List.of(
                  AysParameter.builder()
                          .name("AUTH_TOKEN_PRIVATE_KEY")
                          .definition("dkNHbEVaSW9rdGQ1cVBKek03dHJqT2daMmdIbEpUSEw=LS0tLS1CRUdJTi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wvUFk4OTB5ajZJWHlzZElYMVRwQ1NST1Uzb1ptUWZBTG9WWWNBNHU3QWxpY0sKYnBKbTYwampiMHNNejRIVU9CaVZIUT09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0KNjNLcEhJM2FXeUtrOHNsVkFiaGY3UjBvcURVeHh5RUk=").build(),
                  AysParameter.builder()
                          .name("AUTH_TOKEN_PUBLIC_KEY")
                          .definition("cExiWUJBSVFTbHVwOXFXSkxSTDhsOGRDVkh1VkxyQ0E=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpWi9pRVJCMzYwSldkWEEzWjJDYQo4RS8yYUZsMjZ6TUlaTHM2VnJnVkE5aHJ1WnNqQXJmcXppYmlyTVZLaC8yOW5Xbm5xT2Job0xuK3JDYTl2YU9pCnQxcEZEeTdhSitqUlhoeVhTOUtwUGVubmw5L1VGeEQ3OTd2OWcyMW50a0dUdTRwOU9zU2lPTnZSOUlmSyt3VGcKck0ydVYyWForUVQ4SDRDYU1nQ3B4aGZOenRJMHpnOTg2TjFoS0pXdnNwbDJyb1Q4L2oyTGI4RVdjQ1JhQUtMdAplV04zN2g5SUpLY2VPME1Db0UwNmVTTWNFZkJiS2xSRGZrV2VFdDZvYWlTallYK2lHTkNrM0ljMmdIODJjQWJUCmV3T2NtL0RBeXhiU0M4bnhSbW1wdEpyTno2MWFBY1AvTEc1WlI0ZzllelUvbHEzREZRVUJka2cyS1hjRndpOTAKV3dJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==SlhaNTl3eFBsWTJEWkNSWDdlZFRlaFhPaFhZRzlBN1Q=").build(),
                  AysParameter.builder()
                          .name("AUTH_ACCESS_TOKEN_EXPIRE_MINUTE")
                          .definition("120")
                          .build(),
                  AysParameter.builder()
                          .name("AUTH_REFRESH_TOKEN_EXPIRE_DAY")
                          .definition("1")
                          .build(),
                  AysParameter.builder()
                          .name("AUTH_LOGIN_MAX_TRY_COUNT")
                          .definition("3")
                          .build(),
                  AysParameter.builder()
                          .name("FE_URL")
                          .definition("http://localhost:3000")
                          .build()
          );
          Mockito.when(parameterReadPort.findAll())
                  .thenReturn(parameters);
  
          this.applicationConfigurationParameter = new AysApplicationConfigurationParameter(parameterReadPort);
  
          final Optional<AysUser> superAdmin = userReadPort.findById(AysValidTestData.SuperAdmin.ID);
          final Claims claimsOfMockSuperAdminToken = superAdmin.get().getClaims();
          this.mockSuperAdminToken = this.generate(claimsOfMockSuperAdminToken);
  
          final Optional<AysUser> admin = userReadPort.findById(AysValidTestData.Admin.ID);
          final Claims claimsOfMockAdminToken = admin.get().getClaims();
          this.mockAdminToken = this.generate(claimsOfMockAdminToken);
  
          final Optional<AysUser> user = userReadPort.findById(AysValidTestData.User.ID);
          final Claims claimsOfMockUserToken = user.get().getClaims();
          this.mockUserToken = this.generate(claimsOfMockUserToken);
      }
  
      private AysToken generate(Map<String, Object> claims) {
          final long currentTimeMillis = System.currentTimeMillis();
  
          final Date tokenIssuedAt = new Date(currentTimeMillis);
  
          final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), applicationConfigurationParameter.getAccessTokenExpireMinute());
          final String accessToken = Jwts.builder()
                  .header()
                  .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
                  .and()
                  .id(AysRandomUtil.generateUUID())
                  .issuer(applicationConfigurationParameter.getTokenIssuer())
                  .issuedAt(tokenIssuedAt)
                  .expiration(accessTokenExpiresAt)
                  .signWith(applicationConfigurationParameter.getTokenPrivateKey())
                  .claims(claims)
                  .compact();
  
          final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), applicationConfigurationParameter.getRefreshTokenExpireMinute());
          final JwtBuilder refreshTokenBuilder = Jwts.builder();
          final String refreshToken = refreshTokenBuilder
                  .header()
                  .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
                  .and()
                  .id(AysRandomUtil.generateUUID())
                  .issuer(applicationConfigurationParameter.getTokenIssuer())
                  .issuedAt(tokenIssuedAt)
                  .expiration(refreshTokenExpiresAt)
                  .signWith(applicationConfigurationParameter.getTokenPrivateKey())
                  .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue()))
                  .compact();
  
          return AysToken.builder()
                  .accessToken(accessToken)
                  .refreshToken(refreshToken)
                  .build();
      }
  
  }
  