def test_get_troop_commander(self): # add a commander commander = CommanderInfo(id='cxxx1', name='cmd_http', place="S103", endpoint='http://localhost:50000', subordinates=[], campaigns=[]) self.app.put('/recruiter/commanders/cxxx1', data=json.dumps(commander.to_dict()), content_type='application/json') # get commander's info with all leader_ids leaders = ['lxxx2', 'lxxx3', 'lxxx4'] places = {"lxxx2": "desk2", "lxxx3": "desk3", "lxxx4": "desk4"} for leader_id in leaders: response = self.app.get('/recruiter/department/troop/commander' + '?leader_id=' + leader_id) self.assertEqual(response.status_code, 200) actual = json.loads(response.data.decode("utf-8")) # assert expected = { "_status": {'success': True, 'msg': "status is ok"}, "commander": commander.to_dict(), "place": places[leader_id] } self.assertEqual(actual, expected) pass
def gen_commander_data(com_addr): res, err = rest.get(com_addr) if err is not None: return {} info = CommanderInfo.make(res.json()["info"]) url = "{0}subordinates".format(com_addr) res, err = rest.get(url) if err is not None: return {} subs = res.json()["subordinates"] sub_addr_list = [sub["endpoint"] for sub in subs] leaders = [gen_leader_data(addr) for addr in sub_addr_list] leaders = [l for l in leaders if len(l) != 0] return { "text": "Commander: {0}({1}) at {2}". format(info.name, info.id, info.place), "icon": "troops commander", "href": info.endpoint, "nodes": [ { "text": "campaigns", "nodes": [gen_campaign_data(c) for c in info.campaigns] }, { "text": "subordinates", "nodes": leaders }, ] }
def gen_commander_data(com_addr): res, err = rest.get(com_addr) if err is not None: return {} info = CommanderInfo.make(res.json()["info"]) url = "{0}subordinates".format(com_addr) res, err = rest.get(url) if err is not None: return {} subs = res.json()["subordinates"] sub_addr_list = [sub["endpoint"] for sub in subs] leaders = [gen_leader_data(addr) for addr in sub_addr_list] leaders = [l for l in leaders if len(l) != 0] return { "text": "Commander: {0}({1}) at {2}".format(info.name, info.id, info.place), "icon": "troops commander", "href": info.endpoint, "nodes": [ { "text": "campaigns", "nodes": [gen_campaign_data(c) for c in info.campaigns] }, { "text": "subordinates", "nodes": leaders }, ] }
def test_get_info(self): response = self.app.get('/commander', follow_redirects=True) self.assertEqual(response.status_code, 200) actual = json.loads(response.data.decode("utf-8")) com = CommanderInfo(id='cxxx0', name='cmd_http', place="", endpoint='http://localhost:50000', subordinates=[], campaigns=[]) expected = { "_status": { 'success': True, 'msg': "status is ok" }, "info": com.to_dict() } self.assertEqual(actual, expected)
def test_delete_commander_info(self): # add a commander commander = CommanderInfo(id='cxxx0', name='cmd_http', place="", endpoint='http://localhost:50000', subordinates=[], campaigns=[]) self.app.put('/recruiter/commanders/cxxx0', data=json.dumps(commander.to_dict()), content_type='application/json') response = self.app.delete('/recruiter/commanders/cxxx0') self.assertEqual(response.status_code, 200) actual = json.loads(response.data.decode("utf-8")) expected = { "_status": {'success': True, 'msg': "status is ok"} } self.assertEqual(actual, expected)
def register_commanders(com_id): """ Register a commander --- parameters: - name: com_id description: ID of a requested commander in: path type: string - name: commander in: body required: true schema: $ref: '#/definitions/CommanderInfo' responses: 200: description: ok schema: properties: _status: description: Response status $ref: '#/definitions/ResponseStatus' commander: $ref: '#/definitions/CommanderInfo' 400: description: '[NT] invalid input' schema: properties: _status: description: Response status $ref: '#/definitions/ResponseStatus' input: $ref: '#/definitions/CommanderInfo' """ msgs = { 400: "Input parameter is invalid", } try: com = CommanderInfo.make(request.json) except TypeError: return jsonify(_status=ResponseStatus.make_error(msgs[400]), input=request.json), 400 if com.id != com_id: return jsonify(_status=ResponseStatus.make_error(msgs[400]), input=request.json), 400 accepted = recruiter.register_commander_info(com) if accepted is None: return jsonify(_status=ResponseStatus.NotFound), 404 return jsonify(_status=ResponseStatus.Success, commander=accepted.to_dict())
def awake(self, rec_ep: str, heartbeat_rate: int): from model import CommanderInfo # 上官を解決する url = rec_ep + 'department/troop/commander?leader_id=' + self.id res, err = rest.get(url) if err is not None: return False self.place = res.json()["place"] superior = CommanderInfo.make(res.json()['commander']) self.superior_ep = superior.endpoint logger.info("superior was resolved: id={0}".format(superior.id)) # 部隊に加入する url = self.superior_ep + "subordinates" res, err = rest.post(url, json=self.generate_info().to_dict()) if err is not None: return False logger.info("joined to squad: commander_id: {0}".format(superior.id)) # missionを取得する self.start_heartbeat(heartbeat_rate) return True
def register_commander_info(self, com_info: CommanderInfo): if com_info.id not in self.TroopList: return None com_info.place = self.places[com_info.id] self.commander_cache[com_info.id] = com_info return com_info