Skip to content

Commit

Permalink
Testcase for AppGroup/Teams:3x (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-intelli authored Aug 23, 2023
1 parent 7609cd4 commit fc25a76
Show file tree
Hide file tree
Showing 24 changed files with 908 additions and 3 deletions.
1 change: 1 addition & 0 deletions tests/Api/ApigeeX/Controller/ApiProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Tests\Test\Controller\EntityControllerTester;
use Apigee\Edge\Tests\Test\Controller\EntityControllerTesterInterface;
use Apigee\Edge\Tests\Test\Controller\EntityLoadOperationControllerTestTrait;

/**
* Class ApiProductControllerTest.
Expand Down
43 changes: 43 additions & 0 deletions tests/Api/ApigeeX/Controller/AppGroupControllerAwareTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.
*/

namespace Apigee\Edge\Tests\Api\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Controller\AppGroupController;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Tests\Test\Controller\DefaultAPIClientAwareTrait;
use Apigee\Edge\Tests\Test\Controller\DefaultTestOrganizationAwareTrait;
use Apigee\Edge\Tests\Test\Controller\EntityControllerTester;

trait AppGroupControllerAwareTestTrait
{
use DefaultTestOrganizationAwareTrait;
use DefaultAPIClientAwareTrait;

/**
* @param \Apigee\Edge\ClientInterface|null $client
*
* @return \Apigee\Edge\Tests\Test\Controller\EntityControllerTester|\Apigee\Edge\Api\ApigeeX\Controller\AppGroupControllerInterface
*/
protected static function appGroupController(ClientInterface $client = null): EntityControllerTester
{
$client = $client ?? static::defaultAPIClient();

return new EntityControllerTester(new AppGroupController(static::defaultTestOrganization($client), $client));
}
}
109 changes: 109 additions & 0 deletions tests/Api/ApigeeX/Controller/AppGroupControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.
*/

namespace Apigee\Edge\Tests\Api\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Entity\AppGroupInterface;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Entity\EntityInterface;
use Apigee\Edge\Tests\Api\ApigeeX\Entity\AppGroupTestEntityProviderTrait;
use Apigee\Edge\Tests\Api\Management\Controller\AttributesAwareEntityControllerTestTrait;
use Apigee\Edge\Tests\Test\Controller\DefaultAPIClientAwareTrait;
use Apigee\Edge\Tests\Test\Controller\EntityControllerTesterInterface;
use Apigee\Edge\Tests\Test\Controller\EntityCreateOperationControllerTester;
use Apigee\Edge\Tests\Test\Controller\EntityCreateOperationControllerTestTrait;
use Apigee\Edge\Tests\Test\Controller\EntityCreateOperationTestControllerTesterInterface;
use Apigee\Edge\Tests\Test\Controller\EntityDeleteOperationControllerTestTrait;
use Apigee\Edge\Tests\Test\Controller\EntityUpdateOperationControllerTestTrait;
use Apigee\Edge\Tests\Test\TestClientFactory;
use Apigee\Edge\Tests\Test\Utility\MarkOnlineTestSkippedAwareTrait;

/**
* Class AppGroupControllerTest.
*
* @group controller
* @group management
*/
class AppGroupControllerTest extends EntityControllerTestBase
{
use AppGroupControllerAwareTestTrait;
use AppGroupTestEntityProviderTrait;
use DefaultAPIClientAwareTrait;
use MarkOnlineTestSkippedAwareTrait;
// The order of these trait matters. Check @depends in test methods.
use EntityCreateOperationControllerTestTrait;
use EntityLoadOperationControllerTestTrait;
use EntityUpdateOperationControllerTestTrait;
use EntityDeleteOperationControllerTestTrait;
use PaginatedEntityListingControllerTestTraitBase;
use PaginatedEntityListingControllerTestTrait;
use AttributesAwareEntityControllerTestTrait;

/**
* @group online
*/
public function testStatusChange(): void
{
static::markOnlineTestSkipped(__FUNCTION__);
/** @var \Apigee\Edge\Api\ApigeeX\Controller\AppGroupControllerInterface $controller */
$controller = static::entityController();

/** @var \Apigee\Edge\Api\ApigeeX\Entity\AppGroupInterface $entity */
$entity = static::getNewEntity();
$controller->create($entity);
$controller->setStatus($entity->id(), AppGroupInterface::STATUS_INACTIVE);
$entity = $controller->load($entity->id());
$this->assertEquals($entity->getStatus(), AppGroupInterface::STATUS_INACTIVE);
$controller->setStatus($entity->id(), AppGroupInterface::STATUS_ACTIVE);
$entity = $controller->load($entity->id());
$this->assertEquals($entity->getStatus(), AppGroupInterface::STATUS_ACTIVE);
}

/**
* {@inheritdoc}
*/
protected static function entityController(ClientInterface $client = null): EntityControllerTesterInterface
{
return static::appGroupController($client);
}

/**
* {@inheritdoc}
*/
protected static function getNewEntity(): EntityInterface
{
return static::getNewAppGroup(!TestClientFactory::isOfflineClient(static::defaultAPIClient()));
}

/**
* {@inheritdoc}
*/
protected function entityForUpdateTest(EntityInterface $existing): EntityInterface
{
/* @var \Apigee\Edge\Api\ApigeeX\Entity\AppGroupInterface $existing */
return static::getUpdatedAppGroup($existing, !TestClientFactory::isOfflineClient(static::defaultAPIClient()));
}

/**
* {@inheritdoc}
*/
protected static function entityCreateOperationTestController(): EntityCreateOperationTestControllerTesterInterface
{
return new EntityCreateOperationControllerTester(static::entityController());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.
*/

namespace Apigee\Edge\Tests\Api\ApigeeX\Controller;

use Apigee\Edge\Exception\ApiResponseException;
use Apigee\Edge\Tests\Test\Controller\DefaultAPIClientAwareTrait;
use Apigee\Edge\Tests\Test\Controller\EntityControllerAwareTestTrait;
use Apigee\Edge\Tests\Test\Controller\EntityCreateOperationTestControllerAwareTrait;
use Apigee\Edge\Tests\Test\Entity\NewEntityProviderTrait;
use Apigee\Edge\Tests\Test\Utility\MarkOnlineTestSkippedAwareTrait;
use DMS\PHPUnitExtensions\ArraySubset\Assert;

trait AttributesAwareEntityControllerTestTrait
{
use DefaultAPIClientAwareTrait;
use EntityCreateOperationTestControllerAwareTrait;
use EntityControllerAwareTestTrait;
use NewEntityProviderTrait;
use MarkOnlineTestSkippedAwareTrait;

public function testAddAttributesToEntity(): string
{
/** @var \Apigee\Edge\Api\Management\Controller\AttributesAwareEntityControllerInterface $controller */
$controller = $this->entityController();
/** @var \Apigee\Edge\Entity\Property\AttributesPropertyInterface|\Apigee\Edge\Entity\EntityInterface $entity */
$entity = static::getNewEntity();
static::entityCreateOperationTestController()->create($entity);
/** @var \Apigee\Edge\Structure\AttributesProperty $attributes */
$attributes = $entity->getAttributes();
$originalAttributes = $attributes->values();
$attributes->add('name1', 'value1');
$attributes->add('name2', 'value2');
/** @var \Apigee\Edge\Structure\AttributesProperty $attributesProperty */
$attributesProperty = $controller->updateAttributes($entity->id(), $attributes);
/** @var array $newAttributes */
$newAttributes = $attributesProperty->values();
Assert::assertArraySubset($originalAttributes, $newAttributes);
$this->assertArrayHasKey('name1', $newAttributes);
$this->assertArrayHasKey('name2', $newAttributes);
$this->assertEquals('value1', $newAttributes['name1']);
$this->assertEquals('value2', $newAttributes['name2']);

return $entity->id();
}

/**
* @depends testAddAttributesToEntity
*
* @param string $entityId
*/
public function testGetAttributes(string $entityId): void
{
/** @var \Apigee\Edge\Api\Management\Controller\AttributesAwareEntityControllerInterface $controller */
$controller = $this->entityController();
/** @var \Apigee\Edge\Structure\AttributesProperty $attributesProperty */
$attributesProperty = $controller->getAttributes($entityId);
$attributes = $attributesProperty->values();
$this->assertNotEmpty($attributes);
$this->assertArrayHasKey('name1', $attributes);
$this->assertArrayHasKey('name2', $attributes);
$this->assertEquals('value1', $attributes['name1']);
$this->assertEquals('value2', $attributes['name2']);
}

/**
* @depends testAddAttributesToEntity
*
* @param string $entityId
*/
public function testGetAttribute(string $entityId): void
{
/** @var \Apigee\Edge\Api\Management\Controller\AttributesAwareEntityControllerInterface $controller */
$controller = $this->entityController();
$value = $controller->getAttribute($entityId, 'name1');
$this->assertEquals('value1', $value);
}

/**
* @depends testAddAttributesToEntity
*
* @param string $entityId
*
* @return string
*/
public function testUpdateAttribute(string $entityId): string
{
/** @var \Apigee\Edge\Api\Management\Controller\AttributesAwareEntityControllerInterface $controller */
$controller = $this->entityController();
$expected = 'value1-edited';
$value = $controller->updateAttribute($entityId, 'name1', $expected);
$this->assertEquals($expected, $value);

return $entityId;
}

/**
* @depends testUpdateAttribute
*
* @group online
*
* @param string $entityId
*/
public function testDeleteAttribute(string $entityId): void
{
static::markOnlineTestSkipped(__FUNCTION__);
/** @var \Apigee\Edge\Api\Management\Controller\AttributesAwareEntityControllerInterface $controller */
$controller = $this->entityController();
$attribute = 'name1';
$controller->deleteAttribute($entityId, $attribute);
try {
$controller->getAttribute($entityId, $attribute);
} catch (ApiResponseException $e) {
$this->assertEquals('organizations.keymanagement.AttributeDoesntExist', $e->getEdgeErrorCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public function testLoad($created = null): EntityInterface
return $entity;
}

protected function validateLoadedEntity(EntityInterface $expected, EntityInterface $actual): void
{
// Tricky Management API does not return "description" property
// if it is not set in create but it returns an empty string when
// it gets loaded.
if ($expected instanceof DescriptionPropertyInterface && $actual instanceof DescriptionPropertyInterface && null !== $actual->getDescription()) {
$expected->setDescription($actual->getDescription());
}
$expected->setCreatedAt($actual->getCreatedAt());
$expectedAsObject = json_decode($this->entitySerializer()->serialize($expected, 'json'));

// Validate that we got back the same object as we created.
$this->entitySerializerValidator()->validate($expectedAsObject, $actual);
}

protected function alterObjectsBeforeCompareExpectedAndLoadedEntity(\stdClass &$expectedAsObject, EntityInterface $loaded): void
{
// The serialized version of the created (actual) would not be the
Expand Down
Loading

0 comments on commit fc25a76

Please sign in to comment.