<commit_before>#
# Copyright (c) SAS Institute Inc.
#
# 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.
#


from StringIO import StringIO
import os
import tempfile
import unittest

import mock
import requests

from pymaven import Artifact
from pymaven.client import HttpRepository
from pymaven.client import LocalRepository
from pymaven.client import MavenClient
from pymaven.client import Struct
from pymaven.errors import MissingPathError
from pymaven.errors import MissingArtifactError


class TestMavenClient(unittest.TestCase):
    def test_invalid_repo(self):
        self.assertRaises(ValueError, MavenClient, "foo://bar.com")

    @mock.patch("pymaven.client.LocalRepository")
    def test_find_artifacts(self, _LocalRepository):
        _repo1 = mock.Mock(spec=LocalRepository)
        _repo2 = mock.Mock(spec=LocalRepository)

        _LocalRepository.side_effect = [_repo1, _repo2]

        _repo1.get_versions.return_value = [Artifact("foo:bar:2.0"),
                                            Artifact("foo:bar:2.0-SNAPSHOT"),
                                            Artifact("foo:bar:1.0"),
                                            Artifact("foo:bar:1.0-SNAPSHOT"),
                                            ]

        _repo2.get_versions.return_value = [Artifact("foo:bar:3.0"),
                                            Artifact("foo:bar:3.0-SNAPSHOT"),
                                            Artifact("foo:bar:1.0"),
                                            Artifact("foo:bar:1.0-SNAPSHOT"),
                                            ]
        client = MavenClient("foobar", "foobaz")
        expected = [Artifact("foo:bar:3.0"),
                    Artifact("foo:bar:3.0-SNAPSHOT"),
                    Artifact("foo:bar:2.0"),
                    Artifact("foo:bar:2.0-SNAPSHOT"),
                    Artifact("foo:bar:1.0"),
                    Artifact("foo:bar:1.0-SNAPSHOT"),
                    ]
        actual = client.find_artifacts("foo:bar")
        assert expected == actual, "client.find_artifacts(%s)" % input

    @mock.patch("pymaven.client.LocalRepository")
    def test_get_artifact(self, _LocalRepository):

        _repo = mock.Mock(spec=LocalRepository)

        _repo.exists.side_effect = [True, False]
        _repo.open.return_value = StringIO("some data")

        _LocalRepository.return_value = _repo

        client = MavenClient("/maven")

        actual = client.get_artifact("foo:bar:2.0.0")
        assert "some data" == actual.contents.read()
        _repo.exists.assert_called_with("foo/bar/2.0.0/bar-2.0.0.jar")
        _repo.open.assert_called_with("foo/bar/2.0.0/bar-2.0.0.jar")

        self.assertRaises(MissingArtifactError, client.get_artifact,
                          "foo:bar:3.0")
        _repo.exists.assert_called_with("foo/bar/3.0/bar-3.0.jar")
        _repo.open.assert_not_called()

        self.assertRaises(AssertionError, client.get_artifact,
                          "foo:bar:[1.0,2.0]")


@mock.patch("pymaven.client.HttpRepository._request")
class TestHttpRespository(unittest.TestCase):
    def test_listdir(self, _request):
        res = mock.MagicMock(spec=Struct)
        res.__enter__.return_value = StringIO(SIMPLE_METADATA)
        _request.side_effect = [res, requests.exceptions.HTTPError]
        expected = ["1.0-SNAPSHOT",
                    "1.0",
                    "3.0-SNAPSHOT",
                    "2.0.0",<commit_msg>Fix client test<commit_after>#
# Copyright (c) SAS Institute Inc.
#
# 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.
#


from StringIO import StringIO
import os
import tempfile
import unittest

import mock
import requests

from pymaven import Artifact
from pymaven.client import HttpRepository
from pymaven.client import LocalRepository
from pymaven.client import MavenClient
from pymaven.client import Struct
from pymaven.errors import MissingPathError
from pymaven.errors import MissingArtifactError


class TestMavenClient(unittest.TestCase):
    def test_invalid_repo(self):
        self.assertRaises(ValueError, MavenClient, "foo://bar.com")

    @mock.patch("pymaven.client.LocalRepository")
    def test_find_artifacts(self, _LocalRepository):
        _repo1 = mock.Mock(spec=LocalRepository)
        _repo2 = mock.Mock(spec=LocalRepository)

        _LocalRepository.side_effect = [_repo1, _repo2]

        _repo1.get_versions.return_value = [Artifact("foo:bar:2.0"),
                                            Artifact("foo:bar:2.0-SNAPSHOT"),
                                            Artifact("foo:bar:1.0"),
                                            Artifact("foo:bar:1.0-SNAPSHOT"),
                                            ]

        _repo2.get_versions.return_value = [Artifact("foo:bar:3.0"),
                                            Artifact("foo:bar:3.0-SNAPSHOT"),
                                            Artifact("foo:bar:1.0"),
                                            Artifact("foo:bar:1.0-SNAPSHOT"),
                                            ]
        client = MavenClient("foobar", "foobaz")
        expected = [Artifact("foo:bar:3.0"),
                    Artifact("foo:bar:3.0-SNAPSHOT"),
                    Artifact("foo:bar:2.0"),
                    Artifact("foo:bar:2.0-SNAPSHOT"),
                    Artifact("foo:bar:1.0"),
                    Artifact("foo:bar:1.0-SNAPSHOT"),
                    ]
        actual = client.find_artifacts("foo:bar")
        assert expected == actual, "client.find_artifacts(%s)" % input

    @mock.patch("pymaven.client.LocalRepository")
    def test_get_artifact(self, _LocalRepository):

        _repo = mock.Mock(spec=LocalRepository)

        _repo.exists.side_effect = [True, False]
        _repo.open.return_value = StringIO("some data")

        _LocalRepository.return_value = _repo

        client = MavenClient("/maven")

        actual = client.get_artifact("foo:bar:2.0.0")
        assert "some data" == actual.contents.read()
        _repo.exists.assert_called_with("foo/bar/2.0.0/bar-2.0.0.jar")
        _repo.open.assert_called_with("foo/bar/2.0.0/bar-2.0.0.jar")

        _repo.reset_mock()
        self.assertRaises(MissingArtifactError, client.get_artifact,
                          "foo:bar:3.0")
        _repo.exists.assert_called_with("foo/bar/3.0/bar-3.0.jar")
        _repo.open.assert_not_called()

        _repo.reset_mock()
        self.assertRaises(AssertionError, client.get_artifact,
                          "foo:bar:[1.0,2.0]")
        _repo.open.assert_not_called()


@mock.patch("pymaven.client.HttpRepository._request")
class TestHttpRespository(unittest.TestCase):
    def test_listdir(self, _request):
        res = mock.MagicMock(spec=Struct)
        res.__enter__.return_value = StringIO(SIMPLE_METADATA)
        _request.side_effect = [res, requests.exceptions.HTTPError]
        expected = ["1.0-SNAPSHOT",
                    "1.0",
                    "3.0-SNAPSHOT",
                    "2.0.0",
{'fraction_non_alphanumeric': 0.11112507856693903, 'fraction_numerical': 0.018101822752985542, 'mean_word_length': 2.174780526735834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5408503', 'n_tokens_mistral': 2350, 'n_tokens_neox': 2295, 'n_words': 446}