Skip to content

Commit

Permalink
feat: change INR factory and send msi response code, to allow reuse a…
Browse files Browse the repository at this point in the history
…nd fix connectivity VOL-5801
  • Loading branch information
ilindsay committed Mar 5, 2025
1 parent 7cd8ce8 commit 2fce32e
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 108 deletions.
16 changes: 0 additions & 16 deletions app/api/config/autoload/local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,6 @@ return [
// The cross account role that VOL will assume to access the output bucket and output SQS queue.
'txc_consumer_role_arn' => 'arn:aws:iam::000000000000:role/txc-local-consumer-role',
],
'nr' => [
// @to-do currently waiting on the actual nr address
'inr_service' => [
'uri' => 'http://testServiceAddress',
'oauth2' => [ // if client['headers']['Authorization'] is not set, then this will be used to get token
'client_id' => '', //param
'client_secret' => '', // secret
'token_url' => '', //param
'scope' => '', //param
'proxy' => new \Laminas\Stdlib\ArrayUtils\MergeRemoveKey(),
]
],
'repute_url' => [
'uri' => null,
],
],
'print' => [
'server' => 'print.localdev:631'
],
Expand Down
2 changes: 1 addition & 1 deletion app/api/module/Api/config/command-map.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@
=> CommandHandler\Cases\ConditionUndertaking\DeleteConditionUndertakingS4::class,

// Domain - Si, Erru
Command\Cases\Si\SendResponse::class => CommandHandler\Cases\Si\SendResponse::class,
Command\Cases\Si\SendResponse::class => CommandHandler\Cases\Si\SendResponseFactory::class,
Command\Cases\Si\ComplianceEpisode::class => CommandHandler\Cases\Si\ComplianceEpisode::class,

// Domain - Schedule41
Expand Down
1 change: 0 additions & 1 deletion app/api/module/Api/config/validation-map/cli.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
CommandHandler\Email\SendLiquidatedCompanyForUnregisteredUser::class => IsSystemUser::class,
CommandHandler\Email\SendFailedOrganisationsList::class => IsSystemUser::class,
CommandHandler\Email\SendForgotPassword::class => IsSystemUser::class,
CommandHandler\Cases\Si\SendResponse::class => IsSystemUser::class,
CommandHandler\PrintScheduler\PrintJob::class => IsSystemUser::class,
CommandHandler\PrintScheduler\PrintJobFactory::class => IsSystemUser::class,
CommandHandler\TransportManagerApplication\Snapshot::class => IsSystemUser::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Dvsa\Olcs\Api\Domain\QueryHandler;
use Dvsa\Olcs\Api\Domain\CommandHandler;
use Dvsa\Olcs\Api\Domain\Validation\Handlers as Handler;
use Dvsa\Olcs\Api\Domain\Validation\Handlers\Misc;

return [
Expand All @@ -23,5 +22,7 @@
/**
* Create response xml and send to ATOS
*/
CommandHandler\Cases\Si\CreateResponse::class => Misc\IsInternalUser::class
CommandHandler\Cases\Si\CreateResponse::class => Misc\IsInternalUser::class,
CommandHandler\Cases\Si\SendResponse::class => Misc\IsInternalUser::class,
CommandHandler\Cases\Si\SendResponseFactory::class => Misc\IsInternalUser::class,
];
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@
use Dvsa\Olcs\Api\Service\Nr\InrClient;
use Dvsa\Olcs\DocumentShare\Data\Object\File;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Api\Service\Nr\InrClientInterface;
use Dvsa\Olcs\Api\Entity\Si\ErruRequest as ErruRequestEntity;
use Dvsa\Olcs\Api\Domain\Command\Cases\Si\SendResponse as SendResponseCmd;
use Laminas\Http\Response;
use Laminas\Http\Client\Adapter\Exception\RuntimeException as AdapterRuntimeException;
use Dvsa\Olcs\Api\Domain\Exception\InrClientException;
use Psr\Container\ContainerInterface;

/**
* SendResponse
*
* @author Ian Lindsay <[email protected]>
*/
final class SendResponse extends AbstractCommandHandler implements UploaderAwareInterface
{
use UploaderAwareTrait;
Expand All @@ -32,10 +25,9 @@ final class SendResponse extends AbstractCommandHandler implements UploaderAware
'Document'
];

/**
* @var InrClient
*/
protected $inrClient;
public function __construct(private readonly InrClient $inrClient)
{
}

/**
* SendResponse
Expand Down Expand Up @@ -96,11 +88,4 @@ private function updateStatus(ErruRequestEntity $erruRequest, $statusKey)

return $erruRequest;
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$fullContainer = $container;

$this->inrClient = $container->get(InrClientInterface::class);
return parent::__invoke($fullContainer, $requestedName, $options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Dvsa\Olcs\Api\Domain\CommandHandler\Cases\Si;

use Dvsa\Olcs\Api\Service\Nr\InrClientInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;

final class SendResponseFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$inrClient = $container->build(InrClientInterface::class, ['path' => '/ncr']);
return (new SendResponse($inrClient))->__invoke($container, $requestedName, $options);
}
}
33 changes: 4 additions & 29 deletions app/api/module/Api/src/Service/Nr/InrClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,13 @@
use Laminas\Http\Request;
use Olcs\Logging\Log\Logger;

/**
* Class InrClient
* @package Dvsa\Olcs\Api\Service\Nr
*/
class InrClient implements InrClientInterface
{
/**
* Contructor, expects Laminas rest client
*
* @param RestClient $restClient Laminas rest client
*
* @return void
*/
public function __construct(protected $restClient)
public function __construct(protected RestClient $restClient)
{
}

/**
* Makes a request to INR with penalty information
*
* @param String $xml the xml string being sent
*
* @return String
*/
public function makeRequest($xml)
public function makeRequest(string $xml): int
{
$this->restClient->setEncType('text/xml');
$this->restClient->getRequest()->setMethod(Request::METHOD_POST);
Expand All @@ -45,22 +27,15 @@ public function makeRequest($xml)
return $response->getStatusCode();
}

/**
* Get the rest client
*
* @return RestClient
*/
public function getRestClient()
public function getRestClient(): RestClient
{
return $this->restClient;
}

/**
* close connection to INR
*
* @return void
*/
public function close()
public function close(): void
{
$this->restClient->getAdapter()->close();
}
Expand Down
7 changes: 5 additions & 2 deletions app/api/module/Api/src/Service/Nr/InrClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dvsa\Olcs\Utils\Client\ClientAdapterLoggingWrapper;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\Http\Client as RestClient;
use Olcs\Logging\Log\Logger;
use Psr\Container\ContainerInterface;

/**
Expand All @@ -31,11 +32,13 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
throw new \RuntimeException('Missing INR service config');
}

$path = $options['path'] ?? '';

/** @var Provider $tokenProvider */
$tokenProvider = $container->build(Provider::class, $config['nr']['inr_service']['oauth2']);
$headers = ['Authorization' => 'Bearer ' . $tokenProvider->getToken()];
$httpClient = new RestClient($config['nr']['inr_service']['uri']);

$httpClient = new RestClient($config['nr']['inr_service']['uri'] . $path);
$httpClient->setAdapter($config['nr']['inr_service']['adapter']);
$httpClient->getAdapter()->setOptions($config['nr']['inr_service']['options']);
$httpClient->setHeaders($headers);
Expand Down
10 changes: 1 addition & 9 deletions app/api/module/Api/src/Service/Nr/InrClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

namespace Dvsa\Olcs\Api\Service\Nr;

/**
* Class TransExchangeClient
* @package Dvsa\Olcs\Api\Service\Nr
*/
interface InrClientInterface
{
/**
* @param string $content
* @return int
*/
public function makeRequest($content);
public function makeRequest(string $xml): int;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

declare(strict_types=1);

namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\Cases\Si;

use Dvsa\Olcs\Api\Domain\Exception\InrClientException;
use Mockery as m;
use Dvsa\Olcs\Api\Domain\Command\Result;
use Dvsa\OlcsTest\Api\Domain\CommandHandler\AbstractCommandHandlerTestCase;
Expand All @@ -10,25 +13,22 @@
use Dvsa\Olcs\Api\Domain\Repository\ErruRequest as ErruRequestRepo;
use Dvsa\Olcs\Api\Entity\Si\ErruRequest as ErruRequestEntity;
use Dvsa\Olcs\Api\Service\Nr\InrClient;
use Dvsa\Olcs\Api\Service\Nr\InrClientInterface;
use Laminas\Http\Client\Adapter\Exception\RuntimeException as AdapterRuntimeException;
use Dvsa\Olcs\Api\Service\File\ContentStoreFileUploader;
use Dvsa\Olcs\DocumentShare\Data\Object\File;

/**
* SendResponseTest
*
* @author Ian Lindsay <[email protected]>
*/
class SendResponseTest extends AbstractCommandHandlerTestCase
{
private readonly m\MockInterface $inrService;

public function setUp(): void
{
$this->sut = new SendResponse();
$this->inrService = m::mock(InrClient::class);

$this->sut = new SendResponse($this->inrService);
$this->mockRepo('ErruRequest', ErruRequestRepo::class);

$this->mockedSmServices = [
InrClientInterface::class => m::mock(InrClient::class),
'FileUploader' => m::mock(ContentStoreFileUploader::class)
];

Expand Down Expand Up @@ -70,15 +70,13 @@ public function testHandleCommand()
$this->repoMap['ErruRequest']->shouldReceive('fetchUsingId')->once()->with($command)->andReturn($erruRequest);
$this->repoMap['ErruRequest']->shouldReceive('save')->once()->with(m::type(ErruRequestEntity::class));

$this->mockedSmServices[InrClientInterface::class]
->shouldReceive('makeRequest')
->once()
$this->inrService
->expects('makeRequest')
->with($xml)
->andReturn(202);

$this->mockedSmServices[InrClientInterface::class]
->shouldReceive('close')
->once()
$this->inrService
->expects('close')
->withNoArgs();

$result = $this->sut->handleCommand($command);
Expand All @@ -101,7 +99,7 @@ public function testHandleCommand()
*/
public function testHandleCommandInvalidResponseCode()
{
$this->expectException(\Dvsa\Olcs\Api\Domain\Exception\InrClientException::class);
$this->expectException(InrClientException::class);
$this->expectExceptionMessage('INR Http response code was 400');

$xml = 'xml string';
Expand All @@ -128,15 +126,13 @@ public function testHandleCommandInvalidResponseCode()
$this->repoMap['ErruRequest']->shouldReceive('fetchUsingId')->once()->with($command)->andReturn($erruRequest);
$this->repoMap['ErruRequest']->shouldReceive('save')->once()->with(m::type(ErruRequestEntity::class));

$this->mockedSmServices[InrClientInterface::class]
->shouldReceive('makeRequest')
->once()
$this->inrService
->expects('makeRequest')
->with($xml)
->andReturn(400);

$this->mockedSmServices[InrClientInterface::class]
->shouldReceive('close')
->once()
$this->inrService
->expects('close')
->withNoArgs();

$this->sut->handleCommand($command);
Expand All @@ -147,7 +143,7 @@ public function testHandleCommandInvalidResponseCode()
*/
public function testHandleCommandAdapterException()
{
$this->expectException(\Dvsa\Olcs\Api\Domain\Exception\InrClientException::class);
$this->expectException(InrClientException::class);
$this->expectExceptionMessage('There was an error sending the INR response adapter exception message');

$xml = 'xml string';
Expand All @@ -174,9 +170,8 @@ public function testHandleCommandAdapterException()
$this->repoMap['ErruRequest']->shouldReceive('fetchUsingId')->once()->with($command)->andReturn($erruRequest);
$this->repoMap['ErruRequest']->shouldReceive('save')->once()->with(m::type(ErruRequestEntity::class));

$this->mockedSmServices[InrClientInterface::class]
->shouldReceive('makeRequest')
->once()
$this->inrService
->expects('makeRequest')
->with($xml)
->andThrow(AdapterRuntimeException::class, 'adapter exception message');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dvsa\OlcsTest\Api\Service\Nr;

use Dvsa\Olcs\Api\Service\AccessToken\Provider;
Expand Down Expand Up @@ -37,8 +39,6 @@ public function testCreateService()
'options' => [],
'oauth2' => $oauth2
],


]
];

Expand All @@ -48,7 +48,6 @@ public function testCreateService()
$mockTokenProvider = m::mock(Provider::class)
->expects('getToken')->andReturn('token')->getMock();


$mockSl = m::mock(ContainerInterface::class);

$mockSl->shouldReceive('get')->with('config')->andReturn($config);
Expand All @@ -64,12 +63,10 @@ public function testCreateService()
$wrapper = $restClient->getAdapter();
$curl = $wrapper->getAdapter();


$this->assertInstanceOf(InrClientInterface::class, $service);
$this->assertInstanceOf(RestClient::class, $restClient);
$this->assertInstanceOf(ClientAdapterLoggingWrapper::class, $wrapper);
$this->assertInstanceOf(Curl::class, $curl);
$this->assertEquals('Bearer token', $restClient->getHeader('Authorization'));

}
}

0 comments on commit 2fce32e

Please sign in to comment.