<commit_before>/**
 * Copyright (C) 2013 <EMAIL> (<EMAIL>)
 *
 * Licensed 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 io.motown.ocpp.v15.soap.centralsystem;

import io.motown.domain.api.chargingstation.*;

import io.motown.domain.api.security.AddOnIdentity;
import io.motown.domain.api.security.TypeBasedAddOnIdentity;
import io.motown.ocpp.soaputils.header.SoapHeaderReader;
import io.motown.ocpp.v15.soap.centralsystem.schema.*;
import io.motown.ocpp.v15.soap.centralsystem.schema.FirmwareStatus;
import io.motown.ocpp.viewmodel.domain.AuthorizationResult;
import io.motown.ocpp.viewmodel.domain.BootChargingStationResult;
import io.motown.ocpp.viewmodel.domain.DomainService;
import io.motown.ocpp.viewmodel.domain.IncomingDataTransferResult;
import org.apache.cxf.continuations.Continuation;
import org.apache.cxf.continuations.ContinuationProvider;
import org.junit.Before;
import org.junit.Test;

import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static io.motown.domain.api.chargingstation.test.ChargingStationTestUtils.*;
import static io.motown.ocpp.v15.soap.V15SOAPTestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

public class MotownCentralSystemServiceTest {

    private MotownCentralSystemService motownCentralSystemService;

    private DomainService domainService;

    private SoapHeaderReader soapHeaderReader;

    private static final String ADD_ON_TYPE = "OCPPS15";

    private static final AddOnIdentity OCPPS15_ADD_ON_IDENTITY = new TypeBasedAddOnIdentity(ADD_ON_TYPE, ADD_ON_ID);

    @Before
    public void setup() {
        motownCentralSystemService = new MotownCentralSystemService();
        motownCentralSystemService.setContinuationTimeout(100);

        soapHeaderReader = mock(SoapHeaderReader.class);
        when(soapHeaderReader.getChargingStationAddress(any(MessageContext.class))).thenReturn(LOCALHOST);
        motownCentralSystemService.setSoapHeaderReader(soapHeaderReader);

        domainService = mock(DomainService.class);
        motownCentralSystemService.setDomainService(domainService);
        motownCentralSystemService.setAddOnId(ADD_ON_ID);
        motownCentralSystemService.setContext(mock(WebServiceContext.class));
    }

    @Test
    public void authorizeAcceptedVerifyResponse() throws InterruptedException, ExecutionException {
        WebServiceContext webServiceContext = mock(WebServiceContext.class);
        MessageContext messageContext = mock(MessageContext.class);
        ContinuationProvider continuationProvider = mock(ContinuationProvider.class);
        Continuation continuation = mock(Continuation.class);
        Future future = mock(Future.class);
        AuthorizationResult authorizationResult = mock(AuthorizationResult.class);
        when(authorizationResult.getStatus()).thenReturn(AuthorizationResultStatus.ACCEPTED);
        when(future.isDone()).thenReturn(true);
        when(future.get()).thenReturn(authorizationResult);
        when(continuation.getObject()).thenReturn(future);
        when(continuationProvider.getContinuation()).thenReturn(continuation);
        when(messageContext.get(any())).thenReturn(continuationProvider);
        when(webServiceContext.getMessageContext()).thenReturn(messageContext);
        motownCentralSystemService.setContext(webServiceContext);

        AuthorizeRequest request = new AuthorizeRequest();
        request.setIdTag(IDENTIFYING_TOKEN.getToken());

        AuthorizeResponse response = motownCentralSystemService.authorize(request, CHARGING_STATION_ID.getId());

        assertEquals(AuthorizationStatus.ACCEPTED, response.getIdTagInfo().getStatus());
    }

    @Test
    public void dataTransferAcceptedVerifyResponse() throws InterruptedException, ExecutionException {
        WebServiceContext webServiceContext = mock(WebServiceContext.class);
        MessageContext messageContext = mock(MessageContext.class);
        ContinuationProvider continuationProvider = mock(ContinuationProvider.class);
        Continuation continuation = mock(Continuation.class);
        Future future = mock(Future.class);
        IncomingDataTransferResult incomingDataTransferResult = mock(IncomingDataTransferResult.class);
        when(incomingDataTransferResult.getStatus()).thenReturn(IncomingDataTransferResultStatus.ACCEPTED);
        when(future.isDone()).thenReturn(true);
        when(future.get()).thenReturn(incomingDataTransferResult);
        when(continuation.getObject()).thenReturn(future);
        when(continuationProvider.getContinuation()).thenReturn(continuation);
        when(messageContext.get(any())).thenReturn(continuationProvider);
        when(webServiceContext.getMessageContext()).thenReturn(messageContext);
        motownCentralSystemService.setContext(webServiceContext);

        DataTransferRequest request = new DataTransferRequest();

        DataTransferResponse response = motownCentralSystemService.dataTransfer(request, CHARGING_STATION_ID.getId());

        assertEquals(DataTransferStatus.ACCEPTED, response.getStatus());
    }

    @Test
    public void dataTransferRejectedVerifyResponse() throws InterruptedException, ExecutionException {
        WebServiceContext webServiceContext = mock(WebServiceContext.class);
        MessageContext messageContext = mock(MessageContext.class);
        ContinuationProvider continuationProvider = mock(ContinuationProvider.class);
        Continuation continuation = mock(Continuation.class);
        Future future = mock(Future.class);
        IncomingDataTransferResult incomingDataTransferResult = mock(IncomingDataTransferResult.class);
        when(incomingDataTransferResult.getStatus()).thenReturn(IncomingDataTransferResultStatus.REJECTED);
        when(future.isDone()).thenReturn(true);
        when(future.get()).thenReturn(incomingDataTransferResult);
        when(continuation.getObject()).thenReturn(future);
        when(continuationProvider.getContinuation()).thenReturn(continuation);
        when(messageContext.get(any())).thenReturn(continuationProvider);
        when(webServiceContext.getMessageContext()).thenReturn(messageContext);
        motownCentralSystemService.setContext(webServiceContext);

        DataTransferRequest request = new DataTransferRequest();
<commit_msg>Added tests<commit_after>/**
 * Copyright (C) 2013 Mot<EMAIL> (<EMAIL>)
 *
 * Licensed 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 io.motown.ocpp.v15.soap.centralsystem;

import io.motown.domain.api.chargingstation.*;

import io.motown.domain.api.security.AddOnIdentity;
import io.motown.domain.api.security.TypeBasedAddOnIdentity;
import io.motown.ocpp.soaputils.header.SoapHeaderReader;
import io.motown.ocpp.v15.soap.centralsystem.schema.*;
import io.motown.ocpp.v15.soap.centralsystem.schema.FirmwareStatus;
import io.motown.ocpp.viewmodel.domain.AuthorizationResult;
import io.motown.ocpp.viewmodel.domain.BootChargingStationResult;
import io.motown.ocpp.viewmodel.domain.DomainService;
import io.motown.ocpp.viewmodel.domain.IncomingDataTransferResult;
import org.apache.cxf.continuations.Continuation;
import org.apache.cxf.continuations.ContinuationProvider;
import org.junit.Before;
import org.junit.Test;

import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static io.motown.domain.api.chargingstation.test.ChargingStationTestUtils.*;
import static io.motown.ocpp.v15.soap.V15SOAPTestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

public class MotownCentralSystemServiceTest {

    private MotownCentralSystemService motownCentralSystemService;

    private DomainService domainService;

    private SoapHeaderReader soapHeaderReader;

    private static final String ADD_ON_TYPE = "OCPPS15";

    private static final AddOnIdentity OCPPS15_ADD_ON_IDENTITY = new TypeBasedAddOnIdentity(ADD_ON_TYPE, ADD_ON_ID);

    @Before
    public void setup() {
        motownCentralSystemService = new MotownCentralSystemService();
        motownCentralSystemService.setContinuationTimeout(100);

        soapHeaderReader = mock(SoapHeaderReader.class);
        when(soapHeaderReader.getChargingStationAddress(any(MessageContext.class))).thenReturn(LOCALHOST);
        motownCentralSystemService.setSoapHeaderReader(soapHeaderReader);

        domainService = mock(DomainService.class);
        motownCentralSystemService.setDomainService(domainService);
        motownCentralSystemService.setAddOnId(ADD_ON_ID);
        motownCentralSystemService.setContext(mock(WebServiceContext.class));
    }

    private void prepareAuthorization(AuthorizationResultStatus expectedStatus) throws InterruptedException, ExecutionException {
        WebServiceContext webServiceContext = mock(WebServiceContext.class);
        MessageContext messageContext = mock(MessageContext.class);
        ContinuationProvider continuationProvider = mock(ContinuationProvider.class);
        Continuation continuation = mock(Continuation.class);
        Future future = mock(Future.class);
        AuthorizationResult authorizationResult = mock(AuthorizationResult.class);
        when(authorizationResult.getStatus()).thenReturn(expectedStatus);
        when(future.isDone()).thenReturn(true);
        when(future.get()).thenReturn(authorizationResult);
        when(continuation.getObject()).thenReturn(future);
        when(continuationProvider.getContinuation()).thenReturn(continuation);
        when(messageContext.get(any())).thenReturn(continuationProvider);
        when(webServiceContext.getMessageContext()).thenReturn(messageContext);
        motownCentralSystemService.setContext(webServiceContext);
    }

    @Test
    public void authorizeAcceptedVerifyResponse() throws InterruptedException, ExecutionException {
        prepareAuthorization(AuthorizationResultStatus.ACCEPTED);

        AuthorizeRequest request = new AuthorizeRequest();
        request.setIdTag(IDENTIFYING_TOKEN.getToken());

        AuthorizeResponse response = motownCentralSystemService.authorize(request, CHARGING_STATION_ID.getId());

        assertEquals(AuthorizationStatus.ACCEPTED, response.getIdTagInfo().getStatus());
    }

    @Test
    public void authorizeInvalidVerifyResponse() throws InterruptedException, ExecutionException {
        prepareAuthorization(AuthorizationResultStatus.INVALID);

        AuthorizeRequest request = new AuthorizeRequest();
        request.setIdTag(IDENTIFYING_TOKEN.getToken());

        AuthorizeResponse response = motownCentralSystemService.authorize(request, CHARGING_STATION_ID.getId());

        assertEquals(AuthorizationStatus.INVALID, response.getIdTagInfo().getStatus());
    }

    @Test
    public void authorizeBlockedVerifyResponse() throws InterruptedException, ExecutionException {
        prepareAuthorization(AuthorizationResultStatus.BLOCKED);

        AuthorizeRequest request = new AuthorizeRequest();
        request.setIdTag(IDENTIFYING_TOKEN.getToken());

        AuthorizeResponse response = motownCentralSystemService.authorize(request, CHARGING_STATION_ID.getId());

        assertEquals(AuthorizationStatus.BLOCKED, response.getIdTagInfo().getStatus());
    }

    @Test
    public void authorizeExpiredVerifyResponse() throws InterruptedException, ExecutionException {
        prepareAuthorization(AuthorizationResultStatus.EXPIRED);

        AuthorizeRequest request = new AuthorizeRequest();
        request.setIdTag(IDENTIFYING_TOKEN.getToken());

        AuthorizeResponse response = motownCentralSystemService.authorize(request, CHARGING_STATION_ID.getId());

        assertEquals(AuthorizationStatus.EXPIRED, response.getIdTagInfo().getStatus());
    }

    private void prepareDataTransfer(IncomingDataTransferResultStatus expectedStatus) throws InterruptedException, ExecutionException {
        WebServiceContext webServiceContext = mock(WebServiceContext.class);
        MessageContext messageContext = mock(MessageContext.class);
        ContinuationProvider continuationProvider = mock(ContinuationProvider.class);
        Continuation continuation = mock(Continuation.class);
        Future future = mock(Future.class);
        IncomingDataTransferResult incomingDataTransferResult = mock(IncomingDataTransferResult.class);
        when(incomingDataTransferResult.getStatus()).thenReturn(expectedStatus);
        when(future.isDone()).thenReturn(true);
        when(future.get()).thenReturn(incomingDataTransferResult);
        when(continuation.getObject()).thenReturn(future);
        when(continuationProvider.getContinuation()).thenReturn(continuation);
        when(messageContext.get(any())).thenReturn(continuationProvider);
        when(webServiceContext.getMessageContext()).thenReturn(messageContext);
        motownCentralSystemService.setContext(webServiceContext);
    }

    @Test
    public void dataTransferAcceptedVerifyResponse() throws InterruptedException, ExecutionException {
        prepareDataTransfer(IncomingDataTransferResultStatus.ACCEPTED);

        DataTransferRequest request = new DataTransferRequest();

        DataTransferResponse response = motownCentralSystemService.dataTransfer(request, CHARGING_STATION_ID.getId());

        assertEquals(DataTransferStatus.ACCEPTED, response.getStatus());
    }

    @Test
    public void dataTransferRejectedVerifyResponse() throws InterruptedException, ExecutionException {
        prepareDataTransfer(IncomingDataTransferResultStatus.REJECTED);

        DataTransferRequest request = new DataTransferRequest();

{'fraction_non_alphanumeric': 0.08809146180280202, 'fraction_numerical': 0.003304255881575469, 'mean_word_length': 5.065330661322645, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5112344', 'n_tokens_mistral': 3923, 'n_tokens_neox': 3836, 'n_words': 681}