Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package omci |
| 18 | |
| 19 | import ( |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 20 | "testing" |
| 21 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 22 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 23 | "github.com/opencord/omci-lib-go/v2" |
| 24 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 25 | "gotest.tools/assert" |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func omciToCreateResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.CreateResponse { |
| 29 | msgLayer := (*omciPkt).Layer(omci.LayerTypeCreateResponse) |
| 30 | if msgLayer == nil { |
| 31 | t.Fatal("omci Msg layer could not be detected for CreateResponse - handling of MibSyncChan stopped") |
| 32 | } |
| 33 | msgObj, msgOk := msgLayer.(*omci.CreateResponse) |
| 34 | if !msgOk { |
| 35 | t.Fatal("omci Msg layer could not be assigned for CreateResponse - handling of MibSyncChan stopped") |
| 36 | } |
| 37 | return msgObj |
| 38 | } |
| 39 | |
| 40 | type createArgs struct { |
| 41 | omciPkt []byte |
| 42 | result me.Results |
| 43 | } |
| 44 | |
| 45 | type createWant struct { |
| 46 | result me.Results |
| 47 | } |
| 48 | |
| 49 | func TestCreateResponse(t *testing.T) { |
| 50 | |
| 51 | // generate a CreateRequest packet to create a GemPort |
| 52 | omciReq := &omci.CreateRequest{ |
| 53 | MeBasePacket: omci.MeBasePacket{ |
| 54 | EntityClass: me.GemPortNetworkCtpClassID, |
| 55 | EntityInstance: 12, |
| 56 | }, |
| 57 | Attributes: me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 58 | me.GemPortNetworkCtp_PortId: 0, |
| 59 | me.GemPortNetworkCtp_TContPointer: 0, |
| 60 | me.GemPortNetworkCtp_Direction: 0, |
| 61 | me.GemPortNetworkCtp_TrafficManagementPointerForUpstream: 0, |
| 62 | me.GemPortNetworkCtp_TrafficDescriptorProfilePointerForUpstream: 0, |
| 63 | me.GemPortNetworkCtp_PriorityQueuePointerForDownStream: 0, |
| 64 | me.GemPortNetworkCtp_TrafficDescriptorProfilePointerForDownstream: 0, |
| 65 | me.GemPortNetworkCtp_EncryptionKeyRing: 0, |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 66 | }, |
| 67 | } |
| 68 | omciPkt, err := Serialize(omci.CreateRequestType, omciReq, 66) |
| 69 | if err != nil { |
| 70 | t.Fatal(err.Error()) |
| 71 | } |
| 72 | |
| 73 | omciPkt, _ = HexEncode(omciPkt) |
| 74 | |
| 75 | tests := []struct { |
| 76 | name string |
| 77 | args createArgs |
| 78 | want createWant |
| 79 | }{ |
| 80 | {"createSuccess", |
| 81 | createArgs{omciPkt, me.Success}, |
| 82 | createWant{me.Success}, |
| 83 | }, |
| 84 | {"createProcessingError", |
| 85 | createArgs{omciPkt, me.ProcessingError}, |
| 86 | createWant{me.ProcessingError}, |
| 87 | }, |
| 88 | } |
| 89 | for _, tt := range tests { |
| 90 | t.Run(tt.name, func(t *testing.T) { |
| 91 | pkt, msg, _ := ParseOpenOltOmciPacket(tt.args.omciPkt) |
| 92 | requestPkt, _ := CreateCreateResponse(pkt, msg, tt.args.result) |
| 93 | |
| 94 | omciMsg, omciPkt := omciBytesToMsg(t, requestPkt) |
| 95 | |
| 96 | assert.Equal(t, omciMsg.MessageType, omci.CreateResponseType) |
| 97 | |
| 98 | getResponseLayer := omciToCreateResponse(t, omciPkt) |
| 99 | |
| 100 | assert.Equal(t, getResponseLayer.Result, tt.want.result) |
| 101 | |
| 102 | }) |
| 103 | } |
| 104 | } |