Skip to content

Commit

Permalink
Merge pull request #28 from ClareChu/pool
Browse files Browse the repository at this point in the history
support es
  • Loading branch information
john-deng authored Feb 7, 2020
2 parents c04a55a + ee746d8 commit f79d279
Show file tree
Hide file tree
Showing 21 changed files with 659 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/es/config/application-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# config file for testing

server:
port: 8080

logging:
level: info

es:
host: 10.10.13.5
port: 9200
16 changes: 16 additions & 0 deletions examples/es/config/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# config file for testing
# filename should be application.yml

app:
project: examples
name: es-demo
profiles:
include:
- actuator
- locale
- logging
- es

logging:
level: info

3 changes: 3 additions & 0 deletions examples/es/config/i18n/en-US.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
success = Success
failed = Failed
hello = Hello
3 changes: 3 additions & 0 deletions examples/es/config/i18n/zh-CN.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
success = 成功
failed = 失败
hello = 您好
19 changes: 19 additions & 0 deletions examples/es/config/keygen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Copyright 2018 John Deng (hi.devops.io@gmail.com).
#
# 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.

mkdir -p ssl

openssl genrsa -out ssl/app.rsa 1024
openssl rsa -in ssl/app.rsa -pubout > ssl/app.rsa.pub
15 changes: 15 additions & 0 deletions examples/es/config/ssl/app.rsa
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDBaFochwjb/PFXA9EOmynT+SQr54wh9HMx5VFj0bNUKegMS02W
4DSsneRVaY5Bo+8sDoNPx2Luzp7pPRNbukP4pjgVWAASVWnGeBaGRaYbo/d9SV/R
OdKwmV1DJM3PtOo82oNnI26FJeJ/oTl+5UJ4sd7J8UbR0DCu40venKLlOwIDAQAB
AoGAOFuJDpEGstvlE38INyJdsOgDc0u4mNhbjOmOqvCbOeYwOQzkYheZIceslnr4
ugXkFslv2Bp4qK/KvJpQfsUrMtGNr1FKfwLzD09++GU89ltufgn8Z6iYJ+app0qa
hYKAoGQkvPP4ks+k3nBYRESL01y9mKZ/JSvWfKj8wHOu1pkCQQD/Pdom+5r2q150
QUkw4huDthHBB96LMJiVLSNkPoHXPwXqSoa8CXNquCQA43F1gjtCz37rZDEyFYWH
IYT4TcG1AkEAwft3WsON1ND0p8Z+iUCe/+ySajBvH4ICJ6Muj8D4FdydRvBhNJT0
qmZYZI19Bjk8qYHV7UoUhQQcfBB8TVQhLwJBAOE10CEJbEguGR9243+wYhqrMasi
qGgkLt2hgFLZHHOx9iQuMggERsRpSctG5DuM94ER2KkWNJo+hx0epFb7NOkCQEnq
1c9LRyd6PFAWTi9k5XGmJyC5tg5ujv2lq99xa9eAypm2jYPo4tZvy2z9kU+mzdvV
srwKB4msSGJqMcORtFcCQQDJ1nXPTCgptvny+FhkFGAy3UNLdGXKeAM9UHhJW+/Z
RJvfo1hEyCPZZbPrMrPzqkJ8pBd2VRwoevlJreZDekvd
-----END RSA PRIVATE KEY-----
6 changes: 6 additions & 0 deletions examples/es/config/ssl/app.rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBaFochwjb/PFXA9EOmynT+SQr
54wh9HMx5VFj0bNUKegMS02W4DSsneRVaY5Bo+8sDoNPx2Luzp7pPRNbukP4pjgV
WAASVWnGeBaGRaYbo/d9SV/ROdKwmV1DJM3PtOo82oNnI26FJeJ/oTl+5UJ4sd7J
8UbR0DCu40venKLlOwIDAQAB
-----END PUBLIC KEY-----
77 changes: 77 additions & 0 deletions examples/es/controller/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// 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.

package controller

import (
"hidevops.io/hiboot-data/examples/es/entity"
"hidevops.io/hiboot-data/examples/es/service"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/at"
"hidevops.io/hiboot/pkg/model"
"net/http"
)

// RestController
type userController struct {
at.RestController

userService service.UserService
}

func init() {
app.Register(newUserController)
}

// newUserController inject userService automatically
func newUserController(userService service.UserService) *userController {
return &userController{
userService: userService,
}
}

// Post POST /user
func (c *userController) Post(request *entity.User) (model.Response, error) {
user, err := c.userService.AddUser(request)
response := new(model.BaseResponse)
response.SetData(user)
return response, err
}

// GetById GET /id/{id}
func (c *userController) GetById(id string) (response model.Response, err error) {
user, err := c.userService.GetUser(id)
response = new(model.BaseResponse)
if err != nil {
response.SetCode(http.StatusNotFound)
} else {
response.SetData(user)
}
return
}

// GetById GET /id/{id}
func (c *userController) GetAll() (response model.Response, err error) {
users, err := c.userService.GetAll()
response = new(model.BaseResponse)
response.SetData(users)
return
}

// DeleteById DELETE /id/{id}
func (c *userController) DeleteById(id string) (response model.Response, err error) {
err = c.userService.DeleteUser(id)
response = new(model.BaseResponse)
return
}
103 changes: 103 additions & 0 deletions examples/es/controller/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// 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.

package controller

import (
"errors"
"github.com/stretchr/testify/assert"
"hidevops.io/hiboot-data/examples/es/entity"
"hidevops.io/hiboot-data/examples/es/service/mocks"
"hidevops.io/hiboot/pkg/app/web"
"hidevops.io/hiboot/pkg/log"
"hidevops.io/hiboot/pkg/utils/idgen"
"net/http"
"testing"
)

func init() {
log.SetLevel(log.DebugLevel)
}

func TestCrdRequest(t *testing.T) {

mockUserService := new(mocks.UserService)
userController := newUserController(mockUserService)
testApp := web.NewTestApp(userController).Run(t)

id, err := idgen.NextString()
assert.Equal(t, nil, err)

testUser := &entity.User{
Id: id,
Name: "Bill Gates",
Username: "billg",
Password: "3948tdaD",
Email: "bill.gates@microsoft.com",
Age: 60,
Gender: 1,
}

// first, call mocks.UserService.AddUser
mockUserService.On("AddUser", testUser).Return(testUser, nil)
// then run the test that will call UserService.AddUser
t.Run("should add user with POST request", func(t *testing.T) {
// First, let's Post User
testApp.Post("/user").
WithJSON(testUser).
Expect().Status(http.StatusOK)
})

mockUserService.On("GetUser", id).Return(testUser, nil)
t.Run("should get user with GET request", func(t *testing.T) {
// Then Get User
// e.g. GET /user/id/123456
testApp.Get("/user/id/{id}").
WithPath("id", id).
Expect().Status(http.StatusOK)
})

mockUserService.On("GetAll").Return(&[]entity.User{*testUser}, nil)
t.Run("should get user with GET request", func(t *testing.T) {
// Then Get User
// e.g. GET /user/id/123456
testApp.Get("/user/all").
Expect().Status(http.StatusOK)
})

// assert that the expectations were met
mockUserService.AssertExpectations(t)

unknownId, err := idgen.NextString()
assert.Equal(t, nil, err)
mockUserService.On("GetUser", unknownId).Return((*entity.User)(nil), errors.New("not found"))

t.Run("should return 404 if trying to find a record that does not exist", func(t *testing.T) {
// Then Get User
testApp.Get("/user/id/{id}").
WithPath("id", unknownId).
Expect().Status(http.StatusNotFound)
})

// assert that the expectations were met
mockUserService.AssertExpectations(t)

mockUserService.On("DeleteUser", id).Return(nil)
t.Run("should delete the record with DELETE request", func(t *testing.T) {
// Finally Delete User
testApp.Delete("/user/id/{id}").
WithPath("id", id).
Expect().Status(http.StatusOK)
})
}
28 changes: 28 additions & 0 deletions examples/es/entity/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// 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.

package entity

import "hidevops.io/hiboot/pkg/model"

type User struct {
model.RequestBody
Id string `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Age uint `json:"age"`
Gender uint `json:"gender"`
}
15 changes: 15 additions & 0 deletions examples/es/entity/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// 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.

package entity
33 changes: 33 additions & 0 deletions examples/es/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// 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.

package main

import (
_ "hidevops.io/hiboot-data/examples/es/controller"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/app/web"
"hidevops.io/hiboot/pkg/starter/actuator"
"hidevops.io/hiboot/pkg/starter/locale"
"hidevops.io/hiboot/pkg/starter/logging"
)

func main() {
web.NewApplication().
SetProperty(app.ProfilesInclude,
actuator.Profile,
locale.Profile,
logging.Profile).
Run()
}
21 changes: 21 additions & 0 deletions examples/es/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// 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.

package main

import "testing"

func TestRunMain(t *testing.T) {
go main()
}
Loading

0 comments on commit f79d279

Please sign in to comment.