1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I abstract this class I'm not the owner of?

Discussion in 'Programming/Internet' started by RollRoll, Oct 8, 2018.

  1. RollRoll

    RollRoll Guest

    I am trying to create a Youtube api wrapper and I'm trying to decouple as much as possible (I'm DIing it with DI container later, please don't focus on that for now. Also, please, I am not asking opinion why not using the YouTube wrapper from the beginning. I hope that is clear)

    The problem I am facing is that the ChannelInfo is a class from YouTubeLibraryV3 original wrapper. How can I decouple that in a way it is testable (no real YouTube api reading) from my unit tests.

    I hope I was clear, please comment if I need to clarify. Here it is:

    Test Class

    [TestFixture]
    public class YouTubeCaptureTests
    {
    private string _apiKeyForTest = "1234";
    private string _channelIdForTest = "123456";


    [Test]
    public void CollectBasicDataTest()
    {
    IApiKey apiKey = new ApiKey(_apiKeyForTest);
    string channelId = _channelIdForTest;

    IYouTubeCaptureRequirements requirements = new YouTubeCaptureRequirements(apiKey);

    var channelInfo = new ChannelInfo(apiKey.ToString());
    var channelResult = new YouTubeChannelResult();

    IYouTubeChannelCapture capture = new YouTubeChannelCapture(requirements, channelInfo, channelResult);
    YouTubeChannelResult result = capture.GetByChannelId(channelId);


    string apiKeyValue = requirements.GetApiKeyValue();
    Assert.IsTrue(apiKey.ToString() == _apiKeyForTest, "passed apiKey is not the same than from .ToString() result");
    Assert.IsTrue(apiKeyValue == apiKey.ToString(), "apiKeyValue == apiKey.ToString() is false. It is supposed to match");
    Assert.IsTrue(result != null, "YouTubeChannelResult result shoudn't be null");
    Assert.IsTrue(result.ViewCount > 0, "result.ViewCount should be more than zero");
    Assert.IsTrue(result.SubscribeCount > 0, "result.SubscribeCount should be more than zero");
    Assert.IsTrue(result.VideoCount > 0, "result.VideoCount should be more than zero");
    }
    }


    Implementation

    public class YouTubeChannelCapture : IYouTubeChannelCapture
    {
    private readonly IYouTubeCaptureRequirements _requirements;
    private readonly ChannelInfo _channelInfo;
    private readonly YouTubeChannelResult _result;

    public YouTubeChannelCapture(IYouTubeCaptureRequirements requirements, ChannelInfo channelInfo, YouTubeChannelResult result)
    {
    _requirements = requirements;
    _channelInfo = channelInfo;
    _result = result;
    }

    public YouTubeChannelResult GetByChannelId(string channelId)
    {
    var output = _channelInfo.GetById(channelId);

    _result.ViewCount = output.ViewCount;
    _result.SubscribeCount = output.SubscriberCount;
    _result.VideoCount = output.VideoCount;
    _result.CommentCount = output.CommentCount;

    return _result;
    }
    }

    public interface IYouTubeChannelCapture
    {
    YouTubeChannelResult GetByChannelId(string channelId);
    }



    public class YouTubeCaptureRequirements : IYouTubeCaptureRequirements
    {
    private readonly IApiKey _apiKey;

    public YouTubeCaptureRequirements(IApiKey apiKey)
    {
    _apiKey = apiKey;
    }

    public string GetApiKeyValue()
    {
    return _apiKey.ToString();
    }
    }

    /// <summary>
    /// Everything required for capturing Youtube Data regardless of the youtube object type
    /// </summary>
    public interface IYouTubeCaptureRequirements
    {
    string GetApiKeyValue();
    }

    Login To add answer/comment
     

Share This Page