# -*- coding: utf-8 -*-
# Import pytohn libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
# Import Salt libs
import salt.modules.nexus as nexus
@skipIf(NO_MOCK, NO_MOCK_REASON)
class nexusTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
return {nexus: {}}
def test_artifact_get_metadata(self):
with patch('salt.modules.nexus._get_artifact_metadata_xml', MagicMock(return_value='''
com.company.sampleapp.web-module
web
0.1.0
0.0.1
0.0.2
0.0.3
0.1.0
20171010143552
''')):
metadata = nexus._get_artifact_metadata(nexus_url='http://nexus.example.com/repository',
repository='libs-releases',
group_id='com.company.sampleapp.web-module',
artifact_id='web',
headers={})
self.assertEqual(metadata['latest_version'], '0.1.0')
def test_snapshot_version_get_metadata(self):
with patch('salt.modules.nexus._get_snapshot_version_metadata_xml', MagicMock(return_value='''
com.company.sampleapp.web-module
web
0.0.2-SNAPSHOT
20170920.212353
3
20171112171500
sans-externalized
jar
0.0.2-20170920.212353-3
20170920212353
dist
zip
0.0.2-20170920.212353-3
20170920212353
''')):
metadata = nexus._get_snapshot_version_metadata(nexus_url='http://nexus.example.com/repository',
repository='libs-releases',
group_id='com.company.sampleapp.web-module',
artifact_id='web',
version='0.0.2-SNAPSHOT',
headers={})
self.assertEqual(metadata['snapshot_versions']['dist'], '0.0.2-20170920.212353-3')
def test_artifact_metadata_url(self):
metadata_url = nexus._get_artifact_metadata_url(nexus_url='http://nexus.example.com/repository',
repository='libs-releases',
group_id='com.company.sampleapp.web-module',
artifact_id='web')
self.assertEqual(metadata_url, "http://nexus.example.com/repository/libs-releases/com/company/sampleapp/web-module/web/maven-metadata.xml")
def test_snapshot_version_metadata_url(self):
metadata_url = nexus._get_snapshot_version_metadata_url(nexus_url='http://nexus.example.com/repository',
repository='libs-snapshots',
group_id='com.company.sampleapp.web-module',
artifact_id='web',
version='0.0.2-SNAPSHOT')
self.assertEqual(metadata_url, "http://nexus.example.com/repository/libs-snapshots/com/company/sampleapp/web-module/web/0.0.2-SNAPSHOT/maven-metadata.xml")
def test_construct_url_for_released_version(self):
artifact_url, file_name = nexus._get_release_url(repository='libs-releases',
group_id='com.company.sampleapp.web-module',
artifact_id='web',
packaging='zip',
nexus_url='http://nexus.example.com/repository',
version='0.1.0')
self.assertEqual(artifact_url, "http://nexus.example.com/repository/libs-releases/com/company/sampleapp/web-module/web/0.1.0/web-0.1.0.zip")
self.assertEqual(file_name, "web-0.1.0.zip")
def test_construct_url_for_snapshot_version(self):
with patch('salt.modules.nexus._get_snapshot_version_metadata', MagicMock(return_value={'snapshot_versions': {'zip': '0.0.2-20170920.212353-3'}})):
artifact_url, file_name = nexus._get_snapshot_url(nexus_url='http://nexus.example.com/repository',
repository='libs-snapshots',
group_id='com.company.sampleapp.web-module',
artifact_id='web',
version='0.2.0-SNAPSHOT',
packaging='zip',
headers={})
self.assertEqual(artifact_url, "http://nexus.example.com/repository/libs-snapshots/com/company/sampleapp/web-module/web/0.2.0-SNAPSHOT/web-0.0.2-20170920.212353-3.zip")
self.assertEqual(file_name, "web-0.0.2-20170920.212353-3.zip")