class TestSolrProvisioner(unittest.TestCase):
    """
    Test the SolrProvisioner. Use the Docker builder to create the index
    """

    def setUp(self):
        """
        Starts a solr node for all the tests
        """
        self.name = 'livetest-solr-{}'.format(gen_salt(5))
        self.builder = SolrDockerRunner(
            name=self.name,
        )

        self.builder.start()
        self.port = self.builder.client.port(
            self.builder.container['Id'],
            8983
        )[0]['HostPort']

    def tearDown(self):
        """
        Tears down the consul node used by the tests
        """
        self.builder.teardown()

    def _provision(self, service):
        """
        Run the provision for a given service
        """
        SolrProvisioner(service, container=self.builder)()

    def test_provisioning_recommender_service(self):
        """
        First run the provisioner and then we can check that the documents have been added to the solr index
        """
        self._provision('recommender')

        # Obtain what we expect to find
        config_file = '{}/{}/recommender/recommender.json'.format(
            SolrProvisioner.template_dir,
            SolrProvisioner.name,
        )

        with open(config_file) as json_file:
            config = json.load(json_file)

        # Compare with consul
        for document in config:
            response = requests.get('http://{host}:{port}/solr/query?q=bibcode:{bibcode}'.format(
                host='localhost',
                port=self.port,
                bibcode=document['bibcode'])
            )
            self.assertEqual(
                200,
                response.status_code,
                msg='We expect a 200 response but got: {}, {}'.format(response.status_code, response.json())
            )

            actual_document = response.json()['response']['docs'][0]

            # Not all key/values are returned in the same fashion, so the following specified are some hand-picked
            # keywords that we want to test were entered correctly into Solr
            known_keys = ['pubdate', 'first_author', 'abstract', 'read_count', 'doctype', 'year', 'bibcode', 'volume']

            for key in known_keys:
                self.assertIn(
                    key,
                    actual_document.keys(),
                    msg='Could not find key "{}" in response: {}'.format(key, actual_document.keys())
                )
                self.assertEqual(
                    document[key],
                    actual_document[key],
                    msg='Key "{}" mismatch: expected "{}" != actual "{}"'.format(
                        key,
                        document[key],
                        actual_document[key]
                    )
                )