/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.servicenow.auth;

import java.util.concurrent.TimeUnit;

import org.apache.camel.component.servicenow.ServiceNowConfiguration;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.rs.security.oauth2.client.Consumer;
import org.apache.cxf.rs.security.oauth2.client.OAuthClientUtils;
import org.apache.cxf.rs.security.oauth2.common.ClientAccessToken;
import org.apache.cxf.rs.security.oauth2.grants.owner.ResourceOwnerGrant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OAuthToken {
    private static final Logger LOGGER = LoggerFactory.getLogger(OAuthToken.class);

    private final String url;
    private final ServiceNowConfiguration configuration;
    private ClientAccessToken token;
    private String authString;
    private long expireAt;

    public OAuthToken(String url, ServiceNowConfiguration configuration) {
        this.url = url;
        this.configuration = configuration;
        this.token = null;
        this.authString = null;
        this.expireAt = 0;
    }

    private synchronized void getOrRefreshAccessToken() {
        if (token == null) {
            LOGGER.debug("Generate OAuth token");

            token = OAuthClientUtils.getAccessToken(
                WebClient.create(url),
                new Consumer(
                    configuration.getOauthClientId(),
                    configuration.getOauthClientSecret()),
                new ResourceOwnerGrant(
                    configuration.getUserName(),
                    configuration.getPassword()),
                true
            );

            LOGGER.debug("OAuth token expires in {}s", token.getExpiresIn());

            token.setIssuedAt(System.currentTimeMillis());
            token.setExpiresIn(TimeUnit.MILLISECONDS.convert(token.getExpiresIn(), TimeUnit.SECONDS));

            authString = token.toString();
            expireAt = token.getIssuedAt() + token.getExpiresIn();
        } else if (System.currentTimeMillis() >= expireAt) {
            LOGGER.debug("OAuth token is expired, refresh it");

            token = OAuthClientUtils.refreshAccessToken(
                WebClient.create(url),
                new Consumer(
                    configuration.getOauthClientId(),
                    configuration.getOauthClientSecret()),
                token,
                null,
                false
            );

            LOGGER.debug("OAuth token expires in {}s", token.getExpiresIn());

            token.setIssuedAt(System.currentTimeMillis());
            token.setExpiresIn(TimeUnit.MILLISECONDS.convert(token.getExpiresIn(), TimeUnit.SECONDS));

            authString = token.toString();
            expireAt = token.getIssuedAt() + token.getExpiresIn();
        }
    }

    public ClientAccessToken getClientAccess() {
        getOrRefreshAccessToken();
        return token;
    }

    public String getAuthString() {
        getOrRefreshAccessToken();
        return authString;
    }
}

@@ -2684,32 +2684,81 @@
 ken.toString();%0A
+%0A            if (token.getExpiresIn() %3E 0) %7B%0A    
             expi
@@ -2820,19 +2820,49 @@
     
-%7D else if (
+    %7D%0A        %7D else if (expireAt %3E 0 && 
 Syst
@@ -3304,32 +3304,42 @@
   LOGGER.debug(%22
+Refreshed 
 OAuth token expi
@@ -3577,24 +3577,73 @@
 toString();%0A
+%0A            if (token.getExpiresIn() %3E 0) %7B%0A    
             
@@ -3689,32 +3689,46 @@
 getExpiresIn();%0A
+            %7D%0A
         %7D%0A    %7D%0A

Remove unused code in test classes